Default Parameter is a value that is assigned to each parameter while declaring a function.
This value is used if that parameter is left blank while calling to the function. To specify a default value for a particular parameter, we simply assign a value to the parameter in the function declaration.
If the value is not passed for this parameter during the function call, then the compiler uses the default value provided. If a value is specified, then this default value is stepped on and the passed value is used.
Example:
int multiply(int a, int b=2)
{
int r;
r = a * b;
return r;
}
int main()
{
Cout<<multiply(6);
Cout<<”\n”;
Cout<<multiply(2,3);
}
Output:
12
6
As shown in the above code, there are two calls to multiply function. In the first call, only one parameter is passed with a value. In this case, the second parameter is the default value provided. But in the second call, as both the parameter values are passed, the default value is overridden and the passed value is used.