Have a look at:
http://www.cplusplus.com/articles/yAqpX9L8/
Imagine your program will load. The computer starts at the top of your file and tries to find the main function.
At some point the main function is found and in the main function you are calling another funtion, lets say "void printHelloWorld()".
Now there are two possibilities:
- The computer can do whatever is in printHelloWorld() because it already found this function when it was looking for the main function.
This happens if you structured your file like this:
1 2 3 4 5 6
|
void printHelloWorld()
{
// some code
}
int main
|
- The computer can not do whatever is in printHelloWorld(), because it doesn't know where this function is. In this case your program will not build.
This happens if you structured your file like this:
1 2 3 4 5 6 7 8 9 10
|
int main()
{
printHelloWorld();
return 0;
}
void printHelloWorld()
{
// some code
}
|
The solution in the later situation is to add a function prototype. The prototype tells the computer that "this file contains a function void printHelloWorld(), so even if you didn't find it yet when it is called for the first time, it is in this file and therefor you can find it".
1 2 3 4 5 6 7 8 9 10 11 12
|
void printHelloWorld();
int main()
{
printHelloWorld();
return 0;
}
void printHelloWorld()
{
// some code
}
|
So basically you need to have a function prototype if the function code is lower in your source code than the place where the function is called from.