Recursion in C, understand recursion example

后端 未结 6 1379
轮回少年
轮回少年 2021-01-27 20:30

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.

6条回答
  •  既然无缘
    2021-01-27 21:03

    Recursion is the process of repeating itself several times until the condition is true.

    Here the function recursion returns the value 3 if i>1 is false and if true it calls recursively.

    As i=9, first time it checks 9>1 is true. so, the function returns 9 - recursion(9/2=4) as i is int.

    then it calls recursion(4) 4>1 hence, returns 4 - recursion(4/2=2)

    again 2>1, returns 2 - recursion(1)

    again 1>1 is false, it returns 3 which should substitute in above value i.e., 2-3 = -1.

    then 4 - (-1) = 5

    9 - 5 = 4.

提交回复
热议问题