One Function Versus Multiple Functions

◀ Precondition and Postcondition▶ Recursive Function Versus Non-recursive Function
Amazon In my junior year I wrote a program to allow chatting over the Internet between two or more computer users. In order to make everything work, I wrote many functions. The total program length is over one thousand lines. When I was debugging, I needed to scroll up and down to get to the places I wanted.

Therefore, I decided to shrink the code down by combining multiple functions into one. To my embarrassment, I found several functions that were essentially doing the same thing. I combined them and reduced the code size dramatically.

If you are writing multiple functions that are doing the same thing on arguments of different types, you should consider using function template which is described in Chapter 4.4. The point is that when one function works and is in good style and convention (low count of lines of code, etc.), why write two or more? Fewer functions also make it easier to debug.

There are times when it’s better to break a function into two functions because some place needs to call both and some other place needs to call only one of them. In this case it may be a good idea to break a function into multiple ones.

The general rule of thumb is keep the number of lines of code modest within a function. 50 lines of code is a good number; 100 tops. If you find yourself writing a huge function do yourself a favor and break them into smaller, more manageable functions.

On the other hand, when you really need many functions to make the program work you can do this trick: keep main() in one file and all other functions in other files. Here is an example.
/* “tr.cpp”: where main resides */
#include <iostream>
#include <string> #include "tr.h" using namespace std; int main() { dummy a; dummy2 b; dummy3 c; a.changeA(1); a.changeB('a'); a.changeC("joy"); b.a = 2; b.b = 'b'; b.c = "dennis"; c.a = 3; c.b = 'c'; c.c = "ethan"; cout << a.getA() << ' ' << a.getB() << ' ' << a.getC() << endl; cout << b.a << ' ' << b.b << ' ' << b.c << endl;
cout << c.a << ' ' << c.b << ' ' << c.c << endl; great(); return 0; } /* “tr.h”: where all classes and functions reside */ #include <iostream> #include <string> using namespace std; class dummy{ private: int a; char b; string c; public: dummy() {a=0; b='a'; c="abcde"; }; int getA() {return a;} char getB() {return b;} string getC() {return c;}
void changeA(int val) {a=val;} void changeB(char val) {b=val;} void changeC(string val) {c=val;} }; struct dummy2{ int a; char b; string c; }; class dummy3{ public: int a; char b; string c; }; void great(){ cout << "Jeremy is diligent and devoted to his parents." << endl; }
After you compile and run it (“g++ tr.cpp –o tr” on my machine), you will see the following output:

1 a joy
2 b dennis
3 c ethan
Jeremy is diligent and devoted to his parents.
Sometimes separating main() from other external classes or functions allows us to debug more easily. You can separate things in as many files as you want; just remember to include appropriate header files in main() file as done in the above example. Also, because tr.h uses functions that
<iostream>
and
<string>
provide, it needs to include them in the header.

◀ Precondition and Postcondition▶ Recursive Function Versus Non-recursive Function

fShare
Questions? Let me know!