-
Declaring Functions
After reading the FAQ and the latest post about void main() and int main() I looked at some sample code that came with my compiler.
One thing I noticed is that they declared all the functions before the main(). IE.
Code:
int function1(var 1, var2);
void function2(void);
etc....
int main()
{
blah blah blah
}
My question is this. What is the point of declaring them before main() and what effect does it have?
I'm using Borland TurboC ("older then dirt" version)
It never required me to use void or int before a function so I'm trying to learn that part so I can change my habit.
-
It doesn't matter where you put the function definition, but if you put it after it's called and with out a prototype, your compiler will complain because it can't type check the function call. So, just either use function prototypes before you use the function and put the definition later ( below main ) or don't use prototypes and define the function before you use it.
-
Generally speaking it's good practice to declare your prototypes before main. Why? Because it's easier to add/remove a prototype, and you can easily see what functions you have included without having to read through the whole program.
-
>It never required me to use void or int before a function so I'm trying to learn that part so I can change my habit.<
well it probably defaulted to void or int, what you use depends on what you wan't to return.