Default Arguments or Parameters in C++


August 31, 2022, Learn eTutorial
945

In this tutorial, with the help of some examples, let us learn about C++ default arguments and how they work.

We could provide default values mainly for the function parameters in C++ programming. suppose you call a function with default arguments without passing any arguments, then the default parameters are used. If arguments are passed to the function while it is being called, then the default arguments will be ignored.

If the calling function fails to supply a value for the argument, the compiler will automatically assign the value specified in the default argument provided in the function declaration and the default value is overridden if any value is passed.

Working of the default arguments in C++

Now let us see the working of the default arguments in C++. Here we are considering four cases :

  1. No Argument is Passed
    Default Arguments

    Both of the default parameters are used by the function test() when it receives a call.

  2. All arguments are passed
    Default Arguments

    When the call to test(3, 3.6) is made, the default values for both parameters are overridden and resulting in i= 3 and the f = -3.6.

  3. Only the first argument is passed
    Default Arguments

    When calling test(3), the first argument is changed to 3 and the second parameter is set to its default value.

  4. Only the second argument is passed
    Default Arguments

    The function behaves inappropriately when temp(3.6) is passed because without passing the first argument the second argument cannot be passed.

    As a result, 3.6 is used as the first argument. Because the first argument is an int, the value which is actually passed is 3.

An example for default argument


#include <iostream>

using namespace std;

// first define the default arguments
void display(char = '*', int = 3);

int main() {
    int count = 5;

    cout << "No argument passed: ";
    // *, 3 will be parameters
    display();

    cout << "First argument passed: ";
    // #, 3 will be parameters
    display('#');

    cout << "Both arguments passed: ";
    // $, 5 will be parameters
    display('$', count);

    return 0;
}

void display(char c, int count) {
    for (int i = 1; i <= count; ++i) {
        cout << c;
    }
    cout << endl;
}

Output:

No argument passed: ***
First argument passed: ###
Both arguments passed: $$$$$

Let us see how the above program works 

  • display() is called without any arguments being passed. The default values for c = '*' and n = 1 are used in this instance by display()
  • display('#') is invoked with a single argument. In this case, the first character is changed to '#'. The second default parameter, n = 1, remains unchanged.
  • With both arguments, display('#', count) is called and the default arguments are not used in this case.

1)    A very simple example of  C++  that really shows how to use default arguments. in this case, we don't need to write three sum functions because only one function will be working by using the default values for the third and fourth arguments.


// CPP Program mainly to demonstrate the  Default Arguments
#include <iostream>

using namespace std;

// A function with default arguments,
// it can be called with
// 2 arguments or 3 arguments or 4 arguments.
int sum(int x, int y, int z = 0, int w = 0) //here we will be assigning default values to z,w as 0
{
    return (x + y + z + w);
}

// Driver Code
int main() {
    // Statement 1
    cout << sum(10, 15) << endl;

    // Statement 2
    cout << sum(10, 15, 25) << endl;

    // Statement 3
    cout << sum(10, 15, 25, 30) << endl;
    return 0;
}

Output:

25
50
80

Simple explanation: Because only two values are passed in statement 1, the variables z and w have default values of 0. Statement 2 passes three values, so the value of z is overridden with the number 25. The values of z and w are replaced with 25 and 30, respectively, in statement 3 because four values are passed.

2)    If we overload a function with default arguments, we must ensure that they are not ambiguous to the compiler, or it will definitely throw an error. The modified version of the above program is as follows:


#include <iostream>

using namespace std;

//A function that accepts default arguments and can be called with
// 2 arguments or 3 arguments or 4 arguments.
int sum(int x, int y, int z = 0, int w = 0) {
    return (x + y + z + w);
}
int sum(int x, int y, float z = 0, float w = 0) {
    return (x + y + z + w);
}
// Driver Code
int main() {
    cout << sum(10, 15) << endl;
    cout << sum(10, 15, 25) << endl;
    cout << sum(10, 15, 25, 30) << endl;
    return 0;
}

Output:

Error
prog.cpp: In function 'int main()':
prog.cpp:17:20: error: call of overloaded 
'sum(int, int)' is ambiguous
  cout << sum(10, 15) << endl; 
                    ^
prog.cpp:6:5: note: candidate: 
int sum(int, int, int, int)
 int sum(int x, int y, int z=0, int w=0) 
     ^
prog.cpp:10:5: note: candidate: 
int sum(int, int, float, float)
 int sum(int x, int y, float z=0, float w=0)

3. A constructor may also include default parameters. Either no parameters or parameters with default arguments are possible for a default constructor.

Code in C++ that shows how to use default arguments in constructors.

#include <iostream>
using namespace std;
class A {
public:
    int sum = 0;
    A(); // default constructor with no argument
    A(int x = 0); // default constructor with one
                        // arguments
    
};

Simple Explanation: The default constructors displayed here have no arguments, and one of them has a single default argument. The default constructor with the argument has a default parameter x with a value of 0.

Benefits of Default Arguments:

  • When we want to expand the capabilities of an existing function, we can do so simply by adding another default argument to the function.
  • It helps in the reduction of program size.
  • It offers a straightforward and efficient programming approach.
  • Using default arguments improves a program's consistency.

Default Arguments Have the Following Drawbacks:

  • It lengthens the execution time because the compiler must replace the omitted arguments in the function call with their default values.