C++ Programming Default Arguments (Parameters) with Example

C++ Programming Default Arguments (Parameters) with Example

In C++ programming, we can give function parameter default values in Default Arguments.

When a function that accepts default arguments is invoked without any additional arguments, the default settings are used.

The default arguments are, however, disregarded if additional arguments are given when calling the function.

Default Argument in C++ Programming, as an example Default Defenses

#include <iostream>
using namespace std;

// defining the default arguments
void display(char = '*', int = 3);

int main() {
    int count = 5;

    cout << "No argument passed: ";
    // *, 3 will be parameters
    display(); 
    
    cout << "First argument passed: ";
     // #, 3 will be parameters
    display('#'); 
    
    cout << "Both arguments passed: ";
    // $, 5 will be parameters
    display('$', count); 

    return 0;
}

void display(char c, int count) {
    for(int i = 1; i <= count; ++i)
    {
        cout << c;
    }
    cout << endl;
}

Output

No argument passed: ***
First argument passed: ###
Both arguments passed: $$$$$

This is how the program functions:

display() is called without any arguments being passed. In this instance, display() uses c = ‘*’ and n = 1, which are both standard parameters.

One parameter is passed when display(“#”) is called. The first here becomes a “#.” It still uses the second default argument, n = 1.

The call to show(“#”, count) includes both parameters. Default parameters are not applied in this situation.

The default arguments may also be specified within the function definition itself. The following program is an exact match for the previous one.

#include <iostream>
using namespace std;

// defining the default arguments
void display(char c = '*', int count = 3) {
    for(int i = 1; i <= count; ++i) {
        cout << c;
    }
    cout << endl;
}

int main() {
    int count = 5;

    cout << "No argument passed: ";
    // *, 3 will be parameters
    display(); 
    
    cout << "First argument passed: ";
     // #, 3 will be parameters
    display('#'); 
    
    cout << "Both argument passed: ";
    // $, 5 will be parameters
    display('$', count); 

    return 0;
}

This program operates as follows:

Output

No argument passed: ***
First argument passed: ###
Both arguments passed: $$$$$

Without receiving any inputs, display() is called. In this instance, show() utilises the default values for both c = ‘*’ and n = 1.

There is only one argument passed to show(‘#’). The first becomes “#” in this situation. The second default value, n = 1, is kept.

Both parameters are passed to display(‘#’, count). There is no usage of default parameters in this situation.

The default arguments can also be set in the declaration of the function itself. The software below is a direct substitute for the one above.

#include <iostream>
using namespace std;

// defining the default arguments
void display(char c = '*', int count = 3) {
    for(int i = 1; i <= count; ++i) {
        cout << c;
    }
    cout << endl;
}

int main() {
    int count = 5;

    cout << "No argument passed: ";
    // *, 3 will be parameters
    display(); 
    
    cout << "First argument passed: ";
     // #, 3 will be parameters
    display('#'); 
    
    cout << "Both argument passed: ";
    // $, 5 will be parameters
    display('$', count); 

    return 0;
}

Reminders for Default Arguments in C++ Programming

All succeeding parameters must have default values once a parameter’s default value has been specified. For instance,

// Invalid
void add(int a, int b = 3, int c, int d);

// Invalid
void add(int a, int b = 3, int c, int d = 4);

// Valid
void add(int a, int c, int b = 3, int d = 4);

The function must be specified before the function call if the default arguments are defined there rather than in the function prototype.

// Invalid code

int main() {
    // function call
    display();
}

void display(char c = '*', int count = 5) {
    // code
}

You may like:

Recursion In C++ Language (Basic Recursion Example)

https://theudaipurstore.com/maharana-pratap/

This Post Has 4 Comments

Leave a Reply