Understanding Recursion ( Deja Vu )

With real life examples and internal working .

Understanding Recursion ( Deja Vu )

Situation

Imagine You are in college and your professor has given you a task to write an assignment of 40 pages, you have two ways to do it:-

1. Write all 40 pages on your own ( which will be hectic and a waste of time and energy )
2. Write 10 pages and outsource the rest 40 pages to someone (e.g.:- friends, colleagues, paid workers, and many other options )

Let's consider situation No. 2 where you outsource the rest of the work.
Assume you have a friend named Dinesh ( replace it with your friend's name for better understanding )

Let's understand it with some steps:-
• Step 1: You said "Hey Dinesh I will write 10 pages of my assignment can you please write the other 40 for me? ", As he is your good friend he didn't deny it.

• Step 2: Dinesh Accepted the task but it will be hectic for him to write 40 pages. he too had a good friend (Name: Rahul ), so he did the same as you have done ( outsourced to his good friend )

• Step 3: Even, though it is a bit work a load for Rahul to write 20 pages, he writes 10 pages and outsources the remaining work to his friend ( Suman )

• Step 4: As Suman has only 10 pages to write it is an easy task for him, so he doesn't require his good friend to help


Before moving ahead Let us understand the above analogy with technical terminology side by side

  • The Boxes with the names of your friends are FUNCTIONS

  • The numbers inside the box are the parameters of the function


Assignment Submission Day

• Step 1: As the day has arrived to submit the assignment (you) have asked your good friend ( Dinesh ) to return the task (remaining pages ). Observe you are waiting for Dinesh (Function) to return the work you have given him you don't want to know how Dinesh (Function) has done the assignment, you are just expecting him to return the task you have given to finish.

  • Step 2: Dinesh goes back to his home (Enters into its own function body ) he gets the 10 pages but he is waiting for Rahul to return the rest of the pages that he has given him and even Dinesh doesn't even bother how Rahul (Function) has done the task, he just need Rahul (Function) to return the remaining work

  • Step 3: The same goes with Rahul (Function) he returns back his home (Enters its own body ) and waits for Suman (Function) to return the work he has given so that he can merge the work ( Work done by him + Work done by Suman )

Step 4: IMPORTANT Suma gets back home ( Enters into its own function body ) and collects the 10 pages he has written and observe here he DOESN'T WAIT FOR ANYONE to return the remaining work as he has only 10 pages to write.

In the above figure, Suman returns the work done by him and observe suman ( Function ) gets Disappear ( function gets removed from the stack ) after their respective work is done

Step 5 :

Rahul adds the work done by him and Suman and returns to Dinesh because Dinesh is expecting 20 pages from Rahul ( Dinesh Function is waiting in the stack for Rahul Function to finish its work )

Step 6 :

Dinesh adds the work done by him, Suman, and Rahul, and returns it to You because you are expecting 30 pages from Dinesh( You Function is waiting in the stack for Dinesh Function to finish its work )

Step 7:

As Dinesh has returned the work you have given him, you add all the pages ( 10 + 30 given by Dinesh Function ) and return the total pages to your lecturer who assigned you the task

Step 7: Final Step

Task Done !!

Overall View


Definition :

Recursion is all about Dividing the big problem ( in this case 40 pages of assignment ) into smaller problems.

Pre-requisites:

  • Working of functions and method calls.

  • How do they return the value after completing the execution

Things to remember :

  • You Should know the breaking point/base case where the function doesn't call the function again

  • You should know what will be the parameters that need to be passed to the future function calls

  • You should know the return type of the function because you should return the work done by a function above who are waiting in the Stack to get the current function to finish its work

Let's see the action in the stack

Method calls and their associated data (such as local variables and method parameters) are managed on the call stack during program execution.

As shown above, this is how functions rely on their above function until the base case is reached

Function when they get removed from the stack


Ending Note :

This is just an overview I tried to explain with an analogy because I believe learning becomes easy when you relate it to real-life situations.

There is a lot more about recursion to learn, the best way to do it is by using :

  • Pen and Paper

  • Drawing the recursive tree

Source to learn :

Recursion should be understood in depth and detail and it can be covered in a single blog, I request you to refer below resource for a better understanding

To learn more about Data Structure Algorithms in detail follow the below playlist

IMPORTANT: Recursion cannot get into the head until solving a good amount of different basic to advanced problems, refer to the link below to solve the problems

Problems Link

Thank You for reading!