Learn how recursion ♾️works in 100 seconds. https://fireship.io

#compsci #100SecondsOfCode

Install the quiz app 🤓

iOS https://itunes.apple.com/us/app/fireship/id1462592372?mt=8
Android https://play.google.com/store/apps/details?id=io.fireship.quizapp

Upgrade to Fireship PRO at https://fireship.io/pro
Use code lORhwXd2 for 25% off your first payment. 

My VS Code Theme

- Atom One Dark 
- vscode-icons
- Fira Code Font
2024-02-04T06:24:46.682Z
nova-2
2-general-nova
2024-01-09.29447
30089e05-99d1-4376-b32e-c263170674af
c1147394-8ebf-4633-af5d-a2c422f667a3
304942b48392e9ca1a10ba143c07f294739be8a9ed1170baf5077493e8f323a6
67875a7f-c9c4-48a0-aa55-5bdb8a91c34a
deprecated
A recursive function is a function that calls itself inside of its own definition.
It's like having a dream inside of a dream.
When you have a recursive function that calls itself without anything to stop it, you have created an infinite loop.
The first function gets pushed onto the call stack, the next one on top of it, then another one on Well that and so on forever.
Or until your runtime throws a Stack Overflow error or the computer just runs out of memory.
So recursion is just another way to create a loop.
But infinite loops aren't very useful in practice.
That's why recursive functions have some sort of stopping point.
Like some conditional logic that tells the function to stop calling itself when a condition It's been met.
On an interview, you might be asked to implement some code that finds a specific index in the Fibonacci sequence.
You'll notice a given Index of the sequence is just the sum of the 2 previous values.
So how do we find index number 2023?
Your first thought might be to implement an iterative solution with A for loop.
Compute the Fibonacci value for each index until we get to the target index of 2023.
But then you're asked to implement a recursive We can do that by writing a function that starts with the index and then works its way backwards until it gets to a base case of being less than or equal to 1.
Notice how we're Following the same function inside the function body and we're doing it twice.
There's a big problem here though.
It will traverse down the entire sequence for every single index.
That's O to the N Squared time complexity, which is not good at all.
In the next video, I'll explain how to improve this with memoization.
Recursive functions are not the right choice in every situation, But they are very good for tree and graph traversals.
This has been Recursion in 100 seconds.
Thanks for watching and I will see you in the next one.

Speaker 0: A recursive function is a function that calls itself inside of its own definition. It's like having a dream inside of a dream. When you have a recursive function that calls itself without anything to stop it, you have created an infinite loop. The first function gets pushed onto the call stack, the next one on top of it, then another one on Well that and so on forever. Or until your runtime throws a Stack Overflow error or the computer just runs out of memory.

So recursion is just another way to create a loop. But infinite loops aren't very useful in practice. That's why recursive functions have some sort of stopping point. Like some conditional logic that tells the function to stop calling itself when a condition It's been met. On an interview, you might be asked to implement some code that finds a specific index in the Fibonacci sequence.

You'll notice a given Index of the sequence is just the sum of the 2 previous values. So how do we find index number 2023? Your first thought might be to implement an iterative solution with A for loop. Compute the Fibonacci value for each index until we get to the target index of 2023. But then you're asked to implement a recursive We can do that by writing a function that starts with the index and then works its way backwards until it gets to a base case of being less than or equal to 1.

Notice how we're Following the same function inside the function body and we're doing it twice. There's a big problem here though. It will traverse down the entire sequence for every single index. That's O to the N Squared time complexity, which is not good at all. In the next video, I'll explain how to improve this with memoization.

