fastread homefastrread library fastread menu

C++ : Default Argument

Tutorial by:Manisha Dubey      Date: 2016-06-18 00:11:48

❰ Previous Next ❱

In C++ programming, you can provide default values for function parameters. The idea behind default argument is very simple. If a function is called by passing argument/s, those arguments are used by the function. But if all argument/s are not passed while invoking a function then, the default value passed to arguments are used. Default value/s are passed to argument/s in function prototype. Working of default argument is demonstrated in the figure below:

Working of default argument in C++

Example: Default Argument

/*  C++ Program to demonstrate working of default argument    */

#include <iostream>
using namespace std;

void display(char = '*', int = 1);

int main() {
    cout<<"No argument passed:\n";
    cout<<"\n\nFirst argument passed:\n";
    cout<<"\n\nBoth argument passed:\n";
    display('$', 5);

    return 0;

void display(char c, int n){
    for(int i = 1; i <=n; ++i) {


No argument passed:

First argument passed:

Both argument passed:



In the above program, you can see default value passed to arguments(in function prototype). At first, display() function is called without passing any arguments. In this case, default() function used both default arguments. Then, the function is called using only first argument. In this case, function does not use first default value passed. Function uses the actual parameter passed as first argument and takes default value(second value in function prototype) as it's second argument. When display() is invoked passing both arguments, default arguments are not used.

Note: The missing argument must be the last argument of the list, that is, if you are passing only one argument in the above function, it should be the first argument.

❰ Previous Next ❱


Submit Your Thought, Tutorial, Articls etc.

Submit Your Information India's Number one online promotion website