Function Overloading in C++ (original) (raw)

Function overloading allows us to define multiple functions with the same name but with different parameters, so that the same function name can perform different tasks depending on the values and types of arguments passed.

Example : Assume that you have to add 2 integers. It's easy to make a function for that as shown:

int add2(int a, int b) {
return a + b;
}

Now, assume that you also need a function that adds 3 integers. One method is to create another function with different name:

int add3(int a, int b, int c) {
return a + b + c;
}

But in this case, it may be difficult for you to understand the behaviour of the function because its name differs. Moreover, it is very possible to forget about the other function.

This is where function overloading comes into play. It allows us to create both of the functions with same name as shown:

C++ `

#include using namespace std;

int add(int a, int b) { return a + b; } // Same function with different arguments int add(int a, int b, int c) { return a + b + c; }

int main() { int a = 5, b =7, c = 11; // Add function to add two numbers cout << add(a, b) << endl; // Add function to add three numbers cout << add(a, b, c); return 0; }

`

Different Ways of Function Overloading

A function in C++ can be overloaded in three different ways:

**1. Different Number of Parameters

We can overload a function by changing the number of parameters that it accepts.

**Example: This program demonstrates function overloading with different number of parameters. The given program has two different function definitions for the function with different number of parameters.

C++ `

#include using namespace std;

int multiply(int a, int b); int multiply(int a, int b, int c);

int main() { cout << multiply(10, 2) << endl; cout << multiply(5, 6, 4);

return 0;

}

int multiply(int a, int b) { return a * b; } int multiply(int a, int b, int c) { return a * b * c; }

`

**2. Different Types of Parameters

We can also change the data type of the parameters while keeping the same function name in function overloading.

**Example: This program demonstrates function overloading with different data types of parameters. The given program has two different function definitions for the function with parameters of different data types (double and int) .

C++ `

#include using namespace std;

int add(int a, int b); double add(double a, double b);

int main() { cout << add(10, 2) << endl; cout << add(5.3, 6.2); return 0; }

int add(int a, int b) { return a + b; } double add (double a, double b) { return a + b; }

`

**3. Different Number and Types of Parameters

Functions can also be overloaded by changing both the number and types of parameters.

**Example: In the given program the function is overloaded and has two different function definitions with different order of parameters of different data types.

C++ `

#include using namespace std;

int add(int a, double b); double add(double a, int b, int c);

int main() { cout << add(10, 2.5) << endl; cout << add(5.5, 6, 12); return 0; }

int add(int a, double b) { return a + (double)b; }
double add(double a, int b, int c) { return a + (double)b + (double)c; }

`

Functions that Cannot be Overloaded

In C++, we have some specific scenarios in which the functions cannot be overloaded. As function overloading is primarily based on the function name and the types or number of its parameters. There could be cases where the C++ compiler cannot differentiate between functions and thus cannot be overloaded.

Why functions cannot be overloaded based on return type?

C++ doesn't allow function overloading by changing the return type. It is because the return type is not included in the function call, due to which the compiler won't be able to distinguish between them resulting in ambiguity issue. Therefore, we cannot achieve function overloading by changing the return type only.

**Example

C++ `

#include using namespace std;

string get() { return "This returns string"; } int get() { return 99; }

int main() { string s = get() cout << s;

return 0;

}

`

**Output

./Solution.cpp: In function 'int get()':
./Solution.cpp:8:9: error: ambiguating new declaration of 'int get()'
int get() {
^
./Solution.cpp:5:8: note: old declaration 'std::__cxx11::string get()'
string get() {
^
./Solution.cpp: In function 'int main()':
./Solution.cpp:17:5: error: expected ',' or ';' before 'cout'
cout << s;

Working of Function Overloading

Function overloading in C++ allows multiple functions to have the same name but differ in the number or type of their parameters. The compiler determines the correct overloaded function by evaluating the function signature which consist of the function's name and parameter types and matches it with the passed arguments in the function call.

Function Overloading vs Overriding

Some of the common difference between Function Overloading and Function Overriding are as follows:

Aspect Function Overloading Function Overriding
Definition Same function name with different parameters. Same function with same parameters but different function definition.
Purpose To create multiple functions that perform similar tasks with varying arguments. To change or extend the behavior of an inherited function.
Function Signature Function signatures should be different. Function signature should be similar (name, return type, parameters).
Compile-time or Runtime Done at compile time Done at run time
Scope Overloaded functions are in same scope. Overridden functions are in different scopes.