Locked learning resources

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

Unlock This Lesson

Locked learning resources

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

Unlock This Lesson

Step Through Complex Scripts

Now that you’ve seen how to step through a relatively simple script, you’ll see what it’s like to step through a script that’s a bit more complex. The example you’ll be looking at is a factorial program that is recursive, so it calls the same function again.

If you’re not sure what a recursive program is or how it works, then don’t worry. You don’t need to know any of that in order to follow along. Stepping through this example will help you understand recursion anyway!

00:01 Stepping Through More Complex Scripts. So as you’ve already seen, you can step through simpler scripts, but here’s something a little more complicated to step through. It’s a factorial program which is recursive, so it calls the same function again.

00:15 It’s going to call itself until it gets to this root base case where num is equal to 1. But if you’re not sure how this works, stick with it, because the way we’re going to look through it, you’ll hopefully understand it a little better because we’re actually going to run through each part of it.

00:32 So if you’re not sure what a recursive program is or how it works, hopefully this will give you a bit more understanding of it. And if you do know how recursive programs work, then watching this example will show you how it works in real depth with every step of the program being taken into account. Here we have our factorial program, which uses this recursive style of programming, where the factorial() function will call itself. So, at line 7 we call the factorial() function. Then it checks whether the number is 1, and if it isn’t 1, it calls itself with a lower number until eventually it gets to what’s called the base case, where, in this case, it returns a 1 and that would be returned to the previous calls of factorial(), and then we’ll see the chain execute upwards.

01:23 We’re going to run this and see these different factorial() functions get worked. We click the first time and it just looks at that function.

01:31 We’re going to step over that.

01:35 Now we’re going to step into this line 7, and we’ll see the elements of it.

01:40 You can see it’s assessing smaller and smaller parts of it, and it then gets bigger and it launches factorial(3). So here we have a window where it’s calling factorial(3), and if we expand it a little, we can see the local variable of num, which has been 3, which is passed into it. So here num is equal to 3.

01:59 And now if we step into this, it’s got this comparison here where it’s checking num is equal to 1. It substitutes num into there and then it checks whether 3 is equal to 1, which I think we mostly know the answer to.

02:13 So that becomes False, so it doesn’t run this part. It then runs the else part. So, we can see the logic being executed step-by-step.

02:23 And now it’s going to return num * factorial(), so it’s going to substitute num in there and also substitute num in there.

02:31 So, 3 - 1 is 2, we hopefully know. And now it’s going to call factorial(2), so we have another window open, which again we can just make that larger so you can see Local variables of num is 2. So this is a different num, but it’s the same thing again.

02:49 We run through that a little faster and we see, is 2 equal to 1? No!

02:54 So again, it’s going to run the else branch. It substitutes those numbers in, so 2 * factorial(2 - 1), in this case. So it’s going to get factorial(1). Now, when we call factorial(1) we get another new window. And again, we can see that the local num this time is 1 and we get to the appropriate point in the code using Step into. num, does 1 equal 1?

03:21 Well hopefully, we know the answer is yes, so that’s True. And now it returns a value of 1 rather than calling another function.

03:30 So now factorial(1) disappears and that is put back into here, so now it knows that the answer is 2 * 1, and then it’s going to return 2 * 1, which is 2, back to factorial(3).

03:44 And you can see that value has now been substituted in there, as it has been passed up to factorial(3).

03:51 And now it gets the value of 6, which is returned to it, so that gets returned to the print statement, so finally, it can print out 6 because it’s got those values back.

04:01 They’ve gone all the way down into factorial() and then come all the way back up, and now we have this concrete value of 6, which is the final value, which gets printed out. And there’s the final step in the program’s execution!

Become a Member to join the conversation.