Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why must c++ code be contained within functions?

Tags:

c++

python

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.)

like image 284
joshua.thomas.bird Avatar asked Mar 11 '13 05:03

joshua.thomas.bird


People also ask

Why do we need functions in C programming?

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.

Which function is compulsory in C programming?

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).

What are the components of function in C?

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.

Can you write a function within a function in C?

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.


2 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?

like image 94
Carl Norum Avatar answered Oct 27 '22 01:10

Carl Norum


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.

like image 43
yzt Avatar answered Oct 26 '22 23:10

yzt