Default arguments in C++
Arguments in a function are defined as the values supplied when the function is called. The source is the values supplied, and the destination is the receiving function. Let us now examine the idea of default arguments in further depth.
What is a default Argument?
The default argument is the value in the job declaration that automatically provides if the caller's function does not add value to that argument.
Default argument characteristics
The rules for specifying default parameters are as follows:
- The default parameters do not have constant values. If the value is supplied to the function, these values can be overwritten. Otherwise, the previously specified value is retained.
- The values are transferred from left to right when the function is called.
- All of the values that will be assigned a default value will be displayed on the right.
Example:
#include < iostream >
#include < bits/stdc++.h >
#include <stdlib >
using namespace std ;
int sum ( int a , int b , int c = 0 , int d = 0 ) // Here there are two values in the default arguments
{ // Both c and d are initialised to zero
return ( a + b + c + d ) ; // return sum of all parameter values
}
int main ( )
{
cout << sum ( 10 , 15 ) << endl ; // a = 10 , b = 15 , c = 0 , d = 0
cout << sum ( 10 , 15 , 25 ) << endl ; // a = 10 , b = 15 , c = 25 , d = 0
cout << sum ( 10 , 15 , 25 , 30 ) << endl ; // a = 10 , b = 15 , c = 25 , d = 30
return 0 ;
}
OUTPUT:
25
50
80
….
Process executed in 0.11 seconds
Press any key to continue.
Explanation
- When this function is invoked, it gets to the sum definition. There, it sets a to 10 and b to 15, and the rest of the values are set to zero by default because no value is supplied. And the aggregate of all the values yields the result 25.
- When this function is invoked, a remains 10, b remains 15, and the third parameter, c, is set to 25 rather than zero. And the final result is still 0. The total of a, b, c, and d equals 50, and this is returned as output.
- There are four argument values supplied into the function in this function call, with a as 10, b as 15, c as 25, and d as 30. The total of all the values produces the result 80.
Let us look at another example:
#include < iostream >
#include < bits/tsdc++.h >
#include < stdlib >
using namespace std ;
int sum ( int a , int b , int c = 0 , int d = 0 ) // Here there are two values in the default arguments
{ // Both z and w are initialized to zero
return ( a + b + c + d ) ; // return sum of all parameter values
}
int sum ( int a , int b , float c = 0 , float d = 0 ) // Here sum is overloaded with two float parameter values
{ // This results in ambiguity
return ( a + b + c + d ) ;
}
int main ( )
{
cout << sum ( 10 , 15 ) << endl ; // a = 10 , b = 15 , c = 0 , d = 0
cout << sum ( 10 , 15 , 25 ) << endl ; // a = 10 , b = 15 , c = 25 , d = 0
cout << sum ( 10 , 15 , 25 , 30 ) << endl ; // a = 10 , b = 15 , c = 25 , d = 30
return 0 ;
}
OUTPUT:
error: call of overloaded ' sum ( int , int ) ' is ambiguous
cout << sum ( 10 , 15 ) << endl ; // a = 10 , b = 15 , c = 0 , d = 0
^
note: candidate: int sum ( int , int , int , int )
int sum ( int a , int b , int c = 0 , int d = 0 ) // Here there are two values in the default arguments
note: candidate: int sum ( int , int , float , float )
int sum ( int a , int b , float c = 0 , float d = 0 ) // Here sum is overloaded with two float parameter values
Explanation:
If we call a sum function with all the arguments ( a, b, c, d ) and any value of a single parameter of c or d, the producer is not sure which function to use. As a result, it generates uncertainty, which leads to the mistake.