DEV Community

Abddelrahman Saad
Abddelrahman Saad

Posted on

Introduction to recursion (Part 2 Practical)

Important Note
Please note that there is a first part that must be read before reading this part
Read from here

Now let's get our hands dirty and make a simple function to demonstrate our idea.
imagine you need to count down from a number till 0 what would you do!!
Yes, a loop gets the number and subtract 1 at every iteration.

so it would be something like that

First, we make a function call count down take the number and make a loop inside it

function countDown(num){
  for(let i = num; I > 0; i--){
     console.log(i)
     console.log('Done')
  }
}
Enter fullscreen mode Exit fullscreen mode

If you invoke this function with a number let's say 4 the output would be
4
3
2
1
0
Done

NOW let's do it recursively

Here where things get exciting🤩

I'll write the function but don't worry I'll walk through it step by step

function countDown(num){
  if(num <= 0){
    console.log('Done');
    return;
  }
  console.log(num)
  num--
  countDown(num)
}

countDown(3)
Enter fullscreen mode Exit fullscreen mode

Please don't freak out, and have another look🧐

The first thing it will check if num is less than or equal to 0 and it's not -it's 3 - then it will skip this part as we also.

Then it will print the number in the console - 3 -
then subtract 1 from the number so it's going to be 2 instead of 3,
until now this is normal to you off course.

In the next part, it will execute itself again but this time with the subtracted number which is now 2 so the countDown function would invoke again with different input.

The first role you remember it arent you?🤔

Different input

The first role to make the function recursion it has to work every time with different input

You passed the first part I'm proud of you 🥳👏

Now let get back to our function.

The function will check again if num is less than or equal to 0 and it's not - it's 2 this time - then it will skip this part again
then it will print the number - 2 -
then subtract 1 from the number which is becoming 1 now after subtraction

and again it will execute itself with the new input 1.

it will do the check again but it will skip the check because it's not less than or equal to 0 - it's 1 -
then print the number and subtract 1 from the number and execute itself again but NOW the number became 0.

Now the if statement will work because the number is equal to 0

This is the second and final part of the recursion functions.

The base case

the function must have a time where it stops and returns a value, or we're going to have an infinite loop.

it will return nothing here but we do it because this part is the most important one and without it as you know it won't stop and your program going to crash undoubtedly

HOLA now you've understood the recursion functions and I didn't cheat you it's simple, right? 🥳

Let's be honest this not what recursion meant to do but here for the sake of simplicity and learning but in real-life scenarios, you would use recursion functions as we said in a big number of items

If you interested I can do an advanced one please let me know in the comments and I'll do it as soon as possible

Thank You, and have a productive day ❤️

Top comments (0)