Function overloading means defining a function with same name but different arguments. Arguments may differ in number, type or both.
Functions having same name but different arguments are called overloaded functions.
Function overloading allows us to define functions that perform same task with the same name so that you do not need to find new names for functions performing same task.
Overloaded functions are invoked based on the matching arguments since the names of these functions are the same.
Below is an example of overloaded functions that calculate area. Calculating area of square requires only one value, area of rectangle requires two values and so on.
Thus you require multiple functions and since all the functions calculate area, we can use same name for all the functions.
#include <iostream> using namespace std; int area(int side) { return side * side; } double area(double length, double breadth) { return length * breadth; } long area(int length, int breadth) { return length * breadth; } int main() { cout << "Area of square is " << area(5) << "\n"; cout << "Area of rectangle is " << area(10.5, 12.5) << "\n"; cout << "Area of rectangle is " << area(6, 10) << "\n"; return 0; }
Above example defines three functions with same name. One of them accepts a single argument while other two expect the same number but different type of arguments.
Functions are invoked based on the argument types. When second function is called, the compiler matches the type of arguments of the called function and the function defined(both are double
).
Similarly when calling third function, it invokes the function matching the arguments of called and defined functions(int
). Below is the result.
Area of square is 25
Area of rectangle is 131.25
Area of rectangle is 60
Function overloading rules
Overloaded functions follow the below guidelines.
- Names of these functions should be same. Functions with different names will be different functions, they will not be considered as overloaded.
- Their return types may be same or different.
- Number of arguments may be same or different.
- Types of arguments should be same or different.
- If two functions have same number of arguments, then the data type of atleast one argument must be different.
Following function declarations have same number but different type of arguments, they will be considered as overloaded.
double volume(double, double, double); double volume(double, long, double);
Functions can not be overloaded just by changing their return types. Thus, below function declarations will be an error.
int sum(int, int); double sum(int, int);
a function cannot be overloaded only by its return type
When invoking an overloaded function, the compiler finds for the matching function among overloaded functions. Following is the sequence of steps that it takes.
- Finds an exact function matching the arguments supplied while calling the function.
- If exact match is not found, compiler will try to match a function by type conversion. Example, a function defined with
long
argument type will be matched with a function that supplies afloat
value. - If after any of the above steps more than one functions are matched, then the compiler raises an ambiguity error.
Function overloading is a useful feature and should be used with functions that perform related tasks.