Given the following recursive function:
// Pre-condition: y is non-negative. int mysterious(int x, int y) { if (y == 0) return x; return 2*mysterious(x, y-1); }
What is the return value of the mysterious (3, 2)?
Here is my call stack:
return 2*mysterious(3, 2-1) => 2*3 => 6, 2*1 => mysterious(6,2) return 2*mysterious(6, 2-1) => 6*2 => 12, 2*2 => mysterious(12, 2)
But it looks like y will never reach 0. What am I doing wrong?
kachilous
source share