Loading...
C++ Default Arguments

C++ Default Arguments

In this tutorial, you will learn about different approaches you can take to solve a single problem using functions.


Default Arguments

  • In C++ programming, we can provide default values for function parameters.
  • A default argument is a value provided in a fuction declaration that is automatically assigned by the compiler if the caller of the function doesn't provide a value for the argument with a default value.
  • A default argument is a function argument that has a default value provided to it.
  • If the user does not provide a value for this argument, the default value will be used.
  • If the user does not provide a value for the default argument, the user- supplied value is used.

If a function with default arguments is called without passing arguments, then the default parameters are used.

However, if arguments are passed while calling the function, the default arguments are ignored.


Example 1: Default Argument

// This program to check if you are 18 or not using goto statement.

# include <iostream>
using namespace std;
                   
// defining the default arguments
void character(char = 'a', int = 3);
                                  
int main() {
    int count = 4;
                                  
    cout << "No argument passed: ";
    // a, 2 will be parameters
    character(); 
                                      
    cout << "First argument passed: ";
     // b, 3 will be parameters
    character('b'); 
                                      
    cout << "Both arguments passed: ";
    // c, 4 will be parameters
    character('c', count); 
                                  
    return 0;
}
                                  
void character(char d, int count) {
    for(int i = 1; i <= count; ++i)
    {
        cout << d;
    }
    cout << endl;
}

Output 1

No argument passed: aaa
First argument passed: bbb
Both arguments passed: cccc

Here is how this program works:

  1. character() is called without passing any arguments. In this case, character() uses both the default parameters d = 'a' and n = 1.
  2. character('c') is called with only one argument. In this case, the first becomes 'c'. The second default parameter n = 1 is retained.
  3. character('c', count) is called with both arguments. In this case, default arguments are not used.

We can also define the default parameters in the function definition itself. The program below is equivalent to the one above.


Example 2: Function with No arguments passed but a return value

#include <iostream>
using namespace std;
// defining the default arguments

void character(char d = 'a', int count = 3) {
for(int i = 1; i <= count; ++i) {
        cout << d;
    }
        cout << endl;
}
                              
int main() {
    int count = 4;
                                  
    cout << "No argument passed: ";
    // a, 2 will be parameters
    character(); 
                                      
    cout << "First argument passed: ";
    // b, 3 will be parameters    
    character('b'); 
                                      
    cout << "Both argument passed: ";
    // c, 4 will be parameters
    character('c', count); 
                                  
    return 0;
}

Output 1

No argument passed: aaa
First argument passed: bbb
Both arguments passed: cccc

Key Points

  • Default arguments are different from constant arguments as constant arguments can't be changed whereas default arguments can be overwritten if required.
  • Default arguments are overwritten when calling function provides values for them.
  • During calling of function, arguments from calling function to called function are copied from left to right.
  • Once default value id used for an argument in function definition, all subsequent arguments to it must have default value.
  • It can also be stated as default arguments are assigned from right to left.

Important Points

  1. Once we provide a default value for a parameter, all subsequent parameters must also have default values. For example,
    // Invalid
    void sum(int a, int b = 2, int c, int d);
                          
    // Invalid
    void sum(int a, int b = 2, int c, int d = 4);
                          
    // Valid
    void sum(int a, int c, int b = 2, int d = 4);
  2. If we are defining the default arguments in the function definition instead of the function prototype, then the function must be defined before the function call.
    // Invalid code
                          
    int main() {
    // function call
    print();
    }
    
    void print(char c = '@', int count = 4) {
    // code
    }

Some Valid/Invalid default values are:

int sum (int a, int b, int c) Valid/Invalid
int sum (int a, int b=2, int c=3); Valid
int sum (int a=1, int b=2, int c=3); Valid
int sum (int a=1, int b, int c); Invalid
int sum (int a=1, int b, int c=3); Invalid

Next Tutorial

We hope that this tutorial helped you develop better understanding of the concept of Default Arguments in C++.

Keep Learning : )

In the next tutorial, you'll learn about C++ Storage Class.

- Related Topics