Function Overloading in C++
Function overloading is an important built-in feature of C++ programming. It allows C++ programmers to declare multiple functions in a program with a single name but multiple individual variable parameter lists. These declared user-defined functions can have multiple individual variables or parameters of any number or data type. The function overloading concept helps improve the readability and compatibility of your BASIC program because a user-defined function provides a specific function that performs the same or similar function operation in a program. However, these declared functions help handle multiple individual data types or number of arguments.

Overloaded functions declared in C++ programming are distinguished by the number or data type of their variable parameters, but not by the return data type of their method.
Important features of the function overloading concept in C++.
- Same function name – All overloaded function methods declared in a program must have the same or similar name.
- Different parameters – Function parameters declared in a program are individually separated by variable number or user-defined data type.
- Return type does not differentiate overloaded functions – Remember, C++ users cannot overload a function based solely on multiple individual return data types.
- Compile-time resolution – The proper function is selected at compile time based on the variable argument values passed to a function declared in a program.
Syntax for function overloading in C++.
return_type function_name(parameter1, parameter2, …);
Here, you can use this function syntax to define functions with the same name but multiple individual different variable parameter data types or numbers.
Example of function overloading in C++.
#include <iostream>
using namespace std;
// here total Function used to total two integers parameter value
int total(int p, int q) {
return p + q;
}
// here total Function used to total three integers parameter value
int total(int p, int q, int r) {
return p + q + r;
}
// here total Function used to total two double data type parameter value
double total(double p, double q) {
return p + q;
}
int main() {
int total1 = total(7, 2); // here this function calls the total(int, int) parameter value
int total2 = total(8, 1, 7); // here this function calls the total(int, int, int) parameter value
double total3 = total(7.3, 4.4); // Here this function calls the total(double, double) parameter value
cout << “Output of total two integer parameters [7, 2] value = ” << total1 << endl;
cout << “Output of total two integer parameters [8, 1, 7] value = ” << total2 << endl;
cout << “Output of total two double parameters [7, 3, 4, 4] value = ” << total3 << endl;
return 0;
}
Explanation of function overloading in C++.
- Here the first user-defined function total(int, int) totals two individual integer values.
- Here the second user-defined function total(int, int, int) totals three individual integer values.
- Here the third user-defined function total(double, double) totals two individual double values.
- In this example, a C++ user has overloaded the total function three times with different data types. The exact function is called based on the passed total function argument numbers and their defined data types.
Function overloading rules in C++.
Varying number of arguments – C++ users can overload a function by modifying the variable number of parameters in a function.
#include <iostream>
using namespace std;
// here it multiplies two integers with the parameter value
int multiply(int p, int q) {
return p * q;
}
// here it multiplies three integers with the parameter value
int multiply(int p, int q, int r) {
return p * q * r;
}
int main() {
int m = 3, n = 2, o = 9;
cout << “multiple of 2 integer value [3,2] = ” << multiply(m, n) << endl;
cout << “multiple of 2 integer value [3,2,9] = ” << multiply(m, n, o) << endl;
return 0;
}
Explanation of Varying Number of Arguments.
- Here in this program, the first multiply function multiplies two integer data type values p * q.
- The second function multiplies three integer values p * q * r.
Different types of arguments – C++ users can also overload custom function methods by applying multiple, separate individual data type variable parameters to a function program.
#include <iostream>
using namespace std;
// Here it is: Add two integers as parameter values
int addition(int m, int n) {
return m + n;
}
// Here it is: Add two doubles as parameter values
double addition(double m, double n) {
return m + n;
}
int main() {
int p = 3, q = 9;
double l = 7.2, m = 6.3;
cout << “addition of integers as parameter values - ” << addition(p, q) << endl;
cout << “addition of doubles as parameter values - ” << addition(l, m) << endl;
return 0;
}
Explanation of Different Types of Arguments.
- In this program, the first function adds two integer parameter values.
- The second function adds two double data type parameter values.
Default Arguments – C++ users can also apply default arguments to overloaded functions in a function program. However, in the default argument condition, the compiler decides which function to call in the current program based on the provided function parameter arguments.
int sub(int p, int q = 10) {
return p + q;
}
Explanation of Default Arguments.
- In this program, the function can be called with one or both arguments.
Function Overloading Concept with Different Return Types.
In a user-defined C++ function program, you cannot overload declared functions based solely on their return data type. If the return data types in a program are different, but the variable parameter lists declared in the function are similar, the compiler will not be able to differentiate between the existing function parameters, and this will result in a compilation error in the current program.
Example of incorrect C++ function overloading.
#include <iostream>
using namespace std;
int total(int p, int q) {
return p + q;
}
double total(int p, int q) { // display Error: here it cannot overload based only on return type
return p + q;
}
int main() {
cout << total(1, 2) << endl;
return 0;
}
Explanation of Function Overloading Concept with Different Return Types.
- This program will display a compilation error because both user-defined functions have identical parameter lists, differing only in the return data type of the method’s data type parameter. Therefore, C++ does not allow function overloading based solely on a single return data type.
