In this video, we'll briefly go over the c++ main function.
Every c++ program must have exactly one main function somewhere.
A c++ program can consist of hundreds of files,
but one of those files must contain the main function.
Note that the name main must be written in lowercase letters.
When a c++ program executes,
the main function is called by the operating system,
and the code between the curly braces executes.
When execution hits the return statement,
the program returns the integer value to the operating system.
If the return value is zero, then the program terminated successfully.
If the return value is not zero,
then the operating system can check the value returned and determine what went wrong.
There are two versions of main,
and they're both valid as per the c++ specification.
So far we've only seen the version on the left, which returns an integer
and has nothing between the two parentheses.
This version of main expects no information from the operating system in order to run.
This is the version that we'll use mostly in this course.
The second version of main is the one on the right. In this version,
main expects information from the operating system.
So suppose that when you execute the program,
you need to pass information into the program from the command line.
This is very, very common for command line applications.
In this case, we tell the compiler that we need two pieces of information.
First, we need the number of pieces of information that were passed in.
That's argc.
We can use any variable name here. But historically, we use argc,
which stands for argument count.
The second piece of information is the actual arguments that we passed into the program.
This is captured by argv, which is the argument vector.
The syntax for argv looks a little strange,
but it'll make sense as the course progresses.
It's basically a bunch of strings, such as program.exe,
argument1 and argument2 in this example.
We'll talk more about strings and vectors as we move forward,
but I wanted to show you both versions of the main function
in case you're looking at code out there and you see the second version,
both are perfectly valid and both are commonly seen.
Note that main must always return an integer.
Main happens to be an example of a function.
A function is basically a name that we can use to reference a block of code.
In this example, main is everything between the curly braces.
Functions are very commonly used
and we'll write our own functions as the course progresses.
There'll be an entire section of the course devoted to functions,
but main is a very special function.
As we learn c++, our entire program
will be contained inside the main function.
And as our programs become more complex,
we'll write our own functions and then classes
to better modularize and organize our code.
In the next video, we'll talk about namespaces
and see how we can use them in our first program.