Default Arguments in C++ (original) (raw)
Last Updated : 14 May, 2025
A **default argument is a value provided for a parameter in a function declaration that is automatically assigned by the compiler if no value is provided for those parameters in function call. If the value is passed for it, the default value is overwritten by the passed value.
**Example:
C++ `
#include using namespace std;
// Function with an argument with default value void f(int a = 10) { cout << a << endl; }
int main() {
// Uses default argument
f();
// Uses passed value
f(221);
return 0;
}
`
**Explanation: In this program, the function f has a default argument **a = 10, so when no argument is provided, **a defaults to **10, which is printed. When **221 is passed, **a becomes **221 which is then printed.
Syntax
A default argument is defined by assigning a value to a function parameter in its declaration.
C++ `
return_type name (p1= v1, p2= v2, ...);
`
where **v1, v2, ... are the default values for the parameters **p1, p2, ... respectively.
Rules to Follow
There are some important rules and best practices to keep in mind when using default arguments in C++:
**1. Default Values Must Be Specified in Function Declarations
The default values for parameters must be specified in the function declaration (or prototype). If a function is declared and defined separately, the default values must be in the declaration, not in definition.
C++ `
// Declaration with default argument void func(int x = 10);
// Definition without default argument void func(int x) { cout << "Value: " << x << endl; }
`
**2. Default Arguments Cannot Be Modified
Once default arguments are defined in the declaration, they cannot be modified in the function definition. If you try to change the default value in the definition, it will result in a compilation error.
C++ `
// Declaration void f(int a = 10);
// This definintion will throw and error void f(int a = 222) { // statements }
`
**3. Default Arguments Must Be Provided from Right to Left
In a function with multiple parameters, default values must be provided from the rightmost parameter to the left. It means that if a parameter has a default argument, all parameters to its right must also have default values.
C++ ``
// Valid void func(int x, int y = 20);
// Invalid, as y
does not have a default value
void func(int x = 10, int y);
``
**4. Ambiguity in Function Overloading
If a function containing default arguments is overloaded, then we need to make sure it is not ambiguous to the compiler, otherwise it will throw an error. For example,
C++ `
// Valid void f(int a = 10, int b = 20);
// Will throw error as the signature is same void f(int a = 22, int b = 2);
// Will also throw error void f(int a);
// Will also throw an error void f(int a, b)
`
Examples
The following examples demonstrate the use of default arguments in different cases:
Find Area of Rectangle with Optional Length
We want to keep the rectangle height optional in case it is not provided to the area calculator function. One approach could be to create two overloaded functions, one that takes two parameters and one that takes one. However, this can be simplified by using default arguments instead of function overloading by specifying optional values.
C++ `
#include using namespace std;
// Function with default height 'h' argument double calcArea(double l, double h = 10.0) { return l * h; }
int main() { cout << "Area 1: "<< calcArea(5) << endl;
cout << "Area 2: "<< calcArea(5, 9);
return 0;
}
`
Output
Area 1: 50 Area 2: 45
Combine Default and Parameterized Constructor
Just like normal functions, we can also define default values for the arguments of parameterized constructors. All the rules of the default arguments will be applied to these parameters.
C++ `
#include using namespace std;
class A { public: int data;
// Parameterized constructor with default values
A(int x = 5) { data = x; }
};
int main() {
A a1; // Will not throw error
A a2(25);
cout << a1.data << endl;
cout << a2.data;
return 0;
}
`
**Explanation: In the above program, we create a class with a parameterized constructor that has a default argument. When we create an object without passing any argument, the constructor uses its default value of x = 5. However, when we pass an argument, the constructor uses the passed value of x = 25.
Advantages
- Default arguments are useful when we want to increase the capabilities of an existing 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
- If the default values are not well-documented or understood, it can lead to confusion about what arguments are being used.
- Overloading functions with default arguments can sometimes lead to ambiguities.
- It increases the execution time as the compiler needs to replace the omitted arguments by their default values in the function call.