How can a call to an overloaded function be Ambiguous

In this article, we are going to study how can a call to an overloaded function be ambiguous. We will also be discussing what an overloaded function is and how it affects the working of our program.

What is an Overloaded Function?

Overloaded functions or methods in object-oriented programming are a part of polymorphism where there can be more than one function with the same name but different working. The function name can be the same but the number of parameters or the data type of the parameter can be different in the function. Thus, we get to know from this reason as well how can a call to an overloaded function be ambiguous.

Function overloading is done in compile time and it is faster in execution and less flexible as compared to runtime execution which has slower execution but offers more flexibility.

Reasons why a call to an overloaded function be ambiguous

In the above example, it can be obvious for us to see how can a call to an overloaded function be ambiguous. As the first function has three parameters namely, score, total and grade, having three data types orderly as, integer, integer and float. The second function has two parameters, average with integer data type and grade with the character data type.

Let’s look at a code to understand this concept of how can a call to an overloaded function be ambiguous.

#include <iostream>
using namespace std; 
void fun(int score, int total, float sgpa)
{
  cout << "The score and totals is" << endl;
  cout << score <<endl <<total;
} 
void fun(float average, char* grade)
{
    cout << endl << "Average is = " << average;
    cout << endl << "Grade is = " << grade;
} 
// Driver code
int main()
{
    fun(167, 200,8.35);
    fun(53.3, “C”);
    return 0;
}
Output:
The score and totals is
167
200
Average is = 53.3
Grade is = C

Another answer to how can a call to an overloaded function be ambiguous is when there is a default argument put in place, the function will tend to behave ambiguously when no value is passed, it is going to have a predefined value to it which will be assigned when it is in execution else the user can pass an updated value as an argument.

For ex:-

In the above-mentioned example, the default value of total is set up as 500 which is taken as the total value in case the argument is not passed by the user for the total score as mentioned in the second function which consists of only the score. It is another reason, how can a call to an overloaded function be ambiguous. Let us understand this with the help of code:

// Default argument
#include <iostream>
using namespace std; 
void fun(int score, int total = 500)
{
    cout << endl << "Score is = " << score;
    cout << endl << "Total is = " << total;
} 
// Driver code
int main()
{
    fun(167, 200);
    fun(256); 
    return 0;
}
Output:
Score is = 167
Total is = 200
Score is = 256
Total is = 500

The above program states how can a call to an overloaded function be ambiguous as the moment score was passed with an additional parameter, the total was user defined as 200 unlike the following scenario, when score gave alone, the total was already assumed to be 500 as it is already a default parameter.

Understanding Ambiguity

The mentioned codes helped us get an idea on what makes an impact on creating ambiguity while calling functions and how functions tend to behave differently in specific circumstances, which are named as follows:-

  • Number
  • Order
  • Data Type

Any difference or perhaps, ambiguity to the above three while calling a function can lead to overloading. And this may result in an entirely different outcome with an additional returned value altogether.

Conclusion

We understood how a call to an overloaded function can be ambiguous, and studied the concept of function overloading and how it is a part of polymorphism, an object-oriented programming concept along with its practical use and code.

I hope you liked this article on Inheritance in Java. We hope to see you at PrepBytes soon.

FAQs Related to Function Overloading

1. Is function overloading only restricted to C++ Programming language?
Ans. No, Function Overloading is a subpart of Object Oriented Programming’s feature that is Polymorphism and it is omnipresent in other programming languages like Python, Java etc.

2. Is function overloading performed in compile-time or runtime?
Ans. Function overloading is performed in compile-time while function overriding is a runtime procedure.

Leave a Reply

Your email address will not be published. Required fields are marked *