What is recursion? What is recursion? What is recursion? What is recursion? What is recursion? What is recursion? What is recursion?
Simply put, recursion is when a function calls its self within its function body until it hits a base case. The function will continue to call itself until it gets to the output it wants. It may not sound complex, but recursion is a difficult topic to wrap your head around when you first encounter it, but like most subjects in Javascript, the best way to learn, is to code. In this blog I will help you gain a better understanding of what recursion is.
Here is an example of the most bare bones recursive function you can make.
Though this is not the usual set up, it demonstrates how simple a recursive function could be. Usually, a recursion is set up with a base case and a recursive case. The base case acts as the stop condition and without it the function will continue to run over and over again; here you can run into something called an 'infinite recursion' and this can crash your tests or browser. The recursive case is where the function actually calls itself. Here is another example of a recursive function I pulled from a problem I worked on in bootcamp.
Our function takes some number and finds the factorial of it.
In our function we can see the base case that stops the function from continuing ('if the number is negative return null'). This means the last value returned should be some positive integer. But before that happens we must hit our recursive case; ('if number is zero or one return one else return the number times the invocation of calling 'factorial' on the number - 1'). So, the function will take a number and will multiply it by itself minus one until it gets to the number one. A simple problem but it shows the capabilities of using a recursive function.
Why use recursion, or not?
Recursion is useful when you are doing a task over and over again. Like the previous example where you had to multiply multiple times but didn't necessarily want to use a loop. Not to mention recursive functions are able to solve deep nested problems with less code so the readability of the code is much simpler. But, 'recursive implementation does consume more memory than non-recursive ones' meaning slower execution time for code blocks. This is because there are a lot more steps involved with the implementation. Here is a link to a video that helped me understand the call stack and its limit in regards to recursion.[(https://www.youtube.com/watch?v=mMEmNX6aW_k)]
Understanding Recursion
Another demonstration of recursion I find useful is Sierpiński triangle.
This triangle 'is fractal with the overall shape of an equilateral triangle, subdivided recursively into smaller equilateral triangles.' This is a good visual on how recursion works. Recursive functions call them self multiple times to 'solve solve smaller instances of a problem'; that is the same thing happening here with the triangle. As you zoom in, the triangles get smaller but they maintain the original shape of the triangle.
Conclusion
In conclusion, recursion is a very useful too when applied to the correct problems. It can feel challenging to understand the moving parts of recursion, but there are real-life practical examples that can help you with understanding the use behind this power problem solving tool.
Top comments (0)