also, at the very start of the code after includes etc people define a "shell" of their functions
It's like when you use a variable in your program. In C++, this code will generate an error:
Code:
int main()
{
num = 10;
return 0;
}
The compiler has to encounter num's declaration before num can be used in the program:
int num;
num = 10;
It works similarly with functions. This will generate an error:
Code:
int main()
{
myFunc();
return 0;
}
so you have to do this:
Code:
#include <iostream>
using namespace std;
void myFunc()
{
cout<<"hello"<<endl;
}
int main()
{
myFunc();
return 0;
}
However, you can do this as well:
Code:
#include <iostream>
using namespace std;
void myFunc();
int main()
{
myFunc();
return 0;
}
The line:
void myFunc();
is a promise to an old, grumpy fellow named Mr. Compiler that you will define the function elsewhere. You can define it in the same file or in another file. After Mr. Compiler gets done examining your program, since Mr. Compiler doesn't get around very well anymore, he hands the program over to his friend Mr. Linker. Mr. Linker's job is to make sure you keep the promises you made to Mr. Compiler. So, Mr. Linker goes hunting around all the files in your program looking for a definition of the function myFunc(). If you didn't keep your promise and define the function somewhere in your files, you will get an error.
when a function is called, does the code automatically keep looking down the code until it finds the first defined version of the function (if not defined already)?
In C++, there is no "first defined version". In all your files, there can only be one function that matches. If there are two functions that match the function call, then you will get an error. The same is true for variables: you can't define them more than once. This will produce an error:
int num = 10;
...
...
int num = 20;