In a general case, the second one is better, i.e. to have all the code in the main.. Basically, if you have all the code in functions, and call the functions from main, you're taking equal amount of memory as the original program (maybe even more), but you're decreasing the speed of your program (coz of to and fro function calls).
But if you have to perform the same job again and again, its preferable (to me at least, people have their own opinions about memory vs speed debates) to have a function.. It'll decrease your speed slightly, but it'll be memory efficient...
If you want to be speed efficient, then its better to have an inline function or a macro, which compensates on code length...
I would say that the first case is far better in the general case.
I would only put all my code in main if the app is very tiny, and even then I often factor it into a better named (than main) function out of habit.
As a beginner I would get into the habit of correctly factoring programs and forget about speed issues until you work on some or other complicated algorithm.
By correct factoing, I mean that if main is doing more that one distinct action, I would break them out into functions with names that clearly state what they do.
You are considering classes, and OOP in general for your argument! For such things, you obviously need functions, to organize and clarify your code, I don't disagree with that...
I took into account that swimba's doubt is very basic... I'm sure he's not going to be dealing with OOP anytime soon (I may be wrong, so forgive me thus), but I advised according to that... Totally agree with you as far as OOP is concerned, but not otherwise....
Hmm... But then please explain to me, what about speed efficiency? Won't the program be slower if each and every step of my main makes a call to, and gets returned to by functions?
(The thing is I tend to use functions if and only if I require that piece of code more than once, not otherwise, having learnt C++ in the past 3 months... And I would change that habit if its bad, coz its still quite early for me as a programmer)
It is generally seen as good practice to sort out the structure of a program first, and then optimize it. Obviously you don't code deliberately inefficient code, but you don't try to out to the optimizer, either.
Modern compilers will optimise out small functions, automatically inlining them, so you don't have to worry about splitting a function up into smaller functions if it helps you understand what's going on better. Even if the functions are used just the once in the main function.
And compared to what else is going on, the code of a function call is pretty small in most programs. For example
1 2 3 4 5 6 7 8 9 10 11 12
#include <iostream>
usingnamespace std;
int main()
{
for(int i = 0; 100 > i; ++i)
{
cout << "Testing " << i << endl;
}
return 0;
}