Understanding recursion
Recursion - how to not get overwhelmed
I am sure that throughout your programming journey you have encountered recursive functions. It is one of the more challenging concepts in programming for budding software engineers to grasp. Even though one might rarely encounter recursion in their day-to-day work, it is a crucial concept in the realm of computer science. So I have written this post and hope that it will help you understand this topic better.
We should start from the very beginning. What is recursion?
Recursion is the process of a function calling itself. You can think about it as some sort of a loop. As you might already know, every loop has to have a condition to stop calling itself. It is the same in case of recursive functions, the condition stopping the loop is called base case. If you forget about specifying the base case, the loop will run indefinitely and you will encounter stack overflow.
When should you use it?
You could use recursion when you have a problem which can be divided into smaller, repetitive subproblems. A great example of this kind of problem is writing a function which will raise a given base to the power of a given exponent.
Graph visualising the recursion process
What is the time complexity of the recursive function?
For one-branch recursion the time complexity will be the big O(n). It is due to the fact that to compute the result we would have to go through n steps.
The space complexity would also equal to the big O(n) as we are invoking this function n times and every function takes space in the memory.
This space complexity is very inefficient, recursive function implementations in JavaScript are also typically three times slower than loops.
Why would we use it then?
I think it is due to the fact that some problems are easier and more elegant to solve with recursion than loops.
If you don’t understand it yet, it is okay. Understanding and implementing recursion takes time, so take yours🙂
Have fun with your future programming endeavors!
E.N.