In the previous exercise, we used an iterative function to implement how a call stack accumulates execution contexts during recursive function calls.
We’ll now address the conclusion of this function, where the separate values stored in the call stack are accumulated into a single return value.
Instructions
This is the point in a recursive function when we would begin returning values as the function calls are “popped” off the call stack.
We’ll use another while
loop to simulate this process. Write the while
loop below the “Base Case Reached” print
statement.
This loop will run as long as there are execution contexts stored in call_stack
.
Inside this second loop:
- declare the variable
return_value
- assign the last element in
call_stack
toreturn_value
.- Remove that value from
call_stack
otherwise you’ll have an infinite loop!
- Remove that value from
Print call_stack
to see how the execution contexts are removed from call_stack
.
Print that you’re adding return_value["n_value"]
to result
and their respective values.
Finish the loop by retrieving "n_value"
from return_value
and add it to result
.