INTRODUCTION TO RECURSION

Vikas Tiwari
2 min readOct 13, 2021

Let us start with the jargon word called RECURSION. It will scare you as a beginner but it’s not the case I will try my best to put light on this topic.

So, What is Recursion?

By definition, the function makes a call to itself until some exit condition is triggered. we divide the big problem into chunks and try to calculate solutions of the same.

Recursion in Real Life!

You will be surprised to know that recursion is available everywhere it’s just a matter of to what extent you can think about it. We can learn about how to think recursively by just thinking about big things that are made up of combining small things. Yes, it’s that simple.

How do we define a function in recursion?

Function Definition

- Base Condition: It is the existing condition of the recursive function calling.

- Choice Diagram: Here we make choices of selecting the element or say which element to select and pass them as an argument to the function call. It is also known as a recursive condition.

Let’s take the example of calculating the Factorial of a number with recursion.

Here in the above code, we can see as soon as our n becomes zero we return 1 and come out of the block. Else we decrement our n and within we logically call our function again.

The order we follow while calculating factorial recursively.

factorial(n) ➜ factorial(n-1) ➜ factorial(n-2) ➜ factorial(n-3) ….. factorial(4) ➜ factorial(3) ➜ factorial(2) ➜ factorial(1)

This way we calculate our factorial — 5*4*3*2*1 = 120

Recursion Visualization

Image Source: mit.edu

Conclusion

You have now a basic understanding of Recursion. To get started with coding. If you face any problem feel free to reach me out on LinkedIn, GitHub.

If you like the blog make sure to take a look on my YouTube channel for more amazing stuff.

--

--