WEBVTT

00:00.000 --> 00:07.000
Recursion Basics

00:07.000 --> 00:10.000
In this video, we will understand.

00:10.000 --> 00:15.000
Define recursion and explain how a function can call itself to solve smaller problems.

00:15.000 --> 00:20.000
Identify the base case and recursive case in a recursive function.

00:20.000 --> 00:26.000
Compare recursion and iteration, and know when recursion is more suitable.

00:30.000 --> 00:33.000
Understanding recursion

00:33.000 --> 00:38.000
Recursion is when a function calls itself to solve smaller parts of a problem.

00:38.000 --> 00:42.000
It keeps repeating this process until it reaches a point where it stops.

00:42.000 --> 00:44.000
Called the base case.

00:44.000 --> 00:47.000
A real-world example is tracing a family tree.

00:47.000 --> 00:55.000
You ask about your parents, then your grandparents, and so on until there are no more ancestors.

00:55.000 --> 00:58.000
Key components of recursion

00:58.000 --> 01:01.000
Every recursive function has two key parts.

01:01.000 --> 01:06.000
The recursive case is where the function calls itself with a smaller input.

01:06.000 --> 01:11.000
The base case stops the process and prevents infinite recursion.

01:11.000 --> 01:18.000
For example, in a countdown function, we print the number and call the same function with one less value.

01:18.000 --> 01:25.000
Until we reach zero, the base case, where the function stops.

01:25.000 --> 01:28.000
Recursion versus iteration.

01:28.000 --> 01:33.000
Recursion and iteration both repeat actions, but they work differently.

01:33.000 --> 01:39.000
Iteration uses loops, while recursion uses repeated function calls.

01:39.000 --> 01:46.000
Recursion is often more natural for problems that involve branching structures such as trees or directories on a computer.

01:46.000 --> 01:50.000
However, each recursive call uses memory for a new stack frame,

01:50.000 --> 01:54.000
so forgetting the base case can cause a stack overflow error.

01:54.000 --> 02:00.000
Examples and practice.

02:00.000 --> 02:03.000
Let's look at two simple examples.

02:03.000 --> 02:11.000
Account down from n to 1 is recursive, because the function calls itself until en equals zero.

02:11.000 --> 02:15.000
Another example is finding the sum of elements in an array.

02:15.000 --> 02:19.000
We add one element and then call the function again for the rest.

02:19.000 --> 02:24.000
To practice, try writing a recursive method that sums numbers from 1 to n,

02:24.000 --> 02:29.000
and trace how the function calls reduce step by step.

02:29.000 --> 02:41.000
Recursion may look complex, but understanding the base and recursive cases makes it an elegant way to solve many problems.

02:41.000 --> 02:47.000
Recursion helps solve problems by breaking them into smaller, repeatable steps.

02:47.000 --> 02:54.000
By understanding the base and recursive cases, we can write functions that are clear, efficient, and well structured.

