Within a function, parameters are specified as the data provided during a function invocation. The data supplied acts as the point of origin, while the function receiving this data serves as the endpoint.
Now, we will delve into a comprehensive explanation of default parameters in programming.
Definition
A default parameter is an assigned value in the function declaration that is automatically used by the compiler when no value is passed by the calling function for that parameter.
Attributes for delineating the default parameters
Following are the rules of declaring default arguments -
- The values passed in the default arguments are not constant. These values can be overwritten if the value is passed to the function. If not, the previously declared value retains.
- During the calling of function, the values are copied from left to right.
- All the values that will be given default value will be on the right.
Example
- function with void return type and parameters int x, int y, and int z with a default value of 0. Explanation - The function described above is considered correct. In this case, z is predetermined as part of the default argument.
- Function with void return type and parameters int x, int z with default value of 0, and int y. Explanation - The function shown above is considered incorrect. Here, z is defined in an incorrect position between the other parameters, making it unacceptable.
#include<iostream>
using namespace std;
int sum(int x, int y, int z=0, int w=0) // Here there are two values in the default arguments
{ // Both z and w are initialised to zero
return (x + y + z + w); // return sum of all parameter values
}
int main()
{
cout << sum(10, 15) << endl; // x = 10, y = 15, z = 0, w = 0
cout << sum(10, 15, 25) << endl; // x = 10, y = 15, z = 25, w = 0
cout << sum(10, 15, 25, 30) << endl; // x = 10, y = 15, z = 25, w = 30
return 0;
}
Output
25
50
80
Explanation
In the above program, we have called the sum function three times.
- Sum(10,15) When this function is called, it reaches the definition of the sum. There it initializes x to 10 y to 15, and the rest values are zero by default as no value is passed. And all the values after sum give 25 as output.
- Sum(10, 15, 25) When this function is called, x remains 10, y remains 15, the third parameter z that is passed is initialized to 25 instead of zero. And the last value remains 0. The sum of x, y, z, w, is 50 which is returned as output.
- Sum(10, 15, 25, 30) In this function call, there are four parameter values passed into the function with x as 10, y as 15, z is 25, and w as 30. All the values are then summed up to give 80 as the output.
Note If the function is overloaded with different data types that also contain the default arguments, it may result in an ambiguous match, which results in an error.
Example
#include<iostream>
using namespace std;
int sum(int x, int y, int z=0, int w=0) // Here there are two values in the default arguments
{ // Both z and w are initialised to zero
return (x + y + z + w); // return sum of all parameter values
}
int sum(int x, int y, float z=0, float w=0) // Here sum is overloaded with two float parameter values
{ // This results in ambiguity
return (x + y + z + w);
}
int main()
{
cout << sum(10, 15) << endl; // x = 10, y = 15, z = 0, w = 0
cout << sum(10, 15, 25) << endl; // x = 10, y = 15, z = 25, w = 0
cout << sum(10, 15, 25, 30) << endl; // x = 10, y = 15, z = 25, w = 30
return 0;
}
Output
prog.cpp: In function 'int main()':
prog.cpp:15:20: error: call of overloaded 'sum(int, int)' is ambiguous
cout << sum(10, 15) << endl; // x = 10, y = 15, z = 0, w = 0
^
prog.cpp:4:5: note: candidate: int sum(int, int, int, int)
int sum(int x, int y, int z=0, int w=0) // Here there are two values in the default arguments
^
prog.cpp:9:5: note: candidate: int sum(int, int, float, float)
int sum(int x, int y, float z=0, float w=0) // Here sum is overloaded with two float parameter values
Explanation
When the sum function is invoked with multiple parameters such as x, y, z, and w, or even with just the value of z or w, it introduces ambiguity to the compiler regarding which specific function to proceed with. This uncertainty leads to the occurrence of an error.