A 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:
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:
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:
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:
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:
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:
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:
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.
7.8 — Function Pointers
|
Index
|
7.6 — Function overloading
|
7.8 — Function Pointers
Index
7.6 — Function overloading
[...] 2007 Prev/Next Posts « 7.5 — Inline functions | Home | 7.7 — Default parameters » Friday, August 3rd, 2007 at 8:25 [...]
[...] 2007 Prev/Next Posts « 7.7 — Default parameters | Home | 7.9 — The stack and the heap » Wednesday, August 8th, 2007 at 4:52 [...]
how do you get a random value in C++?
For example :
int main() { using namespace std; int y = 25; // I want y to be a random number below 100 int x; try_again: do { cout << "Enter a value below 100: "; cin >> x; }while (x > 100); if (x < y) { cout << "Too small" << endl; goto try_again; } if (x > y) { cout << "Too big" << endl; goto try_again; } if (x == y) { cout << "WOW!! You Got it!" << endl; } return 0; }You see, I want y to be a random number when program starts up.
I just added a lesson on random number generation. :)
u forgot ; in the Prototype
//Function
#include
using namespace std;
void printvalues(int nvalue1, int nvalue2=10 );
int main()
{
printvalues(1);
printvalues(1,3);
/*scaffolding code for testing purpose*/
cin.ignore(256, ‘\n’);
cout<< "press Enter to continue.." << endl;
cin.get();
return 0;
}
void printvalues(int nvalue1, int nvalue2 = 10 )
{
cout<<"1st : "<< nvalue1<< endl;
cout<<"2st : "<< nvalue2<< endl;
}
hi
can any one tell me what is wrong with this one??
thak you
you must not use specify the default value both in function prototype and definition
void printvalues(int nvalue1, int nvalue2) { cout<<"1st : "<< nvalue1<< endl; cout<<"2st : "<< nvalue2<< endl; }I must say this site is awesome and clean looking..specially how the code is displayed….looks neat! keep up good work and i am doing last min craming when i googled default params…so wish me luck! :)
Hi Alex
Shouldn’t all the examples of pointers to chars in this chapter e.g.
char *strValue
be instead
char *pszValue?
Cheers
Kevin
Can i give d definition as:
void printvalues(int nvalue1=0, int nvalue2=2)
{
cout<<"1st : "<< nvalue1<< endl;
cout<<"2st : "<< nvalue2<< endl;
}
and call the function as:
printvalues(,3);
No you can’t.
[...] are not assignment operators. Those are default parameters for the [...]
[...] [...]