C++ : Functions

Table of Contents


We know that function play an important role in C program development.Dividing a program into function is one of the major principles of top-down, structured programming.Another advantages of using functions is that it is possible to reduce the size of a program by calling and using them at different plcaes in the program.
Recall that we have used a syntax similar to the following in developing C programs.


void show();          // Function Declaration

show();             // Function Call

void show()        // Function Defination

…                      // Function Body




When the function is called, control is transferred to the first statement in the function body.The other statements in the function body are then executed and control returns to the main program when the closing braces is encountered.C++ is no exception.Functions continue to be the building blocks of C++ programs.In fact, C++ has added many new features to functions to make them more reliable and flexible. Like C++ operators,a C++ functions can be overloaded to make it perform different tasks depending on the arguments passed to it.

Main Function

C does not specify any return type for main() function which is the starting point of execution of a program
The definition of main() would look likes this:


// program statement


This is perfectly valid because main function in C does not return any value.
In C++ main return value of type int to the operating system.C++ therefore explixitly defines main() as matching one of the following prototypes:

int main();
int main(int argc, char *argv[ ]);

The functions that have a return value should use the return statement for termination.
The main function in C++ defined as folows:


int main()

return 0;


Since the return type of functions is int by default, the keyword int in the main() header is optional.
Many operating systems test the return value (called exit value) to determine if there is any problem.The normal convention is that an exit value of zero means the program ran successfully, while a non-zero means there is problem. The explicit use of a return (0) statement will indicate that the program is successfully executed.

Function Prototyping

Function prototyping is one of the major improvements added to C++ functions.The prototype describes the function interface to the compiler by giving the details such as teh number and type of arguments and type of return values.

Function prototype is a declaration statement in the calling program and is of the following form:

type function-name (argument-list);

The argument list contains the types and names of arguments that must be passed to the function.Example,

float volume(int x, float y, float z);

Note that each argument variable must be declared independently inside the parentheses.That is a combined declaration like,

float volume(int x, float y, z);

is illegal.
In a function declaration, the names of the arguments are dummy variables and therefore, they are optional.That is the form

float volume(int, float, floatz);

is acceptable at the place of declaration.At this stage, the compiler only checks for the type of arguments when the function is called.

Call by Reference

In traditional C, a function call passes arguments by value.The called function creates a new set of variables and copies the value of the arguments into them. The function does not have access to the actual variables in the calling program and can only work on the copies of values.This mechanism is fine when function does not need to alter the values of the original variables in the calling program.But, there may arise situation where we would like to change the values of the variables in the calling program.

Reference variables in C++ permits us to pass parameters to the functions by reference.When we pass arguments by reference, the formal argument in the called function become an aliases to the actual arguments in the calling funtion. This means that whenthe function is working with its own arguments, it is actually working on the original data.
Consider the following function


void swap(int &a, int &b)      // a and b are reference variables
int t = a;
a = b;
b = t;


Now, if m and n are two integer variables, then the function call


will exchange the values of m and n using their aliases a and b.

Return by Reference

A function can also return a reference.Consider the following function:


int& max(int &x, int &y)
if(x > y)
return x;
return y;


Since the return type of max() is int& ,the function return reference to x or y.(and not the values).This function call such as max(a,b) will yeild a reference to either a or b depending on their values. This means that this function call can appear on the left-hand side of an assignment statement.That is, the statement

max(a,b) = -1;

is legal and assign -1 to a if it is larger, otherwise -1 to b.


In C, it is possible for the functions to call themselves.A function is called ‘recursive’ if a statement within the body of a function calls the same function.Sometimes called ‘circular defnition’, recursion is thus process of defining something in terms of itself.
A simple program which demonstrates the use of recursion function:


/* Calculate the factorial value */
int fact, a;
printf(“Enter any number”);
scanf(“%d”,(ampersand symbol)a);

// Function call
fact = rec(a);

printf(“Factorial value = %d”,fact);

int rec(int x)
int f;
if(x == 1
return (1);
//Function calling itself
f = x * rec(x-1);

return (f);

Enter any number 1
Factorial value = 1
Enter any number 2
Factorial value = 2
Enter any number 3
Factorial value = 6



Leave a Comment