Function Default Parameters

Introduction

  • A default parameter is a function parameter that has a default value provided to it.
  • Default parameters are a good option when the function needs a value that the user may not want to override.
  • All default parameters must be the rightmost parameters

Examples

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
}
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-Share Alike 2.5 License.