From the course: Python: Recursion

Unlock the full course today

Join today to access over 24,800 courses taught by industry experts.

Winding and unwinding the call stack in recursion

Winding and unwinding the call stack in recursion - Python Tutorial

From the course: Python: Recursion

Winding and unwinding the call stack in recursion

- [Instructor] This is a good time to look in a bit more detail at what happens when a recursive function is called. When a function is called in Python a stack frame is allocated to store the local variables for that function. In a recursive function, each recursive call adds a stack frame to the called stack until we reached the base case. This part of the process is often called a winding. Once the base case is reached the stack begins to unwind as each call returns its results. Let's look at how this works with our factorial function. On the slide you can see that the factorial recursive function has been called with the argument of five. And then what happens is that that function call, again, calls factorial recursive with the decreased argument of four. And notice at this stage, nothing has been returned by the first function. Then factorial four, calls factorial three, and factorial three calls factorial two,…

Contents