Why must c++ statements be contained within functions?

Question:

As a newbie to c++, coming from python, I’m not sure why c++ doesn’t allow statements 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.)

Asked By: joshua.thomas.bird

||

Answers:

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?

Answered By: Carl Norum

The main() is the access point to the program. So any code you wanna write would be needs to have an order of execution from that point.

Static variables are initiated before the main() is executed, so you can initiate whatever variables you need before that.

But if you want run code that initiates the state of the program, you should do it just in the beginning of the program, and abuse the static variables and do it some constructor.

Answered By: Yochai Timmer

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.

Answered By: yzt
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.