Recursion

In this lesson, we will briefly go through recursion.

Recursive Functions

We can also call the function itself from its body. Such a function is called a recursive function. A recursive function is a function that calls itself during its execution.

Recursion enables the function to repeat itself several times, collecting the result at the end of each iteration.

Components of a Recursive Function

Each recursive function has two parts:

  • Base Case: The base case is where the call to the function stops, i.e., it does not make any subsequent recursive calls.

  • Recursive Case: The recursive case is where the function calls itself again and again until it reaches the base case.

Let’s have a look at an example. Here, we print numbers from 1010 to 11 using recursion. We call the function printNumbers() inside itself.

Create a free account to access the full course.

By signing up, you agree to Educative's Terms of Service and Privacy Policy