 Hello and welcome. This is Christian. In this video, we're going to look at recursion and see how this works in Java. Okay, so recursion or recursive function is defined as a function that calls itself within the body of its own function. So to understand how that works, let's look at this example here. I have a diagram here to show you three different functions. So let's say that this function here is going to be invoked from here. I initially call function A and I get printed out over here. And I print A on the right side over here as you can see. And then once I print A, I'm going to call function B. So when that happens, it goes to the B function. Notice that print A done is not executed yet because now the header of this thread is already in function B. So what I get next is I'm going to print B to the console that I'll put over here. I just indent this so you can see the level of these functions. And then once I print B, I'm going to call function C. And now it goes to the function C now. So B done here is not printed yet because I'm over here now. And I'm going to print C done and C over here. Then once I'm done, there's no more statements to print. Then only then the function will be turned back to where it's being called at this point here. And after this function call, I print B done. So you get B done at the output over here. Again, when you are done, there's no more statements to run. So the reference point points back to where it was being called initially in the A function. And at this point, I just have one statement to print, which is A done. After that, I'm done. So this is how recursive works. I'm showing you here three functions to use. Really, if you think about function A calling itself that many times, you're going to get something like this. So the result here, as you can see, is still in the correct order. Not as you expected, but it's still in the correct order if you trace this carefully. All right, so let's go and write a really simple program to illustrate how this works in Java. Okay, so here in the IDE, I'm using Eclipse here, by the way. So I'm going to write a function that will print a series of numbers from 1 to n. Okay, and we'll define n as equal to 5. This is our upper bound. I'm going to create another variable called C. This is the counter starting from 1. Okay, so here I'm going to create a function called g1 in a minute. And I'm going to pass to this function n and c. Okay, so this is the function I will use to invoke. So I'll go down here and create my function g1. Okay, so again, a recursive function is a function that calls itself like this. Okay, so whatever this is, that's what it is. A function that calls itself and its body. That is the recursive function. Now, you've got to be very careful with this. When you do this, though, you have to find a way to terminate the recursive call. Otherwise, you will end up having an infinite loop, just like a regular while loop or full loop without a termination point. Okay, so that's just the whole idea, is a function that calls itself. All right, so to make this work, I'm going to go here and make sure that I have a way to stop this. So I'm going to say that I'm going to print the counter from 1 until 5 and I will stop this recursive call when c is greater than 5. So I want to say if c is less than or equal to 5, I'm going to print 5 inclusive, so I want to do that. Then, if that is true, then I'm going to go ahead and make the recursive call in here, inside this black hole code. Otherwise, don't call it, and that's how you terminate the recursive call. If you don't do this, you're going to keep calling it, you can go to an forever loop. So once I'm done in the loop, I'm going to go out here and sys out a message to the console just say done. Now, to show something that it does work, I'm going to go right in here and I'm going to print out c. So I'm going to print out c here. After I print c, I'm going to increment c by 1. Just to make sure that next time I want to call g1 again, I increase my c updated by 1. So c is now 2, it's 3, 4, 5, and 6. 1c is 6, this is false. It gets out of here of this black hole code and prints line 19, and then it will go back and reverse the recursive call. Okay, so just to show this, it does work. Let's say this and run, see what happens. So as you can see on the outside, on the console on the right side, it prints 1, 2, 3, 4, 5, that's c here. After c is now 6, it exits out of that and it starts printing line 19. Done, done, done, actually it does this in reverse order. It's like you go inside the loop and you come back out. So when you come back out, you're going to print this in reverse order. To show that it does work that way, I'm going to go here and you can put here done and put here plus c. And you're going to see that it's a little bit different here. When I run it, you see it prints in reverse order. Just to show you that it does work. Don't worry about why this is 6, 6, 2 times and there's no 1. And that's because here when you go into this black hole code, you increase c by 1 already so that you will never print c as 1 here because you print it after this already been updated, right? So that's why initially you print 1, if this is not a recursive call, you print 1, right? Here you increment c, let's say you don't go in here, what you do? You go down here and you print c and that's why c is now 2. So you go, so 1 here, this goes with that, 1. This 2 goes with this statement. 3 is with that. 4 here, 5 this and then the last one is this. So that's when you reverse it, you print in backwards, okay? So that's how recursive works really. It's hard to understand, it's hard to see how this works. So to illustrate this in a different way, I'm going to show you how to do this using two functions, alright? So down here, I'm going to go and let me put this over here a bit. Go down here and I'm going to create another function here called g2. It takes the same parameters and all this one does is really just this part here, okay? So copy that you can put it here. And then here I'm not going to increment my c, I'm going to turn this off. And then instead of calling g1, I'm going to call g2, okay? So this two function here has the same effects as the one we just did. I just shown you here so that when we trace it, it's better to understand how this works. So as you can see if I print it out, I mean if I run it, I'm going to get the same result as before, okay? No difference at all, okay? So if you trace this, it just basically go here, go to g2. I go in here, increase my c by one. I pass this two parameters back to g1. I call g1, go back here again. So these two functions are just like going back and forth here until this part is no longer true. It exits out, print this in reverse order, okay? You print that end of that statement. But where was g1 called before in the previous last call? It was called down here. So it's going to go back out here and then this is done. So no more, you know, data to print. No, where was g2 called previously? Well, in here. So you go back here, okay, it's done. Then you exit out, okay, print this out. So you print this statement, right? And you keep going until you reach the end of that loop, the cycle, okay? I just realized that this is a little bit different than what you saw earlier. It has like a two double six, six, and you have only one here, okay? The reason why is because when I get to this point, the difference was here, and this block of code, I did not increment my c yet. So that's why I'm able to print one, two, three, four, five, six in that order. And the example earlier, we actually increased the c first, right? Increase that first before we actually do the recursive call. So the output was a little bit different down here. But nonetheless, the order here is still correct, okay? So to show how this actually works, I'm going to go and use the debugger. The debugger is if you go to the left column right here, if I go here and just double click on it, show this little blue-green dot here, okay? And then you go up to the icon here and click the little bug here and just click the debugger. And it's going to ask you for a different perspective. Yeah, just go and switch it. So you have into this, right into this kind of output. So let me close this down here. It'll be weird how it's set up, but the output is right in the middle here. And on the right side is, you know, the variables and values you can watch as you go. So now the program stops at line 7. And I'm going to, you know, run through line by line by pressing F6 or F5, or you can go here and click the little arrow here. Hmm, let's do it again. I'm going to turn this off. Okay, so let's do it one more time. And go ahead and click debug. I'm sure I do debugging as, okay. All right, and I'm not sure what is showing up here. Okay, I wanted that. All right, so here we go. And now you see that up here, you should see these little arrows up here on the top. Want to say step in two, step over. Okay, step in two here is if you want to go into a function, then you step into the function. Otherwise, you would step over to go line by line. So I'm going to go and step over at this point. As you can see the arguments list over here, the variables listed here, their values are here. This arguments is this arguments here, okay. So now we're going to execute line 7 and n will show up here. So if I go and step over, you see that now n is now set to 5. The next one is c is now 1. And now we are line 10. We're going to call a function. So you want to step into the function. So press the F5 or go into this function, this icon here. And now we're inside the function g1. It checks the condition if this is true or not. This should be true. So we go step over that. And then it's going to print that out. So here, just make sure you don't go and step in two. If you click step in two, you're going to go to the print line function. You don't want that. So just step over that. And we print out the number one here to the console. Now we're going to step into g2. So go into that g2 function down here now. And we increase c by 1. And they're going to step into g1. And then now c is 2. It comes back here. You can do a comparison. That's true. We print that out. Here it is. And we go into g2 again. And so the cycle is start over again. Until we get to the point where c is no longer less or equal to 5. And we're just going to step through this. And then now, you see that now c is 6 at this point. It's going to be false when we get to the next part. At this line 14, this is no longer not true. So we exit out of that. And then we go to line 18. So we print c down here. As you can see, c is 6 at this point. So we're going to print the done. And it will say c6 there. As you can see, I mean done 6 here. And with the end of the function, we're going to go back to where g1 was last called. It was called down here line 22. So we're going to go back into that function. And since there's nothing left in here, we're going to go to the end of the function. And then where was g2 called last before this? Right in here in line 16. So we keep doing that until the cycle is done. So now we can see that we're going back where now c is now 5. And then we print that out. And we keep going until we are done with the whole cycle where c was actually 1. And then now that's the last one we call. And then we go back to the main program here, line 10. And no more statements will go and exit the program at that point. All right? So that's how recursive works behind the scene, just to show you how this is done. But really, it's a function that calls itself. Okay? So I hope this is helpful. And again, just make sure that when you do this type of recursion calls, you have to have a way to terminate this call. Okay? If you don't do that, you're going to have a problem. So let me go back here. Let's say that I forgot to increase my c right here, right? So if I go and turn that off, if I forgot to do that, you're going to see that it's going to run into an infinite loop. As you can see here, it's out of memory. So it shuts down at this point. All right? So be very careful when you do this. If I were to call the same one as before, if I called you one here, and I'm not calling this, you know, function here at all, and say I forgot to increase my c, then I'll get the same result. Let me save this. You run it. I get the same problem here. Okay? So out of memory, really, you can see that it prints the same statements all over again. There's a lot of them if you scroll up. Okay, it crashed. So just be careful when you do this. Just make sure you, you know, increase the c or decrease and however you do it to make this false in order to exit out of this loop. Okay, so I hope this is helpful. If you have any questions, feel free to post your comments below. Or if you have a better solution that you want to share with the world, feel free to post them as well. Thank you and have a great day.