DEV Community is a community of 636,824 amazing developers

We're a place where coders share, stay up-to-date and grow their careers.

Recursion Recursion Recursion

Coding has been a challenging journey, and in the beginning I really struggled with understanding this concept called Recursion. My name is Harley Padua, and I will attempt to unpack it a bit. But before I talk about what Recursion is, I recommend you read my first blog about Recursion here.

When I was introduced to this topic, I couldn't help but link it to one of my favorite movies: Inception. The film dealt with this idea of dream traversal, or more specifically, traversing a dream within a dream within a dream in order to retrieve information, with a kick to break out of the dream.

Recursion is a function that calls itself to accomplish some goal, with a base case to break the loop. See the similarities? And much like Inception, this concept can be very confusing the first time around. So let's take a look at an example:

``````function shootGun(sound, num) {};
``````

Here we have a function called shootGun that takes two parameters: a string that represents a sound the gun makes and a number of times we want to shoot.

So first things first: a base case. I mentioned that a base case is what will end the recursive function. Without a thorough one, the function will run on forever and nobody wants that. So, let's think on it. We know a gun will only fire as many times as the trigger is pulled. So if there are no more pulls to the trigger, the gun will stop firing. Since num represents this concept, our code should look like this:

``````if (num <= 0) {
return "";
}
``````

When we have no more shots, we want to return an empty string, since there is no sound to account for. Next is the recursive case. We know we need to have a return statement because functions default to undefined without one. Call the function with it's parameters. Since our base case checks for when num is less than or equal to 0 in order to make sure our function doesn't run on forever, we have to make sure we decrease num so that it will reach that qualification.

``````return shootGun(sound, num - 1);
}
``````

Okay, great! But we are missing something. As it is now, this function isn't doing anything with the sound parameter. Each time the function is called, it just decreases the number, but we want it to print the sound the gun makes as many times as we fire. In order for that to happen, we have to save sound, and add it to each function call. Put it all together and it should look like this:

``````function shootGun(sound, num) {
if (num <= 0) {
return "";
}
return sound + ' ' + shootGun(sound, num - 1);
}
``````

Now we have a rootin', tootin', gun shootin' recursive function! If we call the function with 'bang' for the sound and '3' for the number, the function will return 'bang bang bang'! And I'll impart some knowledge that helped me a lot when it came to building more complicated recursive functions: just assume your function is working. As long as your base case is thorough enough to reach a conclusion, play around with your code and see what recursion can do.

Hopefully this has helped shed a little light on what recursion is, and thank you for reading my very first blog!

Discussion (6)

That was cool demo to click "here", will read the whole post 😀

Jayant Lohani

Had only read a little bit about recursion in C and C++. Thanks for refreshing the topic. The blog was great. Short and precise. The starting was really nice. I don't think that anyone will be spared by that "Click here" example. 😅 😆
Overall great blog.👍

Simon Aronsson

Your practical example made me giggle. I think Wikipedia has it as well. 🥳

Chidiebere Ogujeiofor