Using Default Arguments | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Using Default Arguments

What is the output of this code? int func(int a=2, int b=5) { return b-a; } int main() { int k=7; cout<<func(k); } From my understanding, we have to put the return formula b-a (something minus something) into use. But the question is, how can one know where to plug in the k? How would one know whether to do 5-7 or 2-7? Is it that for addition and subtraction we plug from the right, while for multiplication and division we plug from the left? Probably not right?

19th Jul 2020, 6:26 AM
Solus
Solus - avatar
2 Answers
+ 4
Solus Arguments follows rule in sequence order (In all languages). So if you defined function with more parameters but passing less parameters then extra parameters will use default value. So according to your problem here the value of k will be assign to first parameter a (because of order) and parameter b will use default value. So here a will be 7 and b will be 5.
19th Jul 2020, 8:00 AM
A͢J
A͢J - avatar
0
Here k is copied on a of function header and default value of b is 5 . So 7-5 That's output 2
19th Jul 2020, 6:43 AM
Sâñtôsh
Sâñtôsh - avatar