A Pretty Nice Way To Understand Recursion.

Tre'von Mitchell
4 min readJan 19, 2021

--

Learning recursion at first can have you so lost and not understanding what’s going on at all. Recursion seems very hard and impossible to get. When going into a Bootcamp or a course that will teach about recursion may or not be helpful and you just need something to help you smooth it out a little bit slower, so you can look at it clearly and differently. The first thing you need to know is that a recursion function is basically a function that is calling itself inside the block of code while it’s executing, but at some point, it needs to stop calling itself.

Now, for a recursion function to stop calling itself it needs an ending point or what we call the base case of the recursion function. Each recursion function needs a base case in order for it to stop calling itself. We can look at the base case as some type of accumulator like we would use in the reduce higher-order function. The base case of the recursion needs to be like our starting point or data type of the final result we want to return. In order for us to return that data type we need a conditional statement so that if the function reaches it, we can return that data type. Let’s look at the example at the bottom to get a better understanding.

Here we have an object called node with keys of value and next. Value has a number as its value and next has a value of another object with the same key-value pairs. So here we are dealing with a nested object and we want to get the sum of each value number inside the node object using recursion.

This is where we can look at our base case for our recursion function. We have a function call getNodeValues and it takes in an object. So let's think about it, in the nested object, it eventually hits a value of null and will stop assigning an object to the next key. In the conditional statement, we can check to see if the current next value equals null, and since we want the sum of each value key we can return that as our little accumulator which will be a number of 500. Here below is how the base case would look.

Now that we have the base case set, we need to call our function in a Recursive Case. Each recursion function also needs a recursive case where we call the function. How can we call the function in the recursive case? Well, our function takes in an object as its argument and since we are dealing with a nested object, we can call our function taking in each nested object from the next key inside of node using the dot notation. It will eventually hit the base case that we created earlier and return a number. Since our goal is to add each value to get the sum, we need to add the number from our value key to our function call. Below is how our recursive case would look like.

So let’s break this down some more. The last function call would hit the base case and return its value number which in this case is “500”. 500 will go in some type of call stack and be added to the number that came before it from the nested object, which will be 85. Then 85 will be added to the number that appeared before which is 11 and so on. You can also look at it as the function call will be replaced with the number 500 and be added to the node dot value. Node dot value can be replaced with 85 then 500 will be replaced by 585 and will be added to 11 which replaced 85 which will go on until there are no more values inside the nested key. The final result should be 651 when adding each number from the value keys. Please practice more recursion prompts to get better at recursion functions and thank you for reading.

--

--

No responses yet