“We either live with intention or exist by default” Kristin Armstrong
A default argument is the value that we set for the parameter/s of a function when it is being declared and will be used if we skip assigning value to those parameters of the function at runtime when it is called.
#include <iostream>
using namespace std;
void f1(int value = 20);
int main() {
f1();
return 0;
}
void f1(int value ){
cout<<value;
}
Output:
In this example, we called the f1() function within the body of the main() function, but we didn’t assign any value to this function as its argument. But still we got the output of 20.
This is because when we declared the function, one parameter of type int is set for this function that also has a default value of 20.
So now because we didn’t assign any argument when the function was called, the default one is used instead.
But if we skip the declaration of a function, and put the definition on top of the main() function, in that case we can assign the default values to the parameters of the target function.
Example:
#include <iostream>
using namespace std;
void f1(int value=20 ){
cout<<value;
}
int main() {
f1();
return 0;
}
Output:
20
Note: The default values are only used if we skip to put an argument for the parameters of the target function. But if we set a value, in that case the new argument will be used instead of the default one.
Example:
#include <iostream>
using namespace std;
void f1(int value= 20);
int main() {
f1(100);
return 0;
}
void f1(int value){
cout<<value;
}
Output:
100
As you can see, we set another value for the f1 function when it is called, and so that argument is sent to the output instead of the default one.
When we use a function with parameters, those parameters with default values should be add from right to left. This means we can’t have a parameter with default value unless all the parameters on the right side also have a default value.
Example:
int ex(int a, int b = 10, int c = 20); // This is VALID.
int ex1( int a, int b = 30, int c); // This is invalid because the `b` parameter has a default value but the `c` parameter which is on the right side of the `b` parameter does not have one.
int ex2( int a = 0, int b= 1, int c= 2); This is VALID.
Let’s call the function named ex with different number of arguments and see what will happen behind the scene:
#include <iostream>
using namespace std;
int ex(int a, int b = 10, int c = 20);
int main() {
ex(1); // same as ex(1,10,20);
ex(1,13); // same as ex(1,13,20);
ex(1,13,50); // same as ex(1,13,50);
return 0;
}
int ex(int a, int b, int c){
cout<<a<<" ,"<<b<<" ,"<<c<<endl;
}
Output:
1 ,10 ,20
1 ,13 ,20
1 ,13 ,50
In the first call to the ex function, we set only one argument and that will be assigned to the a parameter of the function and the other two parameters will have the default values.
In the second call, we set 2 arguments for the function and so the default value of the second argument of the function is replaced with the value we added.
And in the last call we used 3 arguments and as a result the two default arguments are replaced with new arguments that we inserted.