The best programs are written so that computing machines can perform them quickly and so that human beings can understand them clearly. A programmer is ideally an essayist who works with traditional aesthetic and literary forms as
well as mathematical concepts, to communicate the way that an algorithm works and to convince a reader that the results will be correct. Donald E. Knuth

C++ Functions

A function is a block of statements which perform a specific task. A program can be divided into functions. This enhances the modularity and maintainability of a program.

Function Declaration and Definition
Every C++ program has at least one function i.e main() where the program execution begins. Consider the following C++ program :

#include<iostream>
using namespace std;

int add(int, int); // function declaration or prototype

int main() {
   int x = 5, y = 3;
   int s = add(x, y); // function call
   cout << "Sum :: " << s << endl;
   return 0;
}

// function definition
int add(int a, int b) {
   int sum = a + b;
   return sum;
}

Function Declaration / Prototype describes the function interface to the compiler giving information about the function name, return types and parameters (arguments). In the above program,
Function Name : add
Return Type : int
Parameters / Arguments : int a, int b

Function Definition provides the code which constitutes the body of the function. A general form of C++ function definition is shown below :

return_type function_name ( argument list ) {
            function body
}

Function Call invokes the function body to perform a specific task.

Please note that argument list may consists of 0 or more no. of arguments and the return type of the function may be void also in case the function doesn't return any value. Following program uses two functions to generate first 'n' terms of fibonacci series. In fibonacci series, ith term of the series is equal to the sum of (i - 1)th and
(i - 2)th term with first two terms as 0 and 1.
Fibonacci series : 0 1 1 2 3 5 8 13 21 ....

/*
 * This program prints the first 'n' terms of fibonacci series
 * let the series be fib = { 0, 1, 1, 2, 3, 5, 8, 13, 21, ... }
 * fib(0) = 0 and fib(1) = 1
 * fib(i) = fib(i-1) + fib(i-2) for i >= 2 

 */

#include<iostream>
using namespace std;

/***************** function prototypes ******************/

int sum ( int, int );
void fibonacci ( int );

/***************** function definitions *****************/

/* Adds two numbers */
int sum ( int a, int b ) {
   int s = a + b;
   return s;
}

/* Prints first 'n' terms of fibonacci series */
void fibonacci ( int n ) {
   int a = 0, b = 1;
   int s = 0, i;
   cout << a << " " << b << " ";
   for ( i = 2; i < n; i++ ) {
      s = sum(a, b); // call to function sum()

      cout << s << " ";
      a = b;
      b = s;
   }
}

/********************************************************/

/* main function */
int main () {
   int n = 7;
   cout << "Fibonacci Series :- " << endl;
   fibonacci(n); // call to function fibonacci()
   return 0;
}


Another sample program which finds the maximum of three numbers using a function max (int a, int b) which finds the maximum of two numbers.

/*
 * This program finds the maximum of three numbers
 */

#include<iostream>
using namespace std;

int max ( int, int );
int maxThreeNum ( int, int, int );

/* Finds max of two numbers */
int max ( int a, int b ) {
   // find max using conditional operator
   int large = ( a > b ) ? a : b;
   return large;
}

/* Finds max of three numbers */
int maxThreeNum ( int a, int b, int c ) {
   int x;
   x = max(a, b); // store the max of 'a' and 'b' in 'x'
   x = max(c, x); // store the max of 'x' and 'c' in 'x' 
   return x;
}

/* main function */
int main() {
   int x = 6, y = 9, z = 3;
   int max_val = maxThreeNum(x, y, z);
   cout << "Maximum Value : " << max_val << endl;
   return 0;
}


Parameter Passing
We have seen above that while calling a function we pass parameters/arguments.
There are two ways of passing parameters :
Call by Value : This method copies the argument value into the formal parameters of the function. Apparently, all the programs we have seen till now uses call by value. In this method, changes made to the argument values in the called function is not reflected in the calling function. Consider the program which finds the maximum value out of three numbers. In the function call maxThreeNum(x, y, z) , values in x, y and z are copied into variables a, b and c respectively.
Call by Reference :
This method copies the address of the arguments into the formal parameters of the function. In this method, changes made to the argument values in the called function is reflected in the calling function.
Following program illustrates the difference between call by value and call by reference :

/*
 * This program distinguishes the behaviours of call-by-value and
 * call by reference parameters.
 */

#include<iostream>
using namespace std;

/* param 'a' is passed by using call-by-value
   param 'b' is passed by using call-by-reference
*/
void changeValues(int a, int &b) {
   a = a * 10; // new value of 'a' is not reflected in main()
   b = b * 10; // new value of 'b' is reflected in main();
}

int main() {
   int a = 2, b = 5;
   cout << "Before calling changeValues() : " << endl;
   cout << " a : " << a << "  b : " << b << endl;
   changeValues(a, b); // calling changeValues()
   cout << "After calling changeValues() : " << endl;
   cout << " a : " << a << "  b : " << b << endl;
   return 0;
}

A typical program in which call by reference is used is swapping of two numbers :

/*
 * Swapping of two numbers
 */

#include<iostream>
using namespace std;

// swap two numbers
void swap(int &x, int &y) {
   int temp;
   temp = x;
   x = y;
   y = temp;
}

int main() {
   int a = 5, b = 2;
   cout << "Before Swapping :-" << endl;
   cout << "a : " << a << "  b : " << b << endl;
   swap(a, b);
   cout << "After Swapping :-" << endl;
   cout << "a : " << a << "  b : " << b << endl;
   return 0;
}


Default values for arguments
We can define default values for end arguments in the function definition. If the corresponding values are not passed during function call, default values for those arguments are used. Following program illustrates this :

#include<iostream>
using namespace std;

/* param 'b' has default value of 10 */
int function ( int a, int b = 10 ) {
   return (a * b);
}

int main() {
   int a = 5, b = 6;
   int prod;
   cout << "Passing both arguments 'a' and 'b' :-" << endl;
   prod = function(a, b); // calling function() with two params
   cout << "Product : " << prod << endl;
   cout << "Passing one argument only i.e 'a' :-" << endl;
   prod = function(a); // calling function() with one param
   cout << "Product : " << prod << endl;
   return 0;
}


Function Overloading
C++ provides a feature to define two or more functions with the same name but different argument list. This is known as function overloading. Following program clarifies this concept :

/*
 * Demonstration of Function Overloading
 */
#include<iostream>
using namespace std;

// two functions with same name but different parameter list
int add ( int, int );
int add ( int, int, int );

// adds two numbers
int add ( int a, int b ) {
   return (a + b);
}

// adds three numbers
int add ( int a, int b, int c ) {
   return (a + b + c);
}

int main() {
   int x = add (5, 4);
   int y = add (2, 6, 11);
   cout << x << " " << y << endl;
   return 0;
}

Back | Next