You are on page 1of 4

Function Overloading:

Function Overloading refers the use of same function name to perform a variety of
different tasks. In function overloading concept, a number of functions are made with a
one function name but with different argument lists (may be different number of
arguments or different type of arguments). The function would perform different
operations depending on the argument list in the function call. The correct function is
determined by either number of arguments and type of the arguments.
Examples:
a) Different number of arguments
#include<iostream.h>
#include<conio.h>
int add(int, int);
int add(int,int,int);
void main()
{
int r1,r2;
clrscr();
r1=add(10,30); // function add() is called which has only two arguments
r2=add(20,30,40); //function add() is called which has three arguments
cout<<"Result1="<<r1<<endl<<"Result2="<<r2;
getch();
}

int add(int a,int b)


{ return(a+b); }

int add(int a, int b, int c)


{ return(a+b+c); }

In this example, there are two user-defined function named add() but they have been used
for different purposes. One has been used to add two integer number and other is to add
three integer numbers. The exact function is called according to number of arguments
passed while calling the function. In first call (i.e. add(10,30)), only two arguments have
been passed. Thus first prototype (i.e. int add(int, int)) is used which adds two numbers.
In second function call, three numbers have been passed. This it uses second function
prototypes.

b) Different Kind of arguments:


#include<iostream.h>
#include<conio.h>
int absolute(int x);
long absolute(long y);
void main()
{
int num,r1;
long lnum,r2;
clrscr();
cout<<"Enter an integer -ve or +ve"<<endl;
cin>>num;
cout<<"Enter a long number -ve or +ve"<<endl;
cin>>lnum;
r1=absolute(num);
r2=absolute(lnum);
cout<<endl<<"Absolte Value for int="<<r1<<endl<<"Absolute Value for long="<<r2;
getch();
}

int absolute(int a)
{ cout<<"I am from funtion 1"<<endl;
return(a>0?a:a*-1); }

long absolute(long a)
{ cout<<endl<<"I am from function 2"<<endl;
return(a>0?a:a*-1); }

Output:
Enter an integer -ve or +ve -34
Enter a long number -ve or +ve -120000
I am from funtion 1
I am from function 2
Absolte Value for int=34
Absolute Value for long=120000

In this example, same function absolute() is used to calculate absolute value of both
integer and long integer. The appropriate function is called according to type of
arguments passed to it. If integer is passed as actual argument, then first function absolute
is used which calculates absolute value of integer. Similarly, if long number is passed,
another absolute function is called which calculates absolute value of long integer. This is
illustrated as output.

Default arguments:
C++ allows to use default argument if function call doesn’t pass its all arguments. When
a function is called without specifying all its arguments, the c++ function assigns a
default value to the parameter which does not have a matching argument in the function
call. The default values are specified in function prototypes.
An example:
#include<iostream.h>
#include<conio.h>
float interest(float,float,float=20);
void main()
{
clrscr();
float interst_result, interst_result_default;
interst_result=interest(1000,2,30);
interst_result_default=interest(1000,2);
cout<<"Interest with rate 30% is "<<interst_result<<endl<<"Intrest with default rate 20%
"<<interst_result_default;
getch();
}

float interest(float p, float t, float r)


{
return((p*t*r)/100);
}

Output:
Interest with rate 30% is 600
Intrest with default rate 20% 400

In this example, default value for parameter rate is specified in the prototype
of interst() function. When this function is called by specifying all arguments like
interest(1000,2,30) (i.e. all three arguments p,t and r), the default value is not used. The
supplied values are used to calculate the interest. But when the function is called by
specifying only two arguments like interest(1000,2), the default value for rate is used to
calculate the interest.

While specifying default arguments, default values can be specified only for trailing
arguments. The default values are added from right to left in the function prototype. The
default value can not be provided to a particular argument in the middle of an argument
list. This is illustrated by following some function prototypes.

float interest(float p, float t, float r=20); //valid


float interest(float p, float t =2, float r); //invalid
float interest(float p, float t=2, float r=20); //valid
float interest(float p=1000, float t, float r); //invalid

Uses of default arguments:


Default arguments are useful in such situations where some arguments almost always
have the same value. For an example: bank interest may remain the same for all
customers for a particular period of deposit.
The default arguments can be used to add new parameters to the existing
functions. This increases the capability of a function by adding another argument. Using
default arguments means that the existing function call can continue to use the old
number of arguments, while new function calls can use more.

Inline functions
When a same task is to done for many times, the function is used to save memory
space. But, the use of function not only saves memory space but also adds extra
overheads. One function call has to pass many steps like jumping to the function, saving
registers, pushing arguments into the stack and returning to the calling function. A lot of
time is taken by these instructions. When function is small, a substantial percentage of
execution time may be spent in such overheads. In the case of small functions, the benefit
obtained by saving memory is less important or meaningful in comparison to extra over
heads during function call. In this situation, one may urge that the use of function is
disadvantageous. They may say that instead of using function, using duplication of code
to perform same task repeatedly is more advantageous. But duplication of code makes
program unorganized and difficult to read.
This problem is eliminated by inline function in C++. An inline function is a
function that is expanded in line when it is invoked. The compiler replaces the function
call with the corresponding function code. A function is made inline by adding prefix
inline to the function definition. The syntax is
Inline return_type function_name(argument_list)
{
}
An example:
#include<iostream.h>
inline float interst(float p, float t, float r)
{ return ((p*t*r)/100); }

void main()
{
float result;
result=interst(12000,2,10);
cout<<"The interest is "<<result;
}
In this example, the function interst is defined as inline.
Defining inline function is meaningful only for small functions. For large
functions, the function call becomes small compared to the execution of the function and
the benefits of the inline functions may be lost. In such cases, the use of normal functions
will be more meaningful. Usually, the functions are made inline when they are small
enough to be defined in one or more lines. The inline keyword merely sends a request,
not a command, to the compiler. Thus, compiler may ignore this request if the function
definition is too long or too complicated and compile the function as a normal function.
Although, inline expansion makes a program run faster because the overhead of a
function call and return is eliminated, it makes the program to take up more memory
because the statements that define the inline function are reproduced at each point where
the function is called. So, a trade off becomes necessary.

When inline function may not work?


¾ For large functions
¾ For functions containing static variables
¾ For recursive functions
¾ For function containing a loop, a switch or a goto.

You might also like