Default Arguments in C++

22 Jun, 2022
A default argument is a value provided in a function declaration that is automatically assigned by the compiler if the calling function doesn’t provide a value for the argument. In case any value is passed, the default value is overridden. 
1) The 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 the 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 Codeint 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;}
25
50
80
Explanation: In statement 1, only two values are passed, hence the variables z and w take the default values of 0. In statement 2, three values are passed, so the value of z is overridden with 25. In statement 3, four values are passed, so the values of z and w are overridden with 25 and 30 respectively.
2) If function overloading is done containing the default arguments, then we need to make sure it is not ambiguous to the compiler, otherwise it will throw an error. The following is the modified version of the 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 Codeint 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 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 one default argument. The default constructor with argument has a default parameter x, which has been assigned a value of 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 the calling function provides values for them. For example, calling the function sum(10, 15, 25, 30) overwrites the values of z and w to 25 and 30 respectively.
When a function is called, the arguments are copied from the calling function to the called function in the order left to right. Therefore, sum(10, 15, 25) will assign 10, 15, and 25 to x, y, and z respectively, which means that only the default value of w is used.
Once a default value is used for an argument in the function definition, all subsequent arguments to it must have a default value as well. It can also be stated that the default arguments are assigned from right to left. For example, the following function definition is invalid as the subsequent argument of the 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 as we can do it just by adding another default argument to the function.
It helps in reducing the size of a program.
It provides a simple and effective programming approach.
Default arguments improve the consistency of a program.
Disadvantages of Default Arguments:
It increases the execution time as the compiler needs to replace the omitted arguments by their default values in the function call.
AmritangshuGhosh
__thanos__
bhanuprakash_p
anshikajain26
itskawal2000
janvist2
sriparnxnw7
CPP-Functions
C++
School Programming
CPP
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Vector in C++ STL
Initialize a vector in C++ (7 different ways)
std::sort() in C++ STL
Bitwise Operators in C/C++
unordered_map in C++ STL
Python Dictionary
Reverse a string in Java
Introduction To PYTHON
Interfaces in Java
Types of Operating Systems