How to understand function recursion precisely?

前端 未结 4 1088
不知归路
不知归路 2020-12-03 13:08

I am currently programming some divide-conquer algorithms, where function recursions are used everywhere, but I have very vague idea or no idea how exactly it works, and tha

4条回答
  •  温柔的废话
    2020-12-03 13:43

    The key to understanding recursion is the concept of the call stack. The call stack consists of "frames". A stack frame contains a function's local variables and an invisible return address. The classic physical analogy is a stack of plates. When you make a function call a plate (stack frame) is added to the top of the stack. When you return from a function the top plate (stack frame) is removed. You can only use the plate (stack frame) that is on top.

    Recursive functions work the same way as ordinary functions. They are a little tricky because you can have multiple instances of their local variables on the stack at a given time. However, like other functions the function only refers to the stack frame that is on the top of the stack.

    To illustrate how this works let's walk through your program showing how the call stack grows and shrinks.

    Let's start with the base case: 0. Recursion(0);

    1. Enter main: The stack is empty: Bottom of stack->||<-Top of stack
    2. Recursion(0); Enter Recursion the stack has grown: Bottom of stack->|0|<-Top of stack
    3. cout << n << endl; The value of n is 0 so the output is "0"
    4. if (n > 0). 0 is not greater than 0 so Recursion(-1) is not called.
    5. cout << n << endl; The value of n is 0 so the output is "0"
    6. Return to main() the stack is empty again: Bottom of stack->||<-Top of stack

    The output would be

    0
    0
    

    Simple enough, no recursion took place. Let's take the next step. Recursion(1);

    1. Enter main: Bottom of stack->||<-The top of stack
    2. Recursion(1); Enter Recursion: Bottom of stack->|1|<-Top of stack
    3. cout << n << endl; The value of n is 1 so the output is "1"
    4. if (n > 0). 1 is greater than 0 so Recursion(0); is called.
    5. Enter Recursion: Bottom of stack->|1,0|<-Top of stack
    6. cout << n << endl; The value of n in this stack frame is 0 so the output is "0"
    7. if (n > 0). 0 is not greater than 0 so the function does not recurse.
    8. cout << n << endl; The value of n is 0 so the output is "0"
    9. Return to the first call to Recursion. Bottom of stack->|1|<-Top of stack
    10. cout << n << endl; The value of n is 1 so the output is "1"

    Output

    1
    0
    0
    1
    

    Let's go through the execution one final time with n == 2

    1. Enter main: Bottom->||<-Top
    2. Recursion(2); Enter Recursion: Bottom->|2|<-Top
    3. cout << n << endl; "2"
    4. if (n > 0). 2 is greater than 0 so Recursion(1); is called.
    5. Enter Recursion: Bottom->|2,1|<-Top
    6. cout << n << endl; "1"
    7. if (n > 0). 1 is greater than 0 so Recursion(0); is called.
    8. Enter Recursion: Bottom->|2,1,0|<-Top
    9. cout << n << endl; "0"
    10. if (n > 0). 0 is not greater than 0 so the function does not recurse again.
    11. cout << n << endl; "0"
    12. Return. Bottom->|2,1|<-Top
    13. cout << n << endl; "1"
    14. Return. Bottom->|2|<-Top
    15. cout << n << endl; "2"
    16. Return to main(). Bottom->||<-Top

    Output

    2
    1
    0
    0
    1
    2
    

提交回复
热议问题