# Default Arguments in C++

## Default Arguments in C++ with Examples:

In this article, I am going to discuss Default Arguments in C++ Language with examples. Please read our previous article, where we discussed Function Template in C++ with examples.

##### Default Arguments in C++:

Let us understand default arguments in C++ with an example. We have taken two functions for the explanation,

Â  Â  Â  Â return x + y;
}
int add(int x, int y, int z){
Â  Â  Â  Â return x + y + z;
}

Here both the function names are â€˜addâ€™ but the first function is taking 2 integers and the second function is taking 3 integers. So, they are overloaded functions with the same name and a different number of arguments.

Now if we write â€˜add(2, 5)â€™ then it will call the first function and if we write â€˜add(3,4,5)â€™ then it will call the second function that is taking 3 arguments. Can we combine these two functions and write a single function? Let us see,

int add(int x, int y, int z = 0){
Â  Â  Â  Â return x + y + z;
}

Here we are taking the â€˜zâ€™ parameter as â€˜0â€™, what does it mean? This is the default argument. â€˜zâ€™ is a default argument. Here we have given some default values to the â€˜zâ€™ parameter. Now if we write â€˜add(2, 5)â€™ then this will call the above function with the parameter values as â€˜x = 2â€™, â€˜y = 5â€™ and â€˜z = 0â€™ as we have not mentioned â€˜zâ€™ in the call so it will take the default value.

And if we write â€˜add(2,5,7)â€™ then the values will be â€˜x = 2â€™, â€˜y = 5â€™ and â€˜z = 0â€™. So, this means the â€˜zâ€™ parameter is optional. It is up to the user. If the â€˜zâ€™ value is given by the user then it will take that otherwise it will take the default value that is â€˜0â€™ in this case.

So, we have combined the above two overloaded functions into one function. This is called the Default Argument in C++. So, assigning some default values to the argument is the default argument function and the benefit of a default argument is we can combine the overloaded function and write one single function. This is a very powerful feature of C++ and it is friendly for the programmer. Now one important thing about this one, suppose we have a function â€˜funâ€™,

int fun(int a, int b, int c, int d){
}

In this function, we can make â€˜dâ€™ as default parameter or â€˜câ€™ and â€˜dâ€™ as default parameter or â€˜bâ€™, â€˜câ€™ and â€˜dâ€™ as default parameter but we cannot make â€˜aâ€™ as default parameter and skip to â€˜bâ€™ making this as mandatory. This is not possible. We should start making the Default arguments from right to left without skipping any parameter. Now let us look at the complete program.

##### Program to understand Default Arguments in C++
```#include <iostream>
using namespace std;
int Sum(int x, int y, int z = 0)
{
return x + y + z;
}

int Max(int a = 0, int b = 0, int c = 0)
{
return a > b && a > c ? a : (b > c ? b : c);
}

int main()
{
cout << Sum(10, 20, 3) << endl;
cout << Sum(10, 20) << endl;
cout << Max(13, 22, 53) << endl;
cout << Max(22, 53) << endl;
return 0;
}
```

#### Default Arguments Frequently Asked Questions

No.

##### Default values should be filled from which side?

Default values for formal arguments must be foibles from the right side without skipping any parameter.