i am having trouble understanding this example. I cant figure out what actually happens after a certain point.
Here is the code, the result is supposed to be 4.
I can see that it calls itself several times but how it actually comes to the result of 4 eludes me entirely. Any help would be greatly appreciated.
#include <stdio.h>
int recursion(int i)
{
return (i>1 ? i - recursion(i/2) : 3);
}
int main()
{
int number = 9;
printf("The result is %d\n", recursion(number));
return 0;
}
edit:
Thanks so much, that clears it up!
Here from code,
recursion(1) = 3andi/2wheni>1,9/2 = 4(since int as parameter)The base condition of this recursive function is when
![Recursion explained myself with a diagram]](https://i.stack.imgur.com/hrrpG.jpg)
i = 1