Is there any benefit, or even difference in having the main() function at the bottom of the code, instead of the top.
I also notice if main() is at the bottom, the functions dont need to be declared. Why is this?
Thanks
Is there any benefit, or even difference in having the main() function at the bottom of the code, instead of the top.
I also notice if main() is at the bottom, the functions dont need to be declared. Why is this?
Thanks
Nope, there's no difference where main() is in your program, just so long as it's there.
The reason why you don't need prototypes if it's at the bottom is because the compiler reads the source file from the top to the bottom, defining symbols as it goes along. Therefore, by the time it gets to main(), it already knows about all the functions you're gonna call.
Handy.
THanks.
well, in C you dont have to declare a function. in C++ you have to..if you try to compile the program in C when the function is defined after main() without giving prototypes, you might get warnings, but the program will work, but there might be issues with the return type of the function. the dev compiler for example will take int to be the default return type if there is no declaration of the function prototype before calling the function..so its always a good practise to give the prototype first.I also notice if main() is at the bottom, the functions dont need to be declared.
as far as main being at the bottom of the program, it depends on how you like it, whether you want to put main() at the start or at the end..i usually prefer main() to be at the bottom with least amount of code inside main() itself..let all the other functions do the job.
and yes, the compiler goes from top to bottom as SMurf said. So , it wont matter where the main() is, so long as it is there.. if its there, the compiler will find it for you
Code:>+++++++++[<++++++++>-]<.>+++++++[<++++>-]<+.+++++++..+++.[-]>++++++++[<++++>-] <.>+++++++++++[<++++++++>-]<-.--------.+++.------.--------.[-]>++++++++[<++++>- ]<+.[-]++++++++++.
No. This is wrong. Just because your compiler does the linking for you, doesn't mean this is allowed behavior. All functions need to be 'visible' before you use them. This means either the function is declared in scope before you use it, or it has an available prototype in scope before you do so. Doing anything else is wrong, whether your compiler skirts around it or not.
As to main, it doesn't matter where it's declared. It can be in its own file if you like, so long as the above rules are followed.
Quzah.
Hope is the first step on the road to disappointment.