If you like my posts please leave a comment.

Saturday, October 20, 2012

Function Overloading



Function overloading;

is a special capability of functions in C++; which enables you to create multiple functions with the same name. Unlike C, where every function in a program should have unique names. Since unique name is required for different functions, there you will have the need to create different functions, even if the actions to be performed by them are very similar. Thus at a time this becomes annoying to a programmer. 
            In C++ we can overcome this situation by creating different functions with same name. This is called function overloading.

            Now the question that should arise in our mind is, “If all the functions are having the same name then, how the compiler is able to match a particular function call with its corresponding function definition?  Answer is – it is able to distinguish among the different function definitions from the type or number or order of arguments being passed during the function call.
Note:
  1. Function argument list is also known as function signature, which is a key to function overloading.
  2. Return types of the functions are not taken in account by the compiler to differentiate between the functions
If two functions are having same number and types of arguments in the same order, they are said to have the same signature. Even if they are using different variables names, their signatures will considered as same. Example –
void sqr(int a, float x); //function 1
void sqr(int p, float q); //function 2
Above two functions having same signature.

Finding the Best Match.

            I think, by now you have come to know that a particular instance of the function is resolved by the compiler through argument matching (process of disambiguation). Argument matching involves comparing actual argument of the function call with the formal arguments of each function definition. There are three possible results of a function call:
  1. An exact match is found.
  2. No match is found
  3. Ambiguous match
Compiler always tries to find the best possible match for the function call. In order to find the best possible match, the compiler follows certain rules, they are as follows:
  1. Search for Exact Match: If the type of the actual argument exactly matches the type of one defined instance, the compiler invokes that particular instance. Example –
void fun1(int); //overloaded functions
void fun1(double);
fun1(5); //exact match is found fun1(int);
  1. A match through integral promotion: If no exact match is found, then compiler attempts to achieve a match through promotion of the actual argument. Example – conversion of integer data types char, short, int to int or into unsigned int if values cannot be represent by int is called integral promotion. For example see the code given below:
void fun1(int); // overloaded functions
void fun1(double);
fun1(‘z’); //match through integral promotion is found with void fun1(int);
  1. A match through standard C++ conversion rules:
If no exact match or match through promotion is not found then attempt is made to find a match through the application of standard C++ conversion of actual arguments. (Data type conversion rules you have already learnt in my previous post “Data type Conversions”). Now let’s see the following example –
void fun1(char); //overloaded functions
void fun1(double);
fun1(222); // match through standard conversion match is found with fun1(double)
  1. A match through user-defined conversions:
If all the above mentioned steps get failed, then the compiler will try the use-defined conversions in combination with integral promotions and built-in conversions to find a unique match.

Function overloading should be used judiciously. Its purpose should be to provide a common name for similar but slightly divergent functions. It is a bad programming habit if you implement function overloading for different types of actions. Moreover excessive use of function overloading may reduce the readability of your program and it may appear confusing.  
            Let’s see the program given below to illustrate this concept more:


//Program to demonstrate implementation of function overloading
#include <iostream.h>
#include <conio.h>
void Amt(float, int, float);   //prototypes of overloaded functions
void Amt(float, int);
void Amt(float, float);
void Amt(int, float);
void Amt(float );
//-------------------------------------------------------------
void Amt(float P, int T, float R)
{
cout<<"\n Principal Amt. = "<<P;
cout<<"\n Time = "<<T;
cout<<"\n Rate = "<<R;
cout<<"\n SI = "<<(P*R*T);
}
void Amt(float P, int T)
{
cout<<"\n Principal Amt. = "<<P;
cout<<"\n Time = "<<T;
cout<<"\n Rate applied = "<<"0.08";
cout<<"\n SI = "<<(P*0.08*T);
}
void Amt(float P, float R)
{
cout<<"\n Principal Amt. = "<<P;
cout<<"\n Time = 2 years";
cout<<"\n Rate applied = "<<R;
cout<<"\n SI = "<<(P*2*R);
}
void Amt(int T, float R)
{
cout<<"\n Principal Amt. = 2000";
cout<<"\n Time = "<<T;
cout<<"\n Rate applied = "<<R;
cout<<"\n SI = "<<(2000*2*R);
}
void Amt(float P)
{
cout<<"\n Principal Amt. = "<<P;
cout<<"\n Time = 2 years";
cout<<"\n Rate applied = 0.08";
cout<<"\n SI = "<<(P*2*0.08);
}
//--------------------------------------------------------
void main()
{
clrscr();
Amt(10000.0F,3,0.09F);cout<<endl;
Amt(10000.0F); cout<<endl;
Amt(2500.0F,3); cout<<endl;
Amt(5,0.06F); cout<<endl;
Amt(500.0F,0.05F);cout<<endl;
Amt(5000);
getch();
}
Output:

 

No comments:

Post a Comment