DEV Community

gagecantrelle
gagecantrelle

Posted on • Updated on

recursion function

In JavaScript there are many ways to loop through you code causing certain lines of code to repeat. Normally you would use a for, for-in, for-of, and while loops(https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Loops_and_iteration). But today I will talk about a different type of loop, Recursion. It is a function that will call it self in side of it self. This causes it to loop through it self until it stop the loop it self.
Image description
In the example you can see that the function test is taking in an array of numbers. then returning a new array containing the same values. By returning the function it cause the function to run again. it will keep running until it return something else. The if statement(base case) is checking the given array(arraynum) length. When it length reach 0 it will return a different array(newarr) and end the loop. By returning test(arr.slice(1), newarr) (recursive case)the slice method will remove the first value in the given array. allowing the if-statement to run, ending the loop when the give array length is equal to zero. Since the variable newarr will be reset back to default when the function repeat. You would have to put in the function as a argument. allowing the function when it call it self to set newarr to equal it current value before it loop.(https://www.freecodecamp.org/news/recursion-in-javascript/)

Since recursion is just a function the call it self, you still then to add in parameters and arguments. Normally when creating a function you have the option to not give it any parameters or arguments. But due to recursion you have to or give it a const variable. Parameters are place holders for a function that you use to give argument name. They are made when making a function, allowing you to hold a copy of the given argument. Arguments are the variables the are given to the function. the parameters will copy the given argument's allowing the original variable not to change. Const variables are values that when set can't be change to anything else. event if it put in a recursive function it will still keep it values. If you set the const to be an array or object you will still be able to give and remove value. so, when the function call it self again because const wont change it values, you can use that in instead of a parameters to hold an array of values. For example in the image above instead of given the function a parameter that equal an empty array. you can put that array in side of the function as a const variable.

Now probably wondering why and when you should use a recursive function. Since normal loop are easy to set up and take less time to set up, why would you use recursion? Because it a function it can be called multiple times. allowing you store lines of code that you want to run at Cretan points in you code. this makes it easier to work on you code, by stopping you from recreating certain lines of code over and over again. it also make your code look cleaner. but since a function can hold any line of code you can put in a for loop instead of truing it in to a recursive function. depending on what your doing with you code you may need to use a recursive function in steed of a regular loop. for example in the image below we are trying to access an object and change certain values.
Image description
Normally when you look at this code, you think the the function will only work on half of obj1. But it works on every part of this, similarly how you run a function in a normal loop. it works the same for a recursive function.

(for more information about recursion use the provide links that I found while researching recursion. the two image was provide by me, one while I was running tests on recursion, and the other image was a toy problem that my teacher give me wile going over how recursive functions works. this blog post was created for an assignment for class)

Top comments (0)