In c++ execution of the program starts from main () function.
If it is so then why we need to define user-defined functions prototypes before main () body?
I am really confused about this thing.
Someone please help me to clarify my confusion.
Thanks
But if you are to call a function, the language's type system will want to ensure you're using it correctly. It can't check too much, but it can check the number and type of the arguments and the return type are correct, but it needs a reference description of what is correct. That's what a prototype is.
You're not allowed to define functions inside other functions.
You can wrap a function within a class or use lambdas.
1 2 3 4 5 6 7 8 9 10 11 12 13
#include <iostream>
int main()
{
class foo
{
public:
staticvoid bar() { std::cout << "Hello, "; }
};
auto baz = [](){ std::cout << "world!"; };
foo::bar(),baz();
return 0;
}
The reason for prototypes is to let the compiler know that a function exists so that you can use it before defining it later. The program starts at main(), but not the compiler.
A compiler reads one .cpp-file (source code) and writes one .o-file (object code).
A project/program can have multiple .cpp-files.
A linker combines object code from .o-files and libraries (.a .so .lib .dll .dyld) into executable.
Operating system loads and executes the executable (aka runs a program).
This program contains a function call on line 4:
1 2 3 4 5
#include <iostream>
int main() {
std::cout << "Hello world\n";
}
The compiler could not compile the three lines of the program into object code, if it would not have been told what the std::cout and the << are.
Since we told (on line 1) the compiler generates object code from line 4 that calls the function.
Linker connects the function call with the object code of the function that has been separately compiled into a library.
Actually speaking, if it's just one function or a group of functions that do not call each other, then you do not even need a prototype. You can just declare the functions before the main function and everything will work normally. Function prototypes are used in these scenarios only for the sake of readability, as the main focus of the program is the main function.
Yes the execution starts at the main function, but that does not account for how the compiler is reading the program, rather just the control flow through which the program's statements are executed. The compiler starts reading the program from top to bottom, like you would expect.
Problem arises when you declare a function below the calling function (which is the main function in case you have just one function other than main) and not use a prototype. The compiler reads the main function first before the defined function as the main function is above the defined function.
But then it sees a call to a function that it had not come across before. So it complains (of course we could make it so that the compiler reads ahead to find the function but that's just unnecessary if we can just use prototypes). But if the defined function was above the main function then, no probs, because the compiler had come across the function before.
In short, what a prototype does is tell the compiler that a function of so-and-so name with so-and-so arguments exists. Note that you can get away with compiling code with just a prototype and no corresponding function declaration.
To think of it, a prototype is a regular declaration of a function with no body. In fact that's what it actually is. And then when you declare the function, you're redefining the original definition of the function. Remember though that you cannot redefine functions that already had a body.
The return type and the arguments must be the same or else it won't count as a redefinition. The compiler would think that there is another overload for the function (google function overloading).