As a newbie to c++, coming from python, I'm not sure why c++ doesn't allow code outside of a function (in the global namespace?). It seems like this could be useful to do some initialization before main() is called or other functions are even declared. (I'm not trying to argue with the compiler, I'd just like to know the thought process behind implementing it this way.)
By using functions, we can avoid rewriting same logic/code again and again in a program. We can call C functions any number of times in a program and from any place in a program. We can track a large C program easily when it is divided into multiple functions. Reusability is the main achievement of C functions.
For every C program, the execution starts from the main() function. It is mandatory to include a main() function in every C program. Includes all user-defined functions (functions the user provides).
Syntax of function declaration A function declaration has three main components: return type, function name, and parameters. The function name is used to identify the function uniquely in code. Function parameters are included in the declaration to identify the number and types of inputs that the function accepts.
Nested function is not supported by C because we cannot define a function within another function in C. We can declare a function inside a function, but it's not a nested function.
When you're running a python program, the interpreter runs through it from top to bottom executing as it goes. In C++, that doesn't happen. The compiler builds all your functions into little blobs of machine code and then the linker hooks them up. At runtime, the operating system calls your main
function, and everything goes on from there. In that context, code outside of functions is meaningless - when would it run?
This can be thought of as a style difference between C++ and Python. However, there are pretty good reasons for it too. In C and C++, there is a very clear and specific place that the execution of your code starts from, and that's the beginning of the main()
function (of course, this is only an approximation of the truth, but we can ignore that for now.) Actually, a C/C++ program starts and ends with the main()
function which - in my opinion - helps quite a lot when you want to understand what a program actually does. The high-level flow of the program is clearer. Contrast this with the alternative; with code scattered all through the file and in between functions and whatnot.
Even in a well-organized and non-trivial Python program, you put your main body of code under an if __name__ == "__main__":
, don't you?
Now for some stuff that are a little bit more advanced. There are ways for code to run before the main()
function is called. Here's one of them:
#include <iostream>
using namespace std;
bool RunBeforeMain ()
{
cout << "Before main()!" << endl;
return true;
}
// This variable here causes the above function to be called
bool ignore_this_variable = RunBeforeMain ();
int main ()
{
cout << "Start of main()" << endl;
return 0;
}
Also, the constructors of all the global variables and all the static
members of classes and some platform-dependent stuff are run before main()
. Analogously, there are ways for code to run after the main()
is finished. Those are usually the destructors for objects constructed before main()
began, and functions registered with the atexit()
function.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With