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.
Now let us see the working of the default arguments in C++. Here we are considering four cases :
Both of the default parameters are used by the function test() when it receives a call.
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.
When calling test(3), the first argument is changed to 3 and the second parameter is set to its default value.
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.
#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
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.