7.7 — Default parameters

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:

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:

A function can have multiple default parameters:

Given the following function calls:

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:

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:

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:

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
7.6 -- Function overloading

11 comments to 7.7 — Default parameters

  • kekie

    Shouldn't they be called 'Default arguments'?

  • Sphingine

    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:


  • otocan

    Hi Alex

    Shouldn't all the examples of pointers to chars in this chapter e.g.

    char *strValue

    be instead

    char *pszValue?



  • dave

    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 wish me luck! :)

  • simon1122


    can any one tell me what is wrong with this one??

    thak you

    • Anshuman

      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;
  • simon1122

    using namespace std;

    void printvalues(int nvalue1, int nvalue2=10 );

    int main()


    /*scaffolding code for testing purpose*/
    cin.ignore(256, '\n');
    cout<< "press Enter to continue.." << endl;

    return 0;

    void printvalues(int nvalue1, int nvalue2 = 10 )

    cout<<"1st : "<< nvalue1<< endl;
    cout<<"2st : "<< nvalue2<< endl;


  • Ericc

    u forgot ; in the Prototype

  • Pathik

    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;
    		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.

Leave a Reply

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">