WHEN AND WHY FUNCTIONS ARE USED?

In every complicated program there are sequence of commands that describes the procedure which are the same for sequences of input values. Until now, if we wanted to repeat the procedure with different parameters, we had to copy all commands on every place that this procedure is used. The disadvantage of that procedure is obvious: if we discover the error in the procedure, than we have to input the correction on every single place where the same procedure is used.
C++ offers the solution to this problem. Instead repeating procedure in code structure the specific procedure is placed inside of Function. When the procedure is placed in function the function is called on a place where is needed. Functions are written as a separate blocks of commands before and after function main (). Calling of a function is done in this way so the execution of the main code is currently terminated and the thread of execution is transferred to a function. After the code inside the function is executed, main program continues from the next command after the function calling. As you can see in next picture. If we look the structure of the code from outside we can see that the function is acting like a separate part of the code whose internal structure does not have to be known to the user of a function. The caller has to know what certain function does, but did not really care how it’s done. Functions have parameters and arguments which caller need to be specified when called and they bring back return value to the caller. So in this way they inform him about results of their work.

Functions can be called from another function (for example the function number Three can be called from function number two), and even from itself. Every functions can be called infinite number of times.

So if we brake a complex (long) code into the functions, we contribute to the modularity of the program – the program is broken down to smaller pieces which are easier to use. The code also becomes readable and understandable. Let’s illustrate the problem with a very simple example – calculation of binomial coefficients:
#include "stdafx.h"
#include "iostream"
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
       int i, p, r;
       cout << "p = ";
       cin >> p;
       cout << "r = ";
       cin >> r;
       long nominator = 1;
       for (i = p; i > 1; i--){
             nominator *= i;
             cout << nominator << endl;
       };
       cout << "The value of nominator is: " << nominator << endl;
       long denominator1 = 1;
       for (i = r; i > 1; i--){
             denominator1 *= i;
             cout << denominator1 << endl;
       };
       cout << "The value of denominator1 is: " << denominator1 << endl;
       long denominator2 = 1;
       for (i = p - r; i > 1; i--){
             denominator2 *= i;
             cout << denominator2 << endl;
       };
       cout << "The value of denominator2 is: " << denominator2 <<endl;
       cout << "The solution of calculation is: "<< nominator / (denominator1*denominator2) << endl;
       system("pause");
       return 0;
}
From this code it’s obvious that we’ve used three very similar for loops which calculates factorials in the nominator and denominator. So let’s assume that we have defined the function called factorial and we’re just using it in the code. Then the code would look like this:
#include "stdafx.h"
#include "iostream"
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
       long p, r;
       cout << "p =" << endl;
       cin >> p;
       cout << "r =" << endl;
       cin >> r;
       cout << factorial(p) / factorial(r) / factorial(p - r) << endl;
       system("pause");
       return 0;
}


This code is much easier and understandable then previous one. Moreover, the functions which we write can be stored in libraries and later they can be included in programs that we are currently working on, without the need to write the code or copy the specific code. Our function factorial() can be stored into the library that we can call math. If we want we that our program calculates the factorials we only need to include the library into our project. 
Share on Google Plus