0
Why is there any output at all, and why is it: 0,1,2? How does it count up again?
3 odpowiedzi
+ 4
It has an output, because there is a printf function.
It outputs in this order, because it runs the function first, before printing it. So it could be written like this:
fun(--3);
printf("%d", n);
->
fun(--2);
printf("%d", n);
printf("%d", n);
->
fun (--1);
printf("%d", n);
printf("%d", n);
printf("%d", n);
->
printf("%d", n); //Subtracted 1 3 times -> 3 - 3 = 0
printf("%d", n); //Subtracted 1 2 times -> 3 - 2 = 1
printf("%d", n); //Subtracted 1 once -> 3 - 1 = 2
(It has different values, because n is not static, so outside of the scope it won't exist)
+ 1
I didn't know that a called function (In this case called several times) actually returnes to the rest of the code within and executes it, that wasn't in the first place, when it was let out before through a call of itself.
I'm a little wiser now. :)
Thank you all for clarifying this.