 Welcome to Intro to AP Computer Science for New AP Teachers. The topic today is recursion. Now don't let that scare you. So we're going to be looking at what is recursion. How can we explain that to students? The way that hopefully makes a little bit of sense. We'll look at the basic structure of a recursive function, a recursive method I should say here, and then we'll talk about the recursion depth limit. There is a limit to the number of times that a function can call itself. If you hit that limit something's gone wrong for sure. I find it's pretty good to compare at least initially recursion to loops. It's anything that you can do recursively. You can usually do with a loop. It's just a lot harder in those types of cases. Look a little bit at returning values from recursion. For example, the example would be the Fubonacci sequence. And then a little bit of an introductory exercise that you might want to try after explaining some of these things. So what is recursion? The basically kind of dictionary definition would be a way of solving coding problems by subdividing problems into smaller versions of themselves, which doesn't really help a lot. But on a practical level, it's where a method calls itself until a particular condition is met. That's something students can kind of wrap their heads around. So the basic structure is you have a method of some sort. In this case, it's going to be a static method. And you're passing some argument into it could be a counter or it could be some value. And then you have what's called a base case, which I'll explain here in a minute or two. And then there's always a recursive call. So this is where you see, you know, this method is calling itself. And so like you have this argument here. So let's say this is x. And then down here, this would be like x plus one or x minus one. Now, depending on the problem, it could be very different. But if this is x and this is x, you're going to just get stuck and stuck in a loop. It's never going to end. Don't hit that recursion death limit. So what you'll see is that some sort of value is usually passed in. And then when you when you call that method again, that value is going to change a little bit. Again, if it doesn't, you're going to run into that limit. So now the recursion death limit, I mentioned a little too soon. So the recursion death limit, there is a limit to the number of times a method can call itself. And my computer was around 12,000, I think. Check my book, the exact number was there. But that probably varies by system or settings, I would imagine. So, you know, here's an example of a problem that will hit the recursive depth limit. So you'll see public static void print num int num. So we print the number. And then we call the function again, but num plus one. So let's say it was zero, one, two, three. And then here's the method call. So you can see here, it's just going to keep going numbers going to keep going up and up and up and up and up. But up, there's no way for this to stop. Stop. Because there's no there's no what we call the base case. So to avoid it, we add a base case. And this is where this is where the recursion stops. We've hit whatever that limit is, we want to hit. So in this case, I could say public static void print num int num. And then we print the number. And then our base case is if it's less than three. So I could have done if num is not equal to three, but this makes probably makes a little bit more sense. So we're going to print num plus one. So if we go in here, this is zero. So we print zero, zero is less than three. One. Print one, one is less than three. Two, two is less than three. And then we get to three. Three is not less than three. The method ends. And so we actually would never get to three because it's less than. And then, you know, so those types of examples are pretty easy. And that's where my introductory activity is. Take some loops and then rewrite them with recursive style. But where it gets a little bit harder, that's right, I'm gonna get ahead of myself again. So in the comparison to loops, you can see how at least for these simple examples, loops are superficially similar to recursion. You have a starting value, you have a condition of some sort, and you have an increment or a similarly changing variable. So you have some condition that is going to end. And then again, when you call the method, something has to change, otherwise it gets stuck in that loop. And then here's an example of the Fibonacci sequence. And this is where it gets a little bit more confusing. So the Fibonacci sequence is like each number is the sum of the number before it. So unless you have zero or one. So if it's a zero, you return a zero. If it's a one, you return one. Otherwise, we return recursive call, n minus one plus n minus two. So how I would do this, I would just write this in the border, put this slide up, the link will be somewhere, I'm sure. So let's say we have three. So three doesn't equal zero, three doesn't equal one. So Fibonacci, three minus one is two, plus Fibonacci, three minus two is one. Now one, we know the value is one. So this becomes one. I wish I had my tablet, I would write this on, but I'm not at home right now. So then this becomes Fibonacci two. So we call that again. That becomes two minus one is one, two minus two is zero. So that's one plus zero plus the original one from before. And I think that gives us I think two, if I counted that correctly. So again, this is this is a really good one to explain the concept. So it shows you the sum of the Fibonacci numbers up to a certain value. Again, just kind of walk through that. Again, the book has a little some more details about this. So again, as I mentioned the introductory exercise, I would just get them started with some simple loops. And it's good review. You're counting up, counting down, adding a range of numbers, you can kind of play around with that. And then once you've explained what recursion is, you can have the students rewrite those loops as recursive methods. Just paying careful attention to the starting value, ending value, and the base case. Again, adding the range numbers might be a little difficult. So you might want to do this one first, and then explain, you know, after explaining the basic case. And then after you explain Fibonacci, you can try adding a range of numbers. You know, just, you know, your mileage may vary depending. Now, this is something that this is a topic that a lot of AP teachers either skip or only spend one or two lessons on at the very end. I'm going to move it this year to right after methods, and just try and teach it real quick. Well, methods are fresh in the students heads. It's only a couple of percentage points on the on the AP exam. So it's not something you want to you know, you want to just drill into too much, but make sure the students can trace some code, because you might get a multiple choice question about that. Very rarely is it part of the fair response questions. There we looked at what is recursion, the basic structure, the recursion depth limits, showed you can compare them to loops, and talked about returning values and you're showing that summing that we saw with the Fibonacci sequence, and gave you a hopefully helpful introductory exercise, you can do it early in the unit. So thanks for watching. Good luck.