Default Arguments in C++

By IncludeHelp Last updated : November 04, 2023

Default arguments are the arguments that are passed in the function definition which is used by the compiler if no arguments are provided, at the time of function call.

Default argument value

To define the default value to an argument, you can assign the value by using the equals sign (=).

Example 1

Below is the example of default argument:

#include <bits/stdc++.h>
using namespace std;

void print(int a, int b = 1, int c = 2, int d = 3)
{
    cout << "a: " << a << endl;
    cout << "b: " << b << endl;
    cout << "c: " << c << endl;
    cout << "d: " << d << endl;
}

int main()
{
    int a = 12, b = 13, c = 14, d = 15;
 
    //first function call
    cout << "only argument a passed\n";
    print(a);
 
    //second function call
    cout << "only arguments a,b passed\n";
    print(a, b);
 
    //third function call
    cout << "only arguments a,b,c passed\n";
    print(a, b, c);
 
    //fourth function call
    cout << "All arguments a,b,c,d passed\n";
    print(a, b, c, d);

    return 0;
}

The output of the above program is:

only argument a passed
a: 12
b: 1
c: 2
d: 3
only arguments a,b passed
a: 12
b: 13
c: 2
d: 3
only arguments a,b,c passed
a: 12
b: 13
c: 14
d: 3
All arguments a,b,c,d passed
a: 12
b: 13
c: 14
d: 15

Explanation

  • In the above example, we have used the default argument concept. This is an instance of operator overloading, where the resolution is done during the compile time.
  • So in case of the first function call, we only passed a single argument in the calling function which was a and the other arguments b, c, d took its default value.
  • In the case of the second function call, we only passed two arguments in the calling function which was a, b, and the other arguments c, d took its default value.
  • In the case of the third function call, we passed three arguments in the calling function which was a, b, c, and the other argument d took its default value.
  • In the case of the fourth function call, we passed all four arguments in the calling function thus no default argument value is taken.

Example 2

In this example, we have 3 arguments in which b and c have the default values.

// C++ program to demonstrate Default Arguments
#include <iostream>
using namespace std;

// Defining a function having default values for
// last two arguments b and c
// thus, this function can be called with
// one, two, or three arguments
int findSum(int a, int b = 0, int c = 0) { 
    return a + b + c; 
}

// Main function
int main() {
  // Calling function with 1 argument
  cout << findSum(10) << endl;
  // Calling function with 2 arguments
  cout << findSum(10, 20) << endl;
  // Calling function with 3 arguments
  cout << findSum(10, 20, 30) << endl;
  return 0;
}

The output of the above program is:

10
30
60

Rules of defining default arguments

In the case of defining default arguments, we need to follow few rules so that compiler doesn't find the function overloading ambiguous.

  1. In the function definitions, if we declare default argument for one variable, then all the variables after that need to have default arguments.
    Like below is not a valid function definition using default arguments and will throw compilation error,
    int func(int a, int b=0, int c)
    //This will throw error as b has assign default argument 
    //value but c hasn't been though c comes after b.
    
    //This it should be declared like:
    int func(int a, int c ,int b=0)
    
  2. The default value for arguments are being copied if the argument value is provided while calling the function.
  3. It shouldn't lead to ambiguity which will lead to a compilation error.

Related Tutorials

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.