Function Prototypes

I'm learning about function prototypes and was given this example code.

 ``123456789101112131415`` ``````#include 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) { return x + y; }``````

It says the main function would output "7"
I don't understand how the main function knows to add and return 3 + 4 when its declared after the main function ends.
I was told that was the point of forward declaration.

It isn't declared after the main program ends or begins. It is declared as soon as the the program reaches the `add(3,4)`.

At least, that was how it was taught to me. Maybe someone with a greater skill in these can help you.
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:
 ``123456789101112131415`` ``````#include 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.
@ Chervil: Good job explaining. Taught me some stuff too.
@ Xecutive: There you go. Thanks for the question. Learned some stuff.
Let me just check to make sure I understand.

At line 8 cout just wants to know that 3&4 are ints.
When its compiled it will sort out the maths later when it gets to line 12.

?
At this point in the code, the compiler already knows that in add(3, 4) these two values are ints because you've already declared these arguments would be ints on line 3:

`int add(int x, int y); // forward declaration of add() using a function prototype `
@ Xecutive

That's more or less right, but a bit oversimplified. It can be tricky to fully understand until you've had a lot of practice.

The thing is, when the compiler gets to the function definition at line 12, it doesn't particularly recognise that this function has any connection with what has gone before. It merely checks that lines 12 through 15 are valid, and generates the appropriate code for that function.

The linker which I've mentioned is a separate process, and it's that which ties the function at line 12 to the call on line 8.

Apologies if this is rather too much information to take in at this stage.
Last edited on
Topic archived. No new replies allowed.