Why can't a function go after Main
You can, but you have to declare it beforehand:
void myFunction(); // declaration
int main()
{
myFunction();
}
void myFunction(){} // definition
Note that a function needs a return type. If the function does not return anything, that type must be void
.
You cannot use a name/symbol which is not yet declared. That is the whole reason.
It is like this:
i = 10; //i not yet declared
int i;
That is wrong too, exactly for the same reason. The compiler doesn't know what i
is – it doesn't really care what it will be.
Just like you write this (which also makes sense to you as well as the compiler):
int i; //declaration (and definition too!)
i = 10; //use
you've to write this:
void myFunction(); //declaration!
int main()
{
myFunction() //use
}
void myFunction(){} //definition
Hope that helps.