Not all of your source code will be in the same file.
If this code:
- int add(int i, int j)
-
{
-
return i+j;
-
}
is in a separate file then how does the compiler know that this is correct:
- int main()
-
{
-
printf("%d\n",add(3,3));
-
}
?
The answer is that you tell the compiler that there is a function named
addthat takes two int arguments and returns an int. You do this using a prototype:
The compiler takes your word for that
and marks "int add(int,int)" as an unresolved external reference. The linker will locate the function and fix up the addresses so that the call works.
Note that your function prototypes are exactly the first line of the function. The function body between the {} is replaced with a semi-colon:
Alos, try moving the code to the end of the file with main() and then compile with and without your prototype. See what happens.