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?
2 Respostas
+ 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.
0
Here k is copied on a of function header and default value of b is 5 .
So 7-5
That's output 2