Calling recursion from the stack?

  • 0
    Do I understand correctly, all recursion calls are pushed onto the stack and then after reaching the initial condition (exiting the recursion), the stack is called in reverse order to calculate the final result?

    For example, the recursive calculation of pow (2, 4) consists of steps:

    1. pow(2, 4) = 2 * pow(2, 3)
    2. pow(2, 3) = 2 * pow(2, 2)
    3. pow(2, 2) = 2 * pow(2, 1)
    4. pow(2, 1) = 2


    Is it correct that first pow is called from top to bottom, then when the value 1 is reached (exit from recursion), the compiler goes from bottom to top (from point 4. to 1.) substituting previously calculated values?

    Does recursion always work like this in JS?
    JavaScript Anonymous, Jun 3, 2019

  • 1 Answers
  • 0
    In JS, yes, every function call puts a return point on the stack.
    Anonymous

Your Answer
To place the code, please use CodePen or similar tool. Thanks you!