Welcome back.
I'm about to show you my favorite animation of the course, I'm very proud of it, so don't laugh at
me if you think it's silly.
I like to think of recursion as this scenario where you have this tap pouring water into a glass for
this little man that's sitting on the couch watching TV.
And what I think about recursion, I like to think that they have two big problems.
One is that, well, they're kind of difficult to understand at first, the second is that we can have
a case like this where I'm pouring water into a glass, I'm pouring water into a glass and pouring water
into a glass.
And that is me calling the function over and over and over.
And as we keep going, as we keep going, fills up the cup past its limit, it keeps going.
It doesn't stop.
It keeps calling the function, keeps calling the function until.
Oh, boy, the man has drowned.
What just happened here, this is called Stack Overflow.
Let's go back to some code.
Remember the function inception that we created?
If I run this function.
And I say Inception.
And simply run this function, I'll get an error.
Let's make this a little bit bigger to see.
I get an error saying maximum call stack size exceeded my browser, in this case, Google Chrome is
smart enough to say, all right, you got to stop this madness.
I'm just calling this inception function over and over and over and over, because remember, our function
just calls itself and eventually, if Google Chrome doesn't stop this, it's going to crash.
And in the past, that's what would happen.
If I run this function, the browser will crash and I would have to restart it.
But they've added a safeguards here saying, hey, you've called the maximum cost size.
You got to stop what you're doing.
Now, this is called stack overflow.
Let's dive deeper into this topic.
Just going to clear the console here and right our inception function, but this time I'm going to add
a keyword called debugger.
That the Chrome browser is going to detect and pause the function when it sees this word.
So I'm going to.
Run this function.
And you'll notice that as soon as I hit enter, it's going to stop and give me a little panel here where
I can control the function.
Let's hit enter.
And there you go, I'm in now the debugger mode.
And you'll see here a few things, one, it'll show me where I am in the function and also show me changes
around just so it's a little bit cleaner.
I'm going to make this a little smaller.
There you go.
You see here something called call stack now from the name Stack Overflow.
This might give you a bit of a hint right now.
We've called the inception function right here.
We have an.
Called it the second time around, if I click on the step over icon over here, it's going to go to
the next line of a code, it's going to go to the next.
And now it's going to call the next inception function.
Have a look over here at the call stack.
As to what's going to happen next.
Do you see that I just added another function on the call stack?
And as we know about STAC data structure, we're just adding the function call on top of the other one,
and if I step over here and go again, that's a third function call.
And if I keep going, keep going, keep going.
You'll see that I'm just adding more and more and more things to the stack.
But there's a problem here, right?
Nothing's getting popped from the stack.
Instead, the function just keeps running, keeps running, keeps running, keeps running, keeps running
until we run out of memory here.
Remember, Stack in this case is grabbing a piece of memory from our computer and adding inception to
the stack.
And as you know, memory is limited.
We don't have infinite amount.
So as we keep going, as we keep going, it's going to stack overflow.
And throw in there, and this is one of the biggest problems with recursion that we're going to get
into later on, as you see here, though, it can be very, very dangerous because we can run programs
that overflow, that never stop, that have infinite loops, essentially that crash our programs.
You'll also see here that this costs memory.
A stack is holding these function calls and recursion.
One of its downside is that we have to hold on to these calls and remember them one by one, which can
get really expensive.
So if you ever get asked this question in an interview about recursion, maybe a possible problem with
recursion, you can simply say, well, the computer needs to allocate memory to remember things stack
overflow can occur when we have recursion and there's no way to stop this recursive.
Call it simply the computer saying, whoa, whoa, whoa, whoa, OK, this is getting silly.
I'm not remembering any more things.
I'm out of memory.
I'm going to just write.
In the next video, we're going to try and solve this problem and learn about something called a base
case, I think that you have to have in a recursive function to stop it from doing this.
I'll see in the next one.