The main() function knows only that it is calling the function called add(), and then outputting the value returned by that function.
The reason for the prototype declaration is so the compiler can check how many and what type of parameters the function requires, and what type of value it returns. On line 3, the function declaration says add() takes two int parameters. Hence on line 8, the compiler will be satisfied, as there are indeed two parameters (3 and 4) which are integers.
The compiler doesn't at that stage know or care what happens inside that function, it just sees that it will return an integer, so generates code so that cout can output an integer.
When the compiler reaches line 12, it can generate the code for the function body. But it is not until the next step, the linker, that the code is all joined together.
For example this code is
not correct, but it will still compile:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
|
#include <iostream>
int add(int x, int y); // forward declaration of add() using a function prototype
int main()
{
using namespace std;
cout << "The sum of 3 and 4 is: " << add(3, 4) << endl;
return 0;
}
int add(int x, int y, int z) // deliberate error here
{
return x + y + z;
}
|
Here, there is a problem when the linker tries to put everything together, as it can't find the proper version of add().
As you can see, it's important to distinguish between the job of the
compiler, and that of the
linker.