• Overloading refers to the process of using the same thing for different purposes. It is also referred to as function polymorphism. It works on the concept “Same name, multiple tasks”.
  • In programming language such as C, we cannot define multiple functions with the same name. We have to define all functions with unique name.
  • fortunately C++ provides a great feature called function overloading through which we can achieve this.
  • Function overloading means we can use the same function name to define multiple functions, which performs different tasks.
  • To perform function overloading we create multiple function definition with the same name but having different number of arguments, data types, and return values.
  • Compiler distinguishes among the functions definitions having the same name with the help of number of arguments and their data types.
  • Depending on the number of argument, data types and return types of argument during function call, appropriate function will be called by the compiler at run time.
// Function declaration

int  sum(int a, int b);                     // Function prototype-1
int  sum(int a, int b, int c);             // Function prototype-2

double sum(int a, int b, double c);       // Function prototype-3
double sum(int a, double b);             // Function prototype-4
double sum(double a, double b);         // Function prototype-5

// Function call

cout << sum(10, 20);                    // Use prototype-1
cout << sum(10, 20, 30);                // Use prototype-2
cout << sum(10, 20, 30.0);              // Use prototype-3
cout << sum(10, 20.0);                  // Use prototype-4
cout << sum(10.0, 20.0);                // Use prototype-5

  • In above example we have five different prototypes for the function sum.
  • Each of this function performs different task depending on the function definition.
  • Out of them we can call any one by passing its matching number of arguments and data types.

The following steps are performed by the compiler to call appropriate function:

  1. Compiler finds first the matching number of arguments and uses that function. Suppose there are two prototype available, one is using 2 arguments and another is using 3 arguments and during function call if we are passing 3 arguments then compiler executes the function having 3 arguments.
  2. If there are two functions having the same number of arguments then compiler checks for the data types of the arguments.
  3. If no match is found in arguments passed during function call then compiler internally performs conversion from:
  • char to int
  • float to double
  1. When there are two functions with the same number of arguments and same argument types then compiler will generate error. Also during internal conversion if there is ambiguity then compiler generates error so we have to be careful while dealing with function overloading..
e.g.   // Function declaration

long square(long n);
double  square(double x);

// Function call

square(10);                  
  • In above case when we are passing 10 as argument it is integer value so that may be converted to either long or double so compiler will generates error.

Note: We cannot separate two functions in function overloading by only differentiating them in return type of the argument.

e.g.  int   sum(int x, int y);
      float sum(int x, int y);    // This will generate the compilation error 

To understand the concept more clearly let’s have a look on the following functions to find out absolute values for integer as well as floating point without overloading them.

int iabs(int i)          // Returns absolute value of an integer.
{
   if (i < 0)
   { 
      return (i * -1);    // Makes positive.
   }
   else
   {
      return (i);        // Already positive.
   }
}

float fabs(float x)     // Returns absolute value of a float.
{
   if (x < 0.0)
   {
      return (x * -1.0);    // Makes positive.
   }
   else
   {
      return (x);        // Already positive.
   }
}
  • If you wish to find out absolute value for floating-point, you pass it to the fabs()
    function as:
ans = fabs(-15.0);
  • Similarly, ff you need to find out absolute value for integer, you pass it to the fabs() function as:
ans = fabs(-15);
  • Since the code for these two functions differ only in their parameter lists, they are perfect candidates for overloaded functions. We can define both functions with the same name as abs(). The C++ compiler determines which function you wanted to call depending on the number of arguments and their data types.
  • The overloaded versions of above functions fabs() and iabs() can be written as follow:
// Program to overload two absolute value functions.

#include "iostream.h"        // Prototype cout and cin.
#include "iomanip.h"        // Prototype setprecision(2).

int abs(int i);            // abs() is overloaded twice
float abs(float x);        // as shown by these prototypes.

void main()
{
   int ians;             // To hold return values.
   float fans;
   
   int i = -15;         // To pass to the two overloaded functions.
   float x = -64.53;

   ians = abs(i);       // C++ calls the integer abs().
   cout << “Integer absolute value of " <<i <<" is “ << ians << “\n”; 
 
  fans = abs(x);      // C++ calls the floating-point abs().
  cout << “Float absolute value of " <<x <<" is “ <<setprecision(2) << fans << “\n”;

     // Notice that you no longer have to keep track of two
    // different names. C++ calls the appropriate
    // function that matches the parameters.
   return;
}

int abs(int i)      // Integer absolute value function
{
   if (i < 0)
   {
      return (i * -1);     // Makes positive.
   }
   else
   {
      return (i);        // Already positive.
   }
}

float abs(float x)       // Floating-point absolute value function
{
   if (x < 0.0)
   { 
      return (x * -1.0);     // Makes positive.
   }
   else
   {
     return (x);             // Already positive.
   }
}

Output:

 Integer absolute value of -15 is 15
 Float absolute value of -64.53 is 64.53