Function Overloading
Function overloading is an object-oriented programming feature where two or more functions can have the same name but different parameters. In other words, it refers to functions having the same name but different jobs with different parameters. It represents an example of polymorphism. Since functions have the same name, the compiler uses the number of arguments and argument types to determine which function to call. Functions are overloaded within the same scope.
#include <iostream>
using namespace std;
int add(int n1, int n2)
{
int sum = n1 + n2;
return sum;
}
float add(float n1, float n2, float n3)
{
float sum = n1 + n2 + n3;
return sum;
}
int main()
{
int sum1 = add(4, 3);
cout<<sum1<<'\n';
float sum2 = add(2.43, 5.43, 8.94);
cout<<sum2<<'\n';
return 0;
}

You can also try this code with Online C++ Compiler
Run Code
Output:
7
16.8
In the example shown, add() is an overloaded function. The first add function accepts two integer parameters and returns one integer result. The second add function receives three float values and returns one float result. The two function calls in the main() function call two different functions that lie within the same scope.
Ambiguity in Function Overloading
In C++, Ambiguity in Function Overloading is a situation that occurs when the compiler is unable to choose one function from all the overloaded functions for execution. This occurs mostly while passing floating-point numbers as variables to an overloaded function. Let's understand this using an example.
#include<iostream>
using namespace std;
void overload(float s,float t)
{
cout << "Function with float-point arguments";
}
void overload(int s, int t)
{
cout << "Function with integer arguments";
}
int main()
{
overload(2.5, 1.5);
return 0;
}

You can also try this code with Online C++ Compiler
Run Code
Output:
main.cpp: In function 'int main()':
main.cpp:15:20: error: call of overloaded 'overload(double, double)' is ambiguous
15 | overload(2.5, 1.5);
| ^
main.cpp:3:6: note: candidate: 'void overload(float, float)'
3 | void overload(float s,float t)
| ^~~~~~~~
main.cpp:7:6: note: candidate: 'void overload(int, int)'
7 | void overload(int s, int t)
| ^~~~~~~~
You can practice by yourself with the help of Online C++ Compiler for better understanding.
When the numbers 2.5 and 1.5 are passed as parameters to the function overload, they are passed as double and not float. Since, there are overloaded functions only for (int, int) and (float, float), the compiler is unable to find a suitable function to execute. This throws an error stating that the call for the overloaded function is ambiguous.
Rectifying the error
There are two ways to avoid these kinds of errors. The easiest way is to declare the values with their specific data types under variables and then pass them. The other way is to use the suffix - f. This lets the compiler know that the specified literal is a floating-point number and not a double.
#include<iostream>
using namespace std;
void overload(float s,float t)
{
cout << "Function with float-point arguments";
}
void overload(int s, int t)
{
cout << "Function with integer arguments";
}
int main()
{
float a = 2.5, b = 1.5;
overload(a, b);
return 0;
}

You can also try this code with Online C++ Compiler
Run Code
OR
#include<iostream>
using namespace std;
void overload(float s,float t)
{
cout << "Function with float-point arguments";
}
void overload(int s, int t)
{
cout << "Function with integer arguments";
}
int main()
{
overload(3.5f, 5.6f);
return 0;
}

You can also try this code with Online C++ Compiler
Run Code
Output:
Function with float-point arguments
In the first example shown below, variables a and b are declared as float and then passed to the overload() function during its call. Hence, the first function is called by the compiler.
In the second example, values are directly passed to the overload function. In this case, the suffix -f is used after the numbers to denote float data type. As a result, the first overload() function is called by the compiler after recognizing the float data type.
Check out this article - Compile Time Polymorphism
Must Read Non-alphanumeric Characters
Frequently Asked Questions
When is function overloading not possible?
Function Overloading is not possible if the overloaded functions have different return types. Functions are overloaded base on the difference in function signatures. Since the return type is not part of the function signature, having different return types can not overload functions.
What are prefixes and suffixes in C++?
Prefixes indicate the base of a number. For example, 0b represents binary numbers like 0b101, 0b11101 etc. Suffixes indicate the data type of a number. For example, 2.4f denotes a floating-point number, and 2634652845368LL denotes a long integer.
Conclusion
In this article, we have extensively discussed Ambiguity in Function Overloading in C++. We briefly went over the effects of not specifically declaring numbers as float before passing them to an overloaded function. We then moved on to see the two ways of avoiding the error caused due to ambiguity in Function Overloading.
Feeling curious? Coding Ninjas has you covered. Check out our articles on Constructors and Destructors, Function overloading in C++ and Classes and Objects in C++. Follow our Guided path for Programming in C++ here.
Explore our Library on Coding Ninjas Studio to gain knowledge on Data Structures and Algorithms, Machine Learning, Deep Learning, Cloud Computing and many more! Test your coding skills by solving our test series and participating in the contests hosted on Coding Ninjas Studio!
Looking for questions asked by tech giants like Amazon, Microsoft, Uber, etc.? Look at the problems, interview experiences, and interview bundle for placement preparations. Upvote our blogs if you find them insightful and engaging! Happy Coding!