Hey what's going on everyone this is Jacob with Deb slopes dot com.
And today I'm going to just do a short video on how recursion and what you would use you know recursive
functions for and what it is exactly recurs in like the textbook definition for it is recursion is the
process of repeating items in a self similar way.
So something that you would use like a loop for or any kind of iteration like that.
You could probably use a form of recursion to solve the same exact problem.
One thing you want to think of with recursion though is it is very expensive when it comes to memory.
So if you're calling the function more than a thousand times you probably don't want to use a recursive
function.
But I'm going to show you some examples.
And let's go ahead and open up playground
Okay.
So I just opened up the X code playground will make the screen bigger.
That should be big.
I'm jumping into a simple example here using a for loop and then we'll show you how you can solve the
same thing with recursion just to get our feet wet.
This isn't the best example because the purpose for using recursion is to usually like make your code
cleaner and easier to read.
And you know with swift these days the way that a foreign loop is now written it's pretty clean and
easy to read but we'll do we'll do it anyways.
Just so you can understand what's going on.
So if we just write a function here.
Print numbers we're just going to print numbers to the screen.
And we're going to make a simple for loop for x in 0 8 Max and we're just going to simply print print
X
and let's open up the bottom here and now we will say print numbers.
And why don't we just toss in three and you know like you're familiar with you get your count 0 1 through
3.
All right.
So how do we do this with using a recursive function call.
It's pretty easy so let's write a function here and we're going to call this print numbers rack for
recursive and we can pass in another integer the same exact way as above.
And here we just have to make sure the max is above zero
and right now right.
So we're going to call our function.
So so a function that calls itself from within itself that's a recursive function.
And what we're going to do here is we're actually going to call print numbers rack and we're going to
pass in the variable that already was passed in which is Max.
But we're going to subtract one from it.
And this kind of as you can see it's kind of the same concept as a loop already.
So we can come down here outside of the if statement and print Max and it's going to iterate through
the same exact way for the for loop did when we call.
So if we do print numbers wreck and let's pass for and just to show a little different.
There we go.
So it just printed 0 1 2 3 4.
So this function is the same exact function is up here.
And like I said this isn't the best example of like why you would use a recursive call function only
because it's it's the same amount of code if not more.
But it's a good it's a good easy example to wrap your head around what recursion is.
So I'm going to go ahead and delete this OK.
Now we can do a little bit more of a not really difficult recursive call but this is exactly the reason
why you'd probably want to use reoccurs or an over like looping through a for loop.
And what we're going to do is we're going to find factorials.
And if you're not familiar with what a factorial of non-negative number of Campeon negative a non-negative
integer
if you're not familiar with that we'll just go over that real quick.
So I'm in back back to middle school math.
So a factorial is shown by the explanation point.
If you remember this like what would be the factorial of 1.
Well that is just going to be equal to 1 if we wanted to that and all of the factorial of 2.
It's 1 1 times 2 and that would equal two.
And then as you go up factorials three would be one times two times three and that equals six.
All right so I mean obviously like for smaller numbers this is really easy to do.
But you know already when you get to 7 you're you know you're doing one times two times three times
for five six and seven that can get pretty tedious and you start getting large numbers here.
So that's five thousand forty.
Now if we were to do this the old like not the old fashioned way but if we were to do this with a four
inch loop we end up writing a lot more code and we're going to do that.
I'm going to write the for loop just so you can see the benefits of using recursion.
So let's see we need of a variable first that is going to keep track of our multiplication solutions
so we'll just set that to zero and then let's make our 4 and loop.
And we want this one.
We don't want zero in there so 1 through whatever number we pass in.
And then if the factorial is not equal to zero we are going to set the factorial variable equal to what
value is already stored in our variable times X times the iteration that we're going through and our
for in loop.
Now if it is zero we need to set our factorial variable to 1
and then we can go ahead and return our factorial variable.
So you know it's not a lot of code but it's still a decent amount.
And why don't we just test this.
We already know the factorial of 7 Let's see where is my size.
So the factorial of seven was five thousand forty.
So that worked.
This solution works for us.
OK so if we want to use this with
recursion we have to think through what we want to do.
We want to take the number that we're passing in and we have to multiply everything from 1 to that number
the number we pass on is the max.
So let's think this through we can write the basic
function.
So we're passing an integer and we also want to return an integer just like before.
So so far everything is the same.
Now at some point we want to return the number that is being passed in times you know every single number.
So from you know 7 to 1.
So it's pretty easy we just had to call call our factorial function and pass in the number again but
minus 1.
So just like the how we iterated through the numbers and printed 0 through 3 before it's the same concept.
There's one problem with this though if we try to call this it's not going to work very well so we probably
have to think about this like OK like up above we have to make sure it's above zero or we're all like
one times one or one times anything is the same number.
So maybe we should just check for if the number is equal to 1.
We can return one.
And that'll take care of you know the smaller numbers as we approach one or it won't allow us to go
negative either.
But then we need to
return this Whoops sorry about that bracket.
Right.
Cut and paste.
All right so if we pass on the same number now to our factorial let's pass and seven and we should get
five thousand forty again on the right side here.
Yes so we did.
So this was called six times starting with it too.
So we basically ignored one because we know that one times anything is going to be the same exact number
and that prevents us from going into the negative numbers if we hit zero or negative one function one
at work.
And yes.
So we would all ran into the same problem here.
But so the point of recursion we have one two three four five six basically seven lines of code finding
the factorial with recursion versus 1 2 3 4 5 6 7 8 versus 11 lines of code.
So it makes it a lot easier to read.
When I first learned about recursion I wanted to use it in every scenario possible.
It's not that realistic but there are certain circumstances where it works out perfect and it can be
a pretty powerful tool.
It's cool.
All right.
So obviously like with work with recursive calls it can get really confusing.
I try to use really basic simple examples for today because they can get very confusing.
But a good way.
A good place to practice where you can use a lot of these skills and practice using it as Project ular
dot dot net.
I like to go here.
They have awesome little like mind exercises it makes me start thinking more like a programmer and like
solving problems.
And it's pretty cool.
You would log in and you just go through you go and order you know one through whatever.
And after the first two after the first three this gets pretty darned difficult.
But you can use like recursive function calls and a lot of these problems to make them easier to solve.
And I would definitely recommend checking it out.
That's project ILIR dot net and it's awesome.
And my name is Jacob Deb soap's dot com and I hope you enjoy the video.