Free Hosting

Default parameters


default parameter is a function parameter that has a default value provided to it. If the user does not supply a value for this parameter, the default value will be used. If the user does supply a value for the default parameter, the user-supplied value is used.
Consider the following program:
1
2
3
4
5
6
7
8
9
10
11
12
void PrintValues(int nValue1, int nValue2=10)
{
    using namespace std;
    cout << "1st value: " << nValue1 << endl;
    cout << "2nd value: " << nValue2 << endl;
}
 
int main()
{
    PrintValues(1); // nValue2 will use default parameter of 10
    PrintValues(3, 4); // override default value for nValue2
}
This program produces the following output:
1st value: 1
2nd value: 10
1st value: 3
2nd value: 4
In the first function call, the caller did not supply an argument for nValue2, so the function used the default value of 10. In the second call, the caller did supply a value for nValue2, so the user-supplied value was used.
Default parameters are an excellent option when the function needs a value that the user may or may not want to override. For example, here are a few function prototypes for which default parameters might be commonly used:
1
2
3
void OpenLogFile(char *strFilename="default.log");
int RollDie(int nSides=6);
void PrintString(char *strValue, Color eColor=COLOR_BLACK); // Color is an enum
A function can have multiple default parameters:
1
2
3
4
5
void PrintValues(int nValue1=10, int nValue2=20, int nValue3=30)
{
    using namespace std;
    cout << "Values: " << nValue1 << " " << nValue2 << " " << nValue3 << endl;
}
Given the following function calls:
1
2
3
4
PrintValues(1, 2, 3);
PrintValues(1, 2);
PrintValues(1);
PrintValues();
The following output is produced:
Values: 1 2 3
Values: 1 2 30
Values: 1 20 30
Values: 10 20 30
Note that it is impossible to supply a user-defined value for nValue3 without also supplying a value for nValue1 and nValue2. This is because C++ does not support a function call such as PrintValues(,,3). This has two major consequences:
1) All default parameters must be the rightmost parameters. The following is not allowed:
1
void PrintValue(int nValue1=10, int nValue2); // not allowed
2) The leftmost default parameter should be the one most likely to be changed by the user.
Default parameters and function overloading
Functions with default parameters may be overloaded. For example, the following is allowed:
1
2
void Print(char *strString);
void Print(char ch=' ');
If there user were to call Print(), it would resolve to Print(' '), which would print a space.
However, it is important to note that default parameters do NOT count towards the parameters that make the function unique. Consequently, the following is not allowed:
1
2
void PrintValues(int nValue);
void PrintValues(int nValue1, int nValue2=20);
If the caller were to call PrintValues(10), the compiler would not be able to disambiguate whether the user wanted PrintValues(int) or PrintValues(int, 20) with the default value.

0 comments:

Blogger Template by Clairvo