Related Articles

Related Articles

Some Interesting facts about default arguments in C++
  • Difficulty Level : Medium
  • Last Updated : 15 Jun, 2017

Predict the output of following C++ programs.

1)

filter_none

edit
close

play_arrow

link
brightness_4
code

#include <iostream>
void init(int a=1, int b=2, int c=3);
  
int main()
{
    init();
    return 0;
}
void init(int a=1, int b=2, int c=3)
{
    std::cout << a << ' ' << b << ' ' << c;
}

chevron_right


The above program looks correct at first glance but will fail in compilation. If function uses default arguments then default arguments can’t be written in both function declaration & definition. It should only be in declaration, not in definition.

The following program is now correct.

filter_none

edit
close

play_arrow

link
brightness_4
code

#include <iostream>
void init(int a=1, int b=2, int c=3);
int main()
{
    init(); // It is fine
    return 0;
}
void init(int a,int b,int c)
{
    std::cout << a << ' ' << b << ' ' << c;
}

chevron_right


2)



filter_none

edit
close

play_arrow

link
brightness_4
code

#include <iostream>
  
// something looks missing
void init(int =1, int =2, int =3);
  
int main()
{
    init();
    return 0;
}
  
void init(int a, int b, int c)
{
    std::cout << a << ' ' << b << ' ' << c;
}

chevron_right


If you closely observe function prototype then it looks like an error but it isn’t actually. Variable names can be omitted in default arguments.

This article is contributed by Pravasi Meet. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

Attention reader! Don’t stop learning now. Get hold of all the important C++ Foundation and STL concepts with the C++ Foundation and STL courses at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up
Recommended Articles
Page :