Default argument in C++

In this article, we are going to discuss the default argument in C++ which is an extensive feature available in C++, but not in C.
Submitted by Radib Kar, on July 11, 2020

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.

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

Output:

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

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.

But is 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

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT


Comments and Discussions!




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.