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

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
{"ft_lang_label":"__label__en","ft_lang_prob":0.7207314,"math_prob":0.8466273,"size":2823,"snap":"2020-45-2020-50","text_gpt3_token_len":740,"char_repetition_ratio":0.21000354,"word_repetition_ratio":0.103053436,"special_character_ratio":0.28161532,"punctuation_ratio":0.1629393,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9746218,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2020-10-28T07:55:58Z\",\"WARC-Record-ID\":\"<urn:uuid:d01d86a9-b45f-4cb1-8869-292c160027c0>\",\"Content-Length\":\"101743\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:b0abee69-7638-4897-9c0a-d36f1ad3042b>\",\"WARC-Concurrent-To\":\"<urn:uuid:b64d9207-e1af-4a1b-b6e5-9bb331db8269>\",\"WARC-IP-Address\":\"104.26.12.251\",\"WARC-Target-URI\":\"https://www.includehelp.com/cpp-tutorial/default-argument.aspx\",\"WARC-Payload-Digest\":\"sha1:OV5YZMLJSPTWSMVYQ5VINHYLUGVLLFQU\",\"WARC-Block-Digest\":\"sha1:3MGZDPWLBS44OO7EDZY3LEUDQALMLT65\",\"WARC-Identified-Payload-Type\":\"application/xhtml+xml\",\"warc_filename\":\"/cc_download/warc_2020/CC-MAIN-2020-45/CC-MAIN-2020-45_segments_1603107897022.61_warc_CC-MAIN-20201028073614-20201028103614-00418.warc.gz\"}"}