FUNCTION OVERLOADING

FUNCTION OVERLOADING 

     Function overloading is one of the most exciting feature of C++.  In C ++, two or more functions can share the same name that perform similar operations but have different parameter lists.  In such a situation, the functions that share the same name are said to be overloaded and the process is referred to as function overloading.  Therefore, function overloading allows a family of functions to share a common name that perform multiple related activities.  When an overloaded function is invoked, it is the responsibility of the compiler to select the appropriate function based on the number of argument (s) passed and if the number of argument (s) are same then depending upon the type of argument (s) passed to the function.  Thus, the key to function overloading is a function's parameter list.  A function return type is not enough to distinguish between two overloaded functions if the parameter lists of two functions matches exactly.  
     In order to understand the concept of function overloading, let us consider an example of a function that is used to calculate sum of two numbers.  The function declaration and the function definition of function sum_int () is given.  
          Function Declaration: 
               void sum_int (int, int);  
          Function Definition: 
               void sum int (int n, int y)
{
                int z = x + y;  
                cout << z;  
     The above function when used in the program will calculate the sum of two integer numbers.  Now suppose that we also want to calculate the sum of two double numbers in the same program.  To solve this problem, we need to make a new function with different name and double type parameters.  The function declaration of this new function sum_double () will look like void sum double (double, double); 
The following two program illustrate how the above problem can be solved using the concept of function overloading.
1.



 Explanation: In the above program, three functions are declared, called and defined which have the same name sum () but differ only in their parameter types (int, double, char).  For each call of the function, the compiler chooses the proper function by comparing the types of the arguments in the call against the parameter list of each defined function.  So when the statement sum (c, d);  is executed, the compiler invokes the function which takes double type parameters, as seen in output.
  

2. Program to show the concept of function overloading to calculate area of where same name function differs in number of parameters.


Explanation: The above program is used to calculate the area of ​​the square, rectangle and triangle.  For this, three functions are declared, defined and called separately.  Each function has the same name area but differs only in the number of parameters.  When the statement area (side);  is executed, the compiler invokes the function which contains only one parameter for calculating area of ​​square.  Now on execution of the statement area (le, br);  the function area () with two parameters is invoked for calculating the area of ​​rectangle.  Similarly, the next call invokes function with three parameters.                 

ADVANTAGES OF FUNCTION OVERLOADING 

      The advantages of function overloading are: 
1. We need to remember single name instead of multiple names of the functions that perform similar type of operations.  This helps in reducing the complexity of making large programs.  
2. Overloaded functions that perform similar tasks can make complex programs more readable, understandable and easy to debug.  
3. Maintainability of the program becomes easier.  
4. Overloaded functions are extensively used for handling class objects.

THINGS TO REMEMBER In Function Overloading:

1. Function overloading is a powerful tool for creating group of related functions that only differ in the type of data sent to it.  However, if not used properly such as using functions with the same name for different purposes can cause considerable confusion which leads to additional overheads in terms of maintainability.  For example: Functions performing various arithmetic operations like addition, subtraction, multiplication, division should not be overloaded with the same name functions as they perform different unrelated operations.  
2. If the functions differ only in the return type then they are not considered as overloaded functions and hence the compiler generates an error message.  This is because it is not possible for the compiler to determine which version of the function to call.

No comments

Powered by Blogger.