In this lecture I want to go over another way to you by using functions and it's called recursion.
So you may have seen in one of your math classes the function defined by using itself.
So an example of that is factorial.
So if you had five factorial.
What that really means is five times four times three times two times what.
Or you may have seen it like five times four factorial.
And these are the same.
So this is equivalent to this.
Well we can actually do this in C++ as well.
So say we have this factorial function that I wrote here and right now it's not implemented.
This is called Stebbing by the way by actually having kind of a default sort of implementation which
we're turning zero here in Maine we have this x and we're outputting the factorial of whatever the integer
is in the actual factorial value right now it's is going to print out the factorial 5 0 is obviously
wrong.
But let's actually use recursion which means rushy going to call ourselves in this function.
So how you do that is we'll say.
So factorial in this case is just the number.
So it be five in this case time.
The factorial of the number minus 1 or 8.
So just like here.
So it's the number multiplied by the next lower number.
So for in this case factorial.
So that's exactly what we did here.
However there's somewhat of a problem here because how do you know when to stop.
So we still need to have a condition that says here's when you stop calling yourself.
So this is kind of you'll see recursion in the following form you always have kind of general case which
is this is just return.
Then in this case the number times itself and given the parameter the number minus one and you always
have the base case so in the base case.
So what is one factorial.
Well one factorials just one right.
So here our base case will be the number if the number is 1.
Then we're just going to return 1.
So that's kind of it.
So we have the base case is if that number is 1 we just return 1.
Because 1 factorial is just one in fact zero factorial is also one.
But we're going to stop here at 1 and you have the general case right here which is what actually does
the recursion.
Recursion and that actually calls itself with a lower number.
So if we run this so we see the factorial of 5 is 120.
So how did this work.
There's no loop it's a while loop right.
Or for loop or do while loop and it's because it's sort of stacks.
So the first time it gets called.
So you're safe factorial of in this case 5 and then come here.
And say five times vectorial four and they'll call it again.
And now the number is four four is not equal to 1.
So to say four times factorial of three.
So the number minus.
Right.
Then go back up here.
Three three is not equal to one so three times the factorial of two.
And then again two times vectorial 1 One is is equal to one.
Now we kind of return off this giant stack that we've been making.
And then finally the base case is really the stopping point.
So now it stops everything kind of returns back and then it sort of unravels like this one.
And then times two times three times four times five.
All in all in here.
So this is very confusing at first.
And I remember having kind of a problem with recursion for and I find that when I tutor students a lot
of universities really emphasize looping by recursion or the bigger universities often.
And I think that's usually because there are a lot of applications of it in many theories but I tend
not to ever use recursion because in practice it really doesn't come up that often actually.
And not only that I find it like I said it's quite confusing and it's it's also actually painfully inefficient.
It's not it's not very fast if you really start diving deep into your current stack.
But Allah you make up your own mind about it.
And we really present this here to you for completeness sake.
So in the next lecture we're going to do some practice.