Recursion in C, understand recursion example

后端 未结 6 1332
轮回少年
轮回少年 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:04

    The recursion function can be written in a less compact fashion this way:

    int recursion(int i) 
    { 
        if(i>1)
        {
            int j;
            j = i - recursion(i/2); // note here that the function recall itself
                                    // note also that as i is integer the function
                                    // will be invoked with i/2 rounded to the lower int value
            return j;
        }
        else
        {
            return 3;
        }
    }
    

    hope this helps...

提交回复
热议问题