Is this declaration correct - void func(int a=7, int b) | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Is this declaration correct - void func(int a=7, int b)

1st Feb 2019, 6:38 AM
Shivansh Mathur
Shivansh Mathur - avatar
5 Answers
+ 6
No the declaration is not correct You cannot assign the default values from the left side unless you have assigned the value in right side. It is a syntax error. It is so, because suppose you have many arguments may be 10 then it will become much more complicated and you have to put sevral "," and which in turn increases the chances of error.
1st Feb 2019, 7:31 AM
Rohit
Rohit - avatar
+ 7
Can you use a default argument before a non-default argument in C++? 🤔
1st Feb 2019, 7:24 AM
Anna
Anna - avatar
+ 5
Just try it on the code playground
1st Feb 2019, 6:42 AM
ShortCode
+ 3
Thank you everyone, I got my answer
1st Feb 2019, 7:32 AM
Shivansh Mathur
Shivansh Mathur - avatar
+ 2
It's wrong. You can declare either int a=7, b; Or int a=7; int b;
1st Feb 2019, 7:13 AM
Calviղ
Calviղ - avatar