0
Recursion-outputQ
int boogie( int x, int y){ if(y==2) return x; else return boogie(x,y-1) + x; } What is returned by boogie(4,6)? //answer:20
1 Réponse
+ 1
Your boogie(... ) Function is recursive function and u passing two values 4 and 6 inside function body u have written if(y==2) then it will return 2 so everytime your else part will execute until this condition y==2 satisfy
in else part
return boogie (4, 6-1) + 4
Here again function called so first value not changing but second value 6 is decreasing by 1 on every call
so it will be like this
return boogie (4, 6-1) + 4
return boogie (4, 5-1) + 4 +4
return boogie (4, 4-1) + 4 +4 +4
return boogie (4, 3-1) + 4 +4 +4 +4
Now if condition will be true and return 4 so overall sum will be 20