Skip to content
Related Articles

Related Articles

Default Arguments in C++

Improve Article
Save Article
Like Article
  • Difficulty Level : Basic
  • Last Updated : 26 May, 2022

A default argument is a value provided in a function declaration that is automatically assigned by the compiler if the caller of the function doesn’t provide a value for the argument with a default value. In case any value is passed the default value is overridden. 

1) Following is a simple C++ example to demonstrate the use of default arguments. Here, we don’t have to write 3 sum functions, only one function works by using default values for 3rd and 4th arguments. 

CPP




// CPP Program to demonstrate 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) //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

Explanation: In statement 1, only two values are passed, hence the variables z and w take the default values as 0. In statement 2, three values are passed, so the value of z is over-ridden with 25. In statement 3, four values are passed, so the value of z and w are over-ridden with 25 and 30 respectively.

2) When Function overloading is done along with default values. Then we need to make sure it will not be ambiguous. 
The compiler will throw an error, if ambiguous. Following is the modified version of above program,

CPP




// CPP Program to demonstrate Function overloading in
// 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)
{
    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;
}

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 can contain default parameters as well. A default constructor can either have no parameters or have parameters with default arguments.

C++




// CPP code to demonstrate use of 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
     
};

Explanation: Here, we see a default constructor with no arguments and a default constructor with default argument. The default constructor with arguments has a default parameter x which has been assigned a value 0. 

Key Points:  

  • Default arguments are different from constant arguments as constant arguments can’t be changed whereas default arguments can be overwritten if required.
  • Default arguments are overwritten when calling function provides values for them. For example, calling of function sum(10, 15, 25, 30) overwrites the value of z and w to 25 and 30 respectively.
  • During the calling of function, arguments from calling function to called function are copied from left to right. Therefore, sum(10, 15, 25) will assign 10, 15, and 25 to x, y, and z. Therefore, the default value is used for w only.
  • Once the default value is used for an argument in the function definition, all subsequent arguments to it must have a default value. It can also be stated as default arguments are assigned from right to left. For example, the following function definition is invalid as the subsequent argument of default variable z is not default.
// Invalid because z has default value, but w after it doesn't have a default value
int sum(int x, int y, int z = 0, int w).

Advantages of Default Arguments:

  • Default arguments are useful when we want to increase the capabilities of an existing function by adding another default arguments.
  • It helps in reducing the size of program.
  • It provides a simple and effective programming approach.
  • Default arguments improves consistency of program.

Disadvantages of Default Arguments:

  • It increase the execution time as compiler need to replace the omitted arguments by there default values in the function call.

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!