 Okay, so recursion. Let me go here and share this screen with you. Look at recursion. So this is, I couldn't find a better definition than from this site. They had a really good definition, I guess, or recursion is, and, you know, how do we use recursion and programming. So here recursion is when a solution to a problem that uses a smaller instances of the same problem itself. A little bit hard to explain but in terms of programming, recursion is usually referred to when a function that calls itself. Okay, that's the recursion. So another example of how recursion can be seen is like the Russian dolls. I'm pretty sure you're seeing this already. If you look at it, it's like the bigger version, right? You open it up and have a smaller version until you find a really simple version of that. So that's the example of where recursion is. It's the same shape, same function, same data, right? And repeating stuff. And then of course, you can see a lot of things as well in nature. It's a pattern going on. If you look at the leaves over here, the ferns, like you have gone from bigger and smaller and smaller version of it. Same thing here as well. And also these, you can also find other places and, you know, at seashells and things like that. So every recursive function that is usually referred to when a function that calls itself. The number is calculated by, you're multiplying the number of, let's say factorial five, multiply that by four by three by two, all the way to by one. You get the product of all, I mean the, yeah, the product of all those numbers and you get a factorial. And so if you look at it, it's like the Russian doll, right? Five and four, three, two, one, small and smaller, until you get to the smallest number and then you end there. So when you write a program, you have to establish one or two basic cases to terminate the recursion. If you don't do that, you're going to end up just like an infinite loop, right, and it will crash the program. So this example on the left side is the factorial function to, you know, calculate factorial number. So the basic case will be here if and it's one, then the result is going to be one and I think factorial one or zero will give you one anyway. So you can check that because you pass and the number five in here, if and it's not five, then you skip the return class you go to the next return. And this one says five times the factorial of four, right, five times factorial four. So four goes back into the function. It does it again. Now what is four so four times factorial three. So you can see how that gets through the whole process until it is reduced to one. When that is true, it returns to one. When one is returned, everything gets reversed. Okay, if we curse back. So it can be very confusing. If you're not careful. There's another one that I feel you or some of you might have used before, as an input. Okay, when you write a function to get the input, you call the input function as a user to enter a number, a positive number for example and if they enter a negative number, you print invalid try again. And so if you did not use a while loop in the inside here, and you happen to call the same function again. If you do that, it's going to call the function. But if you keep running that cycle, no, and definitely eventually you're going to run out of memory. Okay, until you get a valid value that they would be cursed back. But when it comes back and the data, you're going to get an incorrect number. Also, so very, very confusing. So this is what's called a recursive call when you call the function itself within the body of the function. It doesn't matter where anywhere the function. If it's calling itself, we call that a recursive call. Okay. Now remember that when you every time when you call a function in the program doesn't matter is Python or Java or as a matter. There is what's called a stack. And there's a stack memory and the computer program that keeps track of all the function calls. Every time we call a function, it puts pushed a function to the stack, it keeps stacking those function calls. So when you end the function, it can go back and remove those with a stack. And then that's how it also knows and tracks where the function was called from and so forth. So to really explain this, here's an example of that. So instead of having the same function call itself because it's so hard to understand, I'm going to use like a three functions here, a B and C. Okay, and then over here on the left and right side have a stack, the stacking is a memory and computer that keeps track of all of all the function calls. So right now, at the very bottom of the stack, think of this like a stack of dishes. Okay, at the very bottom is the empty stack I put a bracket to show that it's empty. And then over here is the output. Okay, so when you make the very first initial call to a. Okay, so I make that version call call a, and then now this is the active function. And the first line is going to print a so the output would see is a over here. What happens is that when you call the function a is going to push that function a up the stack, or down, you're pulling down, but to into the stack. And then that's the active function until the function is done. Okay, so we print a to the output. The next line now we call B. So what happens we will call me that is now bump jump over here now. So we haven't called this print a done yet because we move over here now. So we call B. So it pushes be function to the top of the stack. Okay, and it prints be over here, and then it does the same thing the next thing we call see, and it keeps going so you see the stack is piling up here. Okay, and then we print see CS to print functions only. So we see see the see done. After that see is completed right this normal functions to call normal data to perform so it needs to go back to where it was called from. So that is going to go back down to be. Okay, when we go back down to be because we reverse it back to where was caught from here, then it popped the function see call from the memory stack. So it is now gone. But then when we come back to see, then, after see there is another statement to call print be. Okay, so that's the internal step five here it would print be done up here. Actually, this is, this is not there yet it shouldn't be there. So you print be, and then be is done. So we go back to what B was before. So this is just telling you that it's still keeping it, keeping track of the memory of where was called from. And then we backtracked that way, and then it goes to be that we pop up be from the memory stack, and then we go back to a now print a is done. And then when a is printed and then we move back to where it's being called from, we will get rid of a of the stack, and then everything is done. So in terms of the stack. So there is a limit to how many functions can be pushed to the stack. I think Python has about 909, 900, almost 1000, you know, stack slots on this if you call function 1000 times that it's going to overflow the stack. It's going to turn stack overflow. Okay, what's that is overflowed your program crashes, and then it's done. Okay, so you don't want to do that so you don't make that many, many, many costs. Of course, you can also increase the stack. If you want to do that but I mean we don't want to do that because it consumes a lot of memory. So that should not be the option.