Should I have main() run all function calls?

Is there any benefit to running all function calls in the main() so that there is no actual code in the main()?

ex:

int main()
{
functionOne();
functionTwo();
}

functionOne here;
functionTwo here;




vs.


int main()
{
functionOne here;
functionTwo here;
}


Thoughts?
or is this just a personal pref?
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...

Hope that clears it out for you... :)
Huh?

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.

For example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;

#include "student.h"

typedef vector<Student> StudentVec;

bool loadStudents(const char* filepath, StudentVec& students);
bool saveStudents(const char* filepath, const StudentVec& students);
bool processStudents(StudentVec& students);
bool displayStudents(const StudentVec& students);

int main()
{
    StudentVec students;

    const char filepath[] = "students.txt";

    bool ret = loadStudents(filepath, students);

    if(ret)
    {
        processStudents(students);
        displayStudents(students);
        saveStudents(filepath,students);
    }

    return 0;
}


Andy
Last edited on
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....
Actually, I am talking about functional decomposition, which is part of proceedural programming.

And my argument holds for all non-trivial programs.

It is better to get into good practices sooner and later. A lot of beginners allow their main functions to grow way too big.

Note that I am not saying you should chop up a tiny function for the sake of it. Unless you actually want to practice using functions, of course.
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>
using namespace std;

int main()
{
    for(int i = 0; 100 > i; ++i)
    {
        cout << "Testing " << i << endl;
    }

    return 0;
}


and

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
using namespace std;

void Test(int i)
{
    cout << "Testing " << i << endl
}

int main()
{
    for(int i = 0; 100 > i; ++i)
    {
        Test(i);
    }

    return 0;
}


will have insiginifcantly different performance for the debug build, and identical for the release build as Test() will have been inlined.

Andy

PS I follow the rule that a function should fit on one page of my IDE, which is about 25 lines. Any longer and it has to be split up.
Last edited on
Topic archived. No new replies allowed.