C/C++ function recursion
c, c++, recursion
Solution
Recursion will proceed as follow:
Func(x = 50, y = 10) x >= y
Func(x = 40, y = 10) x >= y
Func(x = 30, y = 10) x >= y
Func(x = 20, y = 10) x >= y
Func(x = 10, y = 10) x >= y
Func(x = 0, y = 10) x < y
return 0
return Func(x = 0, y = 10) + 1 = 1
return Func(x = 10, y = 10) + 1 = 2
return Func(x = 20, y = 10) + 1 = 3
return Func(x = 30, y = 10) + 1 = 4
return Func(x = 40, y = 10) + 1 = 5
Where `+1` is used to say: "add 1 to the result computed by `Func(x - y, y)` in a recursive call".
Problem
I have been trying to understand the below code snippet,but not quite getting the logic behind the output. ``` int Func(int x, int y){ if (x < y) return 0; else return Func(x - y, y) + 1; } int main() { int x = 50, y=10; printf("%d \n",Func(x,y)); return 0; } ``` The output for the above program is obviously 5. Can anyone tel me what `"+1"` (in `return Func(x - y, y) + 1;`) in the recursion type method actually means and how it have a execution flow.? If i just execute `return Func(x-y,y);` then the output is 0, that is fine. But why the output is 5 in the first case?