Default Function Arguments, Function Overloading and Ambiguity
1. Default Function Arguments
1.1 Introduction
A default function argument is a value assigned to a function parameter in the function declaration. If the calling statement does not provide a value for that parameter, the compiler automatically uses the default value.
Default arguments are useful when a function should work with both complete and partial sets of arguments.
For example:
void display(int a, int b = 10);
Here, b has a default value of 10.
Therefore, both of the following calls are valid:
display(5); display(5, 20);
For the first call, b automatically receives 10.
Figure 1: Default Function Arguments
1.2 Syntax
The general syntax is:
return_type function_name(parameter1, parameter2 = default_value);
For example:
void show(int a, int b = 10);
Multiple parameters can also have default values:
void show(int a, int b = 10, int c = 20);
1.3 Program Example
#include <iostream> using namespace std; void show(int a, int b = 10, int c = 20) { cout << "a = " << a << ", b = " << b << ", c = " << c << endl; } int main() { show(5); show(5, 15); show(5, 15, 30); return 0; }
Output
a = 5, b = 10, c = 20 a = 5, b = 15, c = 20 a = 5, b = 15, c = 30
1.4 Explanation
The function is declared as:
void show(int a, int b = 10, int c = 20)
Here:
ahas no default value.bhas a default value of10.chas a default value of20.
Call 1
show(5);
Only a is supplied.
Therefore:
a = 5 b = 10 ← default value c = 20 ← default value
Call 2
show(5, 15);
Now a and b are supplied.
Therefore:
a = 5 b = 15 c = 20 ← default value
Call 3
show(5, 15, 30);
All three values are supplied:
a = 5 b = 15 c = 30
2. Rules for Default Arguments
Rule 1: Default arguments are generally specified from right to left
Correct:
void fun(int a, int b = 10, int c = 20);
Incorrect:
void fun(int a = 10, int b, int c = 20);
A parameter having a default argument cannot normally be followed by a parameter without a default argument in the same parameter list.
Rule 2: Once default arguments start, following parameters should also have defaults
Correct:
void fun(int a, int b = 10, int c = 20);
Incorrect:
void fun(int a = 10, int b, int c);
Rule 3: Default arguments reduce the need for multiple overloaded functions
For example:
void display(int x, int y = 10);
can handle:
display(5); display(5, 20);
Instead of creating separate functions for both cases.
3. Function Overloading
3.1 Introduction
Function overloading allows multiple functions to have the same name but different parameter lists.
The compiler determines which function to call based on:
- number of arguments,
- type of arguments,
- order of arguments.
For example:
void display(int); void display(double); void display(int, int);
All three functions have the name display, but their parameter lists are different.
Figure 2: Function Overloading
3.2 Syntax
return_type function_name(parameter_list_1); return_type function_name(parameter_list_2); return_type function_name(parameter_list_3);
For example:
void calculate(int); void calculate(double); void calculate(int, int);
4. Program Example: Function Overloading
#include <iostream> using namespace std; void display(int x) { cout << "Integer: " << x << endl; } void display(double x) { cout << "Double: " << x << endl; } void display(int x, int y) { cout << "Two integers: " << x << " " << y << endl; } int main() { display(10); display(5.5); display(10, 20); return 0; }
Output
Integer: 10 Double: 5.5 Two integers: 10 20
4.1 Explanation
When the compiler encounters:
display(10);
it finds the function:
void display(int x)
because the argument is an integer.
For:
display(5.5);
the compiler selects:
void display(double x)
For:
display(10, 20);
it selects:
void display(int x, int y)
Thus, function overloading allows the same function name to perform related operations for different parameter combinations.
5. Function Overloading and Ambiguity
5.1 Introduction
Ambiguity occurs when the compiler finds more than one overloaded function that can match a function call and cannot determine a single best match.
In such a situation, the compiler generates an ambiguous call compilation error.
Ambiguity can occur particularly because of:
- implicit type conversions,
- default arguments,
- multiple equally suitable overloaded functions.
Figure 3: Ambiguity in Function Overloading
6. Example of Ambiguity Due to Type Conversion
Consider:
#include <iostream> using namespace std; void show(int x) { cout << "Integer function"; } void show(float x) { cout << "Float function"; } int main() { show(5.5); return 0; }
Here, 5.5 is a double literal.
The compiler may consider conversions to both int and float. Neither overloaded function provides an exact double match.
Therefore, the call can result in an ambiguous overload resolution rather than selecting one arbitrarily.
The important lesson is that implicit conversions can make overloaded calls difficult for the compiler to resolve.
7. Resolving Ambiguity Using Explicit Type
One way to avoid ambiguity is to explicitly specify the required type.
For example:
show(static_cast<float>(5.5));
Now the argument is explicitly converted to float.
The compiler selects:
void show(float x)
Similarly:
show(static_cast<int>(5.5));
selects:
void show(int x)
Program
#include <iostream> using namespace std; void show(int x) { cout << "Integer function" << endl; } void show(float x) { cout << "Float function" << endl; } int main() { show(static_cast<int>(5.5)); show(static_cast<float>(5.5)); return 0; }
Output
Integer function Float function
8. Ambiguity Due to Default Arguments
Default arguments can also create ambiguity when combined with overloaded functions.
Consider:
void fun(int x, int y = 10); void fun(int x);
Now consider:
fun(5);
There are two possible matches:
fun(int);
and
fun(int, int = 10);
The compiler cannot determine which function the programmer intended.
Therefore, the call is ambiguous.
Figure 4: Default Arguments Creating Ambiguity
9. Program Example: Ambiguity Due to Default Argument
#include <iostream> using namespace std; void fun(int x) { cout << "One argument function" << endl; } void fun(int x, int y = 10) { cout << "Two argument function" << endl; } int main() { fun(5); return 0; }
Result
The compiler reports an ambiguous call because fun(5) can match both:
fun(int)
and:
fun(int, int = 10)
10. How to Avoid Ambiguity
Ambiguity can generally be avoided by designing the overloaded functions carefully.
Method 1: Use a more specific argument
Instead of:
fun(5);
provide the required number of arguments where applicable:
fun(5, 20);
This selects:
fun(int, int);
Method 2: Use explicit type conversion
For overloaded functions:
show(static_cast<float>(5.5));
This clearly identifies the desired parameter type.
Method 3: Avoid conflicting default arguments
Do not unnecessarily define:
fun(int); fun(int, int = 10);
because a single argument can match both.
11. Relationship Between Default Arguments and Function Overloading
Default arguments and function overloading both provide flexibility in function calls, but they work differently.
| Default Arguments | Function Overloading |
|---|---|
| One function can handle different numbers of supplied arguments | Multiple functions have the same name |
| Missing arguments receive predefined values | Compiler selects a function based on parameter matching |
Example: fun(int, int = 10) | Examples: fun(int), fun(double) |
| Can reduce the need for overloaded functions | Provides different implementations |
| Can cause ambiguity when combined with overloads | Can cause ambiguity when multiple overloads match |
12. Combined Concept
The three concepts can be understood through the following relationship:
Conceptual Flow
Function call
↓
Check supplied arguments
↓
Apply default arguments if required
↓
Find matching overloaded functions
↓
If one best match exists → function is called
↓
If multiple equally suitable matches exist → ambiguity error
13. Important Points
Default Function Arguments
- Default arguments provide predefined values for function parameters.
- They are used when the caller does not supply corresponding arguments.
- Default arguments are normally specified from right to left.
- They can reduce the requirement for multiple overloaded functions.
- Care must be taken when combining default arguments with function overloading.
Function Overloading
- Multiple functions can have the same name.
- Their parameter lists must differ.
- Return type alone cannot be used for function overloading.
- The compiler selects the appropriate function during overload resolution.
- Number, type, and order of arguments are important.
Ambiguity
- Ambiguity occurs when more than one overloaded function can match a call without a unique best match.
- Implicit type conversions can produce ambiguity.
- Default arguments can also produce ambiguity with overloaded functions.
- Explicit type conversion can help resolve some ambiguous calls.
- Good function design can prevent ambiguous overloads.
14. Common Mistakes
Mistake 1: Using a default parameter before a required parameter
void fun(int x = 10, int y);
This is not a valid way to arrange default arguments.
Prefer:
void fun(int x, int y = 10);
Mistake 2: Assuming return type creates overloading
These are not overloaded functions:
int fun(int); double fun(int);
The parameter lists are identical. Changing only the return type does not constitute function overloading.
Mistake 3: Creating conflicting overloads with default arguments
void fun(int); void fun(int, int = 10);
The call:
fun(5);
is ambiguous.
15. Key Points for Examination
- Default argument: A predefined value assigned to a function parameter that is used when the corresponding argument is omitted.
- Function overloading: Defining multiple functions with the same name but different parameter lists.
- Ambiguity: A condition in which the compiler cannot uniquely determine which overloaded function should be called.
- Overload resolution: The compiler's process of selecting the most appropriate overloaded function for a given function call.
- Implicit conversion: Automatic conversion of an argument from one data type to another, which can sometimes contribute to overload ambiguity.
Exam-Oriented Definitions
Default Function Arguments:
Default function arguments are values assigned to function parameters in advance. If the caller does not provide a value for such a parameter, the specified default value is automatically used.
Function Overloading:
Function overloading is a C++ feature that allows multiple functions to have the same name with different parameter lists.
Function Overloading Ambiguity:
Function overloading ambiguity occurs when a function call can match more than one overloaded function and the compiler cannot determine a unique best match.
Important distinction:
Default arguments provide different ways of calling one function, whereas function overloading provides multiple function definitions. When the two mechanisms overlap, they must be designed carefully to avoid ambiguous calls.
No comments:
Post a Comment