Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set your subtitle preferences in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please refer to our video player troubleshooting guide for assistance.

Maintaining State

In this lesson, you’ll learn two ways to maintain state in recursive functions:

  • Threading the state through recursive calls
  • Global state (not recommended)

00:00 Let’s talk about maintaining state. So, what does that mean? Well, when dealing with recursive functions, we have to keep in mind that each recursive call has its own execution context or its own frame. So to maintain state during recursion you have to either thread the state through the recursive calls, which means you add an argument to your function definition and pass the state in as an argument, and that means that the future recursive calls will have the current state as part of their execution context.

00:30 That’s what we did with factorial and with our Santa example. Or you could keep the state in the global scope. Here’s another example of threading the state.

00:41 Let’s say we want to write a function where we’ll sum the numbers between 1 and 10. So, 1 + 2 + 3 + 4 + 5, et cetera, + 10. Here we’ll have extra arguments to our function, the current number we’re on and the accumulated sum—so, the sum so far.

00:58 Here’s our base case. If the current number is 11, we’ve gone too far, so we should just return the sum that we’d been keeping track of. Otherwise, you call sum_recursive(), add 1 to the number, and add the current number to the sum.

01:14 And then when we call our function, we have to pass the initial state of the current number, which is 1, and our sum so far, which is 0.

01:23 So, how do we do this with the global state? Well, we’d create two global variables—the same variables as our function, current_number and accumulated_sum. We define our function, no variables this time. We call global on these two variables so that we’re allowed to mutate the global variables.

01:42 Otherwise, we wouldn’t be able to change those global variables.

01:47 We have our base case, same base case.

01:51 Then our recursive case is a little interesting. Instead of just calling our function, we have to add to both of those variables. And then we call sum_recursive().

02:04 We don’t have to pass in any initial state, and we get the same answer.

02:11 There’s a warning that global states are usually not recommended. It’s not good coding style to create a ton of global variables and modify them in all your functions. There are a couple downsides. For example, if another function wants to use the same named variable, it can’t do it.

02:28 There are a couple others, but this is just to show you that it is possible, and to help you think about recursion in two different ways.

Become a Member to join the conversation.