If you've had prior programming experience with languages like Python or
JavaScript, you will know that the interpreter runs your code from top to
bottom. But in C++, we have a function called main
where the
execution begins.
A function is a just a set of statements. These statements, however, are not executed unless specifically told to do so. In order to execute those statements, the function must first be called. So instead of writing the same code over and over again at different places, we can put it into a function and call the function when we want.
The main
is a special function which is not called.
Let's begin with a simple Hello World program.
#include <iostream>
using namespace std;
int main() {
cout << "Hello, world!";
return 0;
}
#include <iostream>
The very first line is called a preprocessor directive. You need to
tell the compiler what Header files you need in your program. Header
files are just files with more C++ code in them which allow you to do cool
things. In our case, the iostream
library lets you take input and print
output onto a console.
int main()
As I mentioned, the main function is where the execution of the program
starts. The statements are executed in order, from top to bottom. The int
is
short for integer. Often, you will want a function to give you a result after
it executes, and the int is the type of the result the function will pass
back. More on this in future posts. Then the name of the function (i.e., main)
followed by the brackets (that's how you tell the compiler that it's a
function).
The curly brackets encloses what is called the function body. We use curly brackets to demarcate where the function starts and where it ends.
Let's not worry about
using namespace std;
for now.
cout
(pronounced as see-out) is
from the iostream header we included earlier and is used to print text onto
the console. The <<
is known as the
insertion operator. We then put in what we actually want to print, i.e, "Hello
World!" (in double quotes as it is a string). We'll talk more about operators
and strings in upcoming posts.
All statements end with a semicolon in C++. The curly brackets and the int
main()
do not end in semicolons because they're not considered as statements.
In fact, you could write all of it in one line without any complaints from the
compiler. They are just written like that to make it easy for (we humans)
programmers to understand and read. The preprocessor directive, however, does
not end in a semicolon because it it not a statement but you're not allowed to
write those in a single line. The following posts will make it clear why.
The next statement, and the last one to be executed, is called a
return statement. Remember the value a function can give back after
execution? The return keyword does exactly that. In this case it's returning
0. But why return a 0 anyway? That's because the value returned by main is
often used by developers to check if things went right. Negative numbers like
-1, -2 are used to tell something did not go right. 0 conventionally means
everything went well.
Note: C++ ignores blank lines.
I do understand that many topics might not be clear after reading this post.
The following posts will shed more light on these topics. Do consider
following the blog to know more about C++ (and Python)
Regards,
Lightspeed