Lesson 0: Writing the entry point to our program


In this lesson we introduce the entry point to our program, a function that appears in all programs, even the web browser your reading this in would have one. It is where the program starts, like a book you read starts at the first page, a program always starts at the entry point.

In C/C++ and many other languages, the entry point is defined by a special function called the main function. If you try and compile your program & is doesn't have this special function, there will be an error saying no main function defined. This is the compiler saying 'I don't know where this program starts'.

Before we go any further, what is a function? Programs are built up by two different building blocks: functions & variables. Functions are the verbs of the program whereas variables are the nouns. The best way to develop a sense of what a function is, is to start using them. Just to get an overview, you can think of them as some process that takes in some information, does some stuff, and returns some information (although it doesn't have to do either of this things, if you don't want it to & other parts of your program aren't relying on it).

Let's have a look at the main function. When we write it in code it looks like this:

int main(int argc, char **argv) {
  return 0;
}

This may look very obscure, and there is quite a bit packed into this, but not to worry, for now you just have to understand we put this at the start of our programs. To explain it a bit more, the name of the function is main, and the int to the left of main is the return type. This means the function is returning an integer (a positive or negative whole number i.e. no fractions). It is up us to choose what that number is, but it has to be a number (not a word or a fractional number). To the right of main in the parentheses, these are the input information. It says no matter who calls this function, it's guranteed to come with this information. So again let's disect this. The first bit of information (or variable) before the comma:

int argc

the type is in integer (abbreviated as int) and we can refer to it by it's name argc in our code.

The second bit of information (or variable) after the comma:

char **argv

the type is a char ** (this is a pretty compilcated type & we don't need to understand it at the moment) and we can refer to it by it's name argv in our code.

Then the end parentheses says 'that's the end of the input variables', and the bracket means 'whatever is after this bracket, & before the end bracket contains the code of our function'. It's like defining a paragraph in a story. It helps the book (or program) easier to understand, and groups similar ideas together. In this case, it's grouping our whole program together.

At the end of our function, just before the end bracket, there is a line of code:

return 0;

This is us fufilling our promise that we were going to give back (or return) an int (integer) at the end of our function. In this case we returning 0. We could have returned 7 or 10, but in this case the number means was everything alright? And if we return zero, whoever reads it will know everything went smoothly. If we didn't add this line, our compiler would issue an error, knowing that it expects an integer, but there isn't one being returned.

But again for now, we just have to copy and paste this at the start of our source code (which we'll do in lesson 2). In the next lesson we'll go over some simpler functions to get a better grasp on functions.

Quick Quiz:

What is the entry point to a program called? Click to see

the main function

What are the two building blocks of a program? Click to see

Functions & Variables

What is the code for the main function? Click to see

int main(int argc, char **argv) {
  return 0;
}

To Next Lesson