My Blog
Articles related to programming, computer science, technology and research.

21/08/2016 Categories: C++ Programming. No Comments on Functions in C++



While writing large programs, main() function will become quite complex to maintain and soon you will lose track of what is happening. This is where functions will aid programmers.


Functions allows the programmer to divide a large program into logical chunks. Each function is a self contained block that does a non-trivial task. The main() function will call other functions which in turn solves the problem. This model of programming using functions is known as modular programming or structured programming.


Consider the following program which calculates the area and perimeter of a circle using functions.


In the above program, following two lines are known as function prototypes:

double area(double);

double peri(double);


A function prototype tells the compiler that there is a function with the specified name, with the specified type and parameters.


Following two lines are function calls to respective functions:




Following two code segments are called function definitions of respective functions:


Function Prototype


A function prototype tells the compiler what is the function name, how many parameters a function accepts and the types of those parameters and the type of value that a function returns to its caller. General syntax of function prototype (declaration) is as follows:

return-type¬† function-name(type, type, …, type);


In the above syntax, parameters are optional. When the function does not return any value, the return type must be specified as void.



Function Definition


The body or implementation of a function is known as function definition. A function definition always consists of a block made up of braces. General syntax of a function definition is as follows:


If the return type of the function is void, there is no need to write the return statement. Remember that a function can return only one value using the return statement.


Function Call


Invoking or calling a function is known as a function call. General syntax of a function call is as follows:

function-name(arg1, arg2, …, argN);


A function call starts the execution of corresponding function definition. The arguments in the function call are known as actual parameters and the parameters in the function definition are known as formal parameters.


Function parameters (in general) are like local variables. Their scope is within the function block. Each time a function executes, memory is allocated for parameters and when execution goes back to the caller, memory is deallocated and they are no longer accessible.


Function Parameters with Default Values


The parameters in a function declaration can be assigned a default value as shown below:

double area(double radius, double Pi = 3.14);


We can declare multiple parameters with default values. But, all suck kind of parameters must be at the last of the parameters list. We can assign our custom value for the parameter as shown below:

area(2.5, 3.1415);


For the above function call, Pi value will be taken as 3.1415 instead of the default value 3.14.


Following program demonstrates parameters with default values:




A function invoking itself is known as recursion. Recursion is an alternative to iteration. Recursion is very close to mathematical way of solving a given problem.


Advantages of recursion are, it is straight forward and easy. Disadvantage of recursion is, it occupies more memory on the stack when compared to iteration.


Note: Remember to write an exit condition while using recursion. If there is no exit condition, then the function will call itself infinitely.


Consider the following program which calculates the factorial of a number using recursion:


In the above program the exit condition is if(n==0 || n==1) return 1;


The recursive call is fact(n-1).


Note: In the above program there is no function declaration (prototype) for the function fact(). If the function definition is written before main(), there is no need of writing function declaration.


Passing an Array as a Parameter


Like we are able to pass variables, we can also pass arrays as arguments to a function. A function prototype which accepts an array and its size as parameters is as follows:

void PrintArray(int nums[ ], int size);


Consider the following program which contains a function that takes an array and its size as arguments and calculates the largest element in the array:


Note that in the above program there is no need to write square brackets in the function call largest(nums, 5).


Parameter Passing Techniques


In C++ we have three ways for passing arguments to a function. They are: pass-by-value, pass-by-reference, and pass-by-address.



Generally used way to pass arguments is pass-by-value. In this method, the arguments passed in the function call are copied to formal parameters in the function definition. So, any changes made to the formal parameters are not reflected on the actual parameters. Consider the following swap function which demonstrates pass-by-value:


Let’s assume that in the main() function we are writing the following code:

int a = 10, b = 20;

swap(a, b);


Although the values of x and y are being interchanged, since they are copies of a and b, the values of a and b remains the same i.e., 10 and 20 respectively.



To make the changes on formal parameters reflect on actual parameters, we can use pass-by-reference. In pass-by-reference method we pass reference to argument instead of a copy. So, both actual and formal parameters refer to the same memory location. Using this method, our swap function will be as follows:


Remember to precede the formal parameters with &.


In pass-by-address, we pass address of the argument to formal parameters. Both pass-by-reference and pass-by-address are semantically same. The only difference is, in pass-by-reference, the formal parameters are guaranteed to alias valid memory locations. Whereas in pass-by-address, the formal parameters are pointers and can even point to null.

Our swap function using pass-by-address will look as follows:


and the code for calling our swap function will be as follows:

int a=10, b=20;

swap(&a, &b);

The complete program which demonstrates all the three methods to pass arguments is as follows:


Inline Functions


A normal function call would involve stack operations and processor to shift to the first instruction in the function definition which might take significant time. For small functions with one or two lines this time is unnecessary. Such functions can be declared as inline functions using the keyword inline.


In general, inline functions are faster than normal functions. When the compiler comes across a call to a inline function, it directly substitutes the function definition in the place of function which might increase the size of code. So, the use of inline keyword in programs should be minimal. Consider the following inline function that demonstrates the use of inline keyword:


Rules for declaring a function as inline are as follows:

  1. Function should not use recursion.
  2. Function should not use static variables.
  3. Function should not return a value.
  4. Function should not contain any go to, switch, or iterative statements.


Function Overloading


Two or more functions with the same name but different set of parameters or different number of parameters are said to be overloaded and this concept is known as function overloading.


It is common to create functions which does the same thing but on different parameters. For example, we might have to create functions to add integers as well as floating-point numbers. In C, to do this, we have to create two functions with different names. But in C++ we can use the same function name to create multiple functions.


Consider the following program which demonstrates the use of function overloading:


Suryateja Pericherla

Suryateja Pericherla

Hello, I am Suryateja Pericherla working as an Asst. Professor in CSE department at Vishnu Institute of Technology. I write articles to share my knowledge and make people knowledgeable regarding certain topics.
Suryateja Pericherla

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll Up
Thank you for visiting! If you like this website do share with your friends by clicking on the social icons available on the left hand side.

As the domain name is quite large to remember, use this short domain to visit this site. Short domain: