The Wayback Machine - https://web.archive.org/web/20250215184843/https://www.geeksforgeeks.org/default-arguments-c/
Open In App

Default Arguments in C++

Last Updated : 11 Jan, 2025
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Share
Report
News Follow

A default argument is a value provided for a parameter in a function declaration that is automatically assigned by the compiler if the calling function doesn’t provide a value for those parameters. If the value is passed for it, the default value is overridden by the passed value.

Let’s take a look at an example:

C++
#include <iostream>
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;
}

Output
10
221

Explanation: In the first call, we passed no value to the function, so the default value is used. In the second, we passed a value which is then used in the function.

This article explains the basics of default arguments, its usage in various common cases, and also answers some commonly asked queries:

Syntax of Default Arguments

A default argument is defined by assigning a value to a function parameter in its declaration.

return_type name (p1= v1, p2= v2, …);

where v1, v2 and more are the default values for the parameters p1, p2 and so on.

Using default arguments can simplify your function calls.

Rules for Using Default Arguments

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, 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. 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)

Example of Default Arguments

The following examples demonstrate the use of default arguments in different cases:

Function to Area of a Rectangle with Optional Height without Function Overloading

The default arguments help in simplifying the function overloading by specifying optional values.

C++
#include <iostream>
using namespace std;

// Function with default height 'h' argument
double calcArea(double l, double h = 10.0) {
    return l * h;
}

int main() {
  
      // Uses default height
    cout << calcArea(5) << endl;
  
      // Uses custom height
    cout << calcArea(5, 7);
    return 0;
}

Output
50
35

Add Default and Parameterized Constructor in a Single Function

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 <iostream>
using namespace std;

// class
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;
}

Output
5
25

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

  • 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.

Default Arguments in C++ – FAQs

Can default arguments be specified in the function definition?

No, default values should only appear in the declaration.

Can default arguments be specified in the function definition?

No, default values should only appear in the declaration.

What happens if a function with default arguments is called with extra arguments?

If you provide more arguments than the function expects, the provided arguments are used for the corresponding parameters, and the remaining parameters use their default values.

Can default arguments be used with overloaded functions?

Yes, but be cautious of ambiguity. Overloaded functions with default arguments can cause confusion if the compiler can’t determine which version to call.

What happens when default arguments are used in virtual functions in C++?

Default arguments can be used in virtual functions. However, when a derived class overrides a base class function, the default arguments of the base class are not inherited. If the derived class provides new default arguments, it may lead to different behaviour.

Can default arguments be used for template parameters?

Yes, default arguments can be specified for template parameters as well.



Next Article
Practice Tags :

Similar Reads

three90RightbarBannerImg