Recursive functions are not the right choice in every situation, But they are very good for tree and graph traversals. This has been Recursion in 100 seconds. Thanks for watching and I will see you in the next one.
A recursive function is a function that calls itself inside of its own definition. It's like having a dream inside of a dream. When you have a recursive function that calls itself without anything to stop it, you have created an infinite loop. The first function gets pushed onto the call stack, the next one on top of it, then another one on Well that and so on forever. Or until your runtime throws a Stack Overflow error or the computer just runs out of memory. So recursion is just another way to create a loop.
But infinite loops aren't very useful in practice. That's why recursive functions have some sort of stopping point. Like some conditional logic that tells the function to stop calling itself when a condition It's been met. On an interview, you might be asked to implement some code that finds a specific index in the Fibonacci sequence. You'll notice a given Index of the sequence is just the sum of the 2 previous values. So how do we find index number 2023? Your first thought might be to implement an iterative solution with A for loop.
logic
mathematics
Compute the Fibonacci value for each index until we get to the target index of 2023.
But then you're asked to implement a recursive We can do that by writing a function that starts with the index and then works its way backwards until it gets to a base case of being less than or equal to 1. Notice how we're Following the same function inside the function body and we're doing it twice. There's a big problem here though. It will traverse down the entire sequence for every single index. That's O to the N Squared time complexity, which is not good at all. In the next video, I'll explain how to improve this with memoization.
operations
programming
Recursive functions are not the right choice in every situation, But they are very good for tree and graph traversals. This has been Recursion in 100 seconds. Thanks for watching and I will see you in the next one.
trees
A recursive function is a function that calls itself inside of its own definition. It's like having a dream inside of a dream. When you have a recursive function that calls itself without anything to stop it, you have created an infinite loop. The first function gets pushed onto the call stack, the next one on top of it, then another one on Well that and so on forever. Or until your runtime throws a Stack Overflow error or the computer just runs out of memory. So recursion is just another way to create a loop. But infinite loops aren't very useful in practice. That's why recursive functions have some sort of stopping point. Like some conditional logic that tells the function to stop calling itself when a condition It's been met. On an interview, you might be asked to implement some code that finds a specific index in the Fibonacci sequence. You'll notice a given Index of the sequence is just the sum of the 2 previous values. So how do we find index number 2023? Your first thought might be to implement an iterative solution with A for loop. Compute the Fibonacci value for each index until we get to the target index of 2023. But then you're asked to implement a recursive We can do that by writing a function that starts with the index and then works its way backwards until it gets to a base case of being less than or equal to 1. Notice how we're Following the same function inside the function body and we're doing it twice. There's a big problem here though. It will traverse down the entire sequence for every single index. That's O to the N Squared time complexity, which is not good at all. In the next video, I'll explain how to improve this with memoization. Recursive functions are not the right choice in every situation, But they are very good for tree and graph traversals. This has been Recursion in 100 seconds. Thanks for watching and I will see you in the next one.
A
a
recursive
recursive
function
function
is
is
a
a
function
function
that
that
calls
calls
itself
itself
inside
inside
of
of
its
its
own
own
definition.
definition
It's
it's
like
like
having
having
a
a
dream
dream
inside
inside
of
of
a
a
dream.
dream
When
when
you
you
have
have
a
a
recursive
recursive
function
function
that
that
calls
calls
itself
itself
without
without
anything
anything
to
to
stop
stop
it,
it
you
you
have
have
created
created
an
an
infinite
infinite
loop.
loop
The
the
first
first
function
function
gets
gets
pushed
pushed
onto
onto
the
the
call
call
stack,
stack
the
the
next
next
one
one
on
on
top
top
of
of
it,
it
then
then
another
another
one
one
on
on
Well
well
that
that
and
and
so
so
on
on
forever.
forever
Or
or
until
until
your
your
runtime
runtime
throws
throws
a
a
Stack
stack
Overflow
overflow
error
error
or
or
the
the
computer
computer
just
just
runs
runs
out
out
of
of
memory.
memory
So
so
recursion
recursion
is
is
just
just
another
another
way
way
to
to
create
create
a
a
loop.
loop
But
but
infinite
infinite
loops
loops
aren't
aren't
very
very
useful
useful
in
in
practice.
practice
That's
that's
why
why
recursive
recursive
functions
functions
have
have
some
some
sort
sort
of
of
stopping
stopping
point.
point
Like
like
some
some
conditional
conditional
logic
logic
that
that
tells
tells
the
the
function
function
to
to
stop
stop
calling
calling
itself
itself
when
when
a
a
condition
condition
It's
it's
been
been
met.
met
On
on
an
an
interview,
interview
you
you
might
might
be
be
asked
asked
to
to
implement
implement
some
some
code
code
that
that
finds
finds
a
a
specific
specific
index
index
in
in
the
the
Fibonacci
fibonacci
sequence.
sequence
You'll
you'll
notice
notice
a
a
given
given
Index
index
of
of
the
the
sequence
sequence
is
is
just
just
the
the
sum
sum
of
of
the
the
2
2
previous
previous
values.
values
So
so
how
how
do
do
we
we
find
find
index
index
number
number
2023?
2023
Your
your
first
first
thought
thought
might
might
be
be
to
to
implement
implement
an
an
iterative
iterative
solution
solution
with
with
A
a
for
for
loop.
loop
Compute
compute
the
the
Fibonacci
fibonacci
value
value
for
for
each
each
index
index
until
until
we
we
get
get
to
to
the
the
target
target
index
index
of
of
2023.
2023
But
but
then
then
you're
you're
asked
asked
to
to
implement
implement
a
a
recursive
recursive
We
we
can
can
do
do
that
that
by
by
writing
writing
a
a
function
function
that
that
starts
starts
with
with
the
the
index
index
and
and
then
then
works
works
its
its
way
way
backwards
backwards
until
until
it
it
gets
gets
to
to
a
a
base
base
case
case
of
of
being
being
less
less
than
than
or
or
equal
equal
to
to
1.
1
Notice
notice
how
how
we're
we're
Following
following
the
the
same
same
function
function
inside
inside
the
the
function
function
body
body
and
and
we're
we're
doing
doing
it
it
twice.
twice
There's
there's
a
a
big
big
problem
problem
here
here
though.
though
It
it
will
will
traverse
traverse
down
down
the
the
entire
entire
sequence
sequence
for
for
every
every
single
single
index.
index
That's
that's
O
o
to
to
the
the
N
n
Squared
squared
time
time
complexity,
complexity
which
which
is
is
not
not
good
good
at
at
all.
all
In
in
the
the
next
next
video,
video
I'll
i'll
explain
explain
how
how
to
to
improve
improve
this
this
with
with
memoization.
memoization
Recursive
recursive
functions
functions
are
are
not
not
the
the
right
right
choice
choice
in
in
every
every
situation,
situation
But
but
they
they
are
are
very
very
good
good
for
for
tree
tree
and
and
graph
graph
traversals.
traversals
This
this
has
has
been
been
Recursion
recursion
in
in
100
100
seconds.
seconds
Thanks
thanks
for
for
watching
watching
and
and
I
i
will
will
see
see
you
you
in
in
the
the
next
next
one.
one
success
The speaker explains that variables inside of a function call it A, and that there is a stopping point when a condition is met. Rec relationship functions are not the best choice for tree and graph dated, but they are good for Rec yet. The speaker also explains how to improve the process of creating a loop and mentions that it may require down-loading of the sequence.
Recursion in 100 Seconds
A recursive function is a function that calls itself inside of its own definition. It's like having a dream inside of a dream. When you have a recursive function that calls itself without anything to stop it, you have created an infinite loop. The first function gets pushed onto the call stack, the next one on top of it, then another one on Well that and so on forever. Or until your runtime throws a Stack Overflow error or the computer just runs out of memory. So recursion is just another way to create a loop. But infinite loops aren't very useful in practice. That's why recursive functions have some sort of stopping point. Like some conditional logic that tells the function to stop calling itself when a condition It's been met. On an interview, you might be asked to implement some code that finds a specific index in the Fibonacci sequence. You'll notice a given Index of the sequence is just the sum of the 2 previous values. So how do we find index number 2023? Your first thought might be to implement an iterative solution with A for loop. Compute the Fibonacci value for each index until we get to the target index of 2023. But then you're asked to implement a recursive We can do that by writing a function that starts with the index and then works its way backwards until it gets to a base case of being less than or equal to 1. Notice how we're Following the same function inside the function body and we're doing it twice. There's a big problem here though. It will traverse down the entire sequence for every single index. That's O to the N Squared time complexity, which is not good at all. In the next video, I'll explain how to improve this with memoization. Recursive functions are not the right choice in every situation, But they are very good for tree and graph traversals. This has been Recursion in 100 seconds. Thanks for watching and I will see you in the next one.
The speaker explains that variables inside of a function call it A, and that there is a stopping point when a condition is met. Rec relationship functions are not the best choice for tree and graph dated, but they are good for Rec yet. The speaker also explains how to improve the process of creating a loop and mentions that it may require down-loading of the sequence.