Recursion: A Programming Concept

I am a software developer and project manager; passionate about solving problems through IT.
Search for a command to run...

I am a software developer and project manager; passionate about solving problems through IT.
No comments yet. Be the first to comment.
The realm of Brain-Computer Interfaces (BCIs) is rapidly evolving, promising to revolutionize human-computer interaction. From medical applications like restoring mobility and communication to entertainment and cognitive enhancement, BCIs are poised ...
If you’re into Technical Writing or a member of any tech community, you’ve probably heard of the buzzword “DevRel.” It’s strange and confusing how so many job roles have been introduced into the world during this information age, and even confusing t...
Web scraping is an automatic method to obtain large amounts of data from websites. The data is largely unstructured and can be further refined. In this article, we will be testing the desktop application Scrape Any Website as an exercise for the HNGi...
Java is a multi-platform, object-oriented, and network-centric language. Spring Boot is an open-source Java-based framework used to create a microservice. It provides a good platform for Java developers to develop stand-alone and production-grade spr...
Going straight to coding without a ‘definite’ idea of what your app should look and feel like is very risky and can lead to unnecessary rework. It’s therefore necessary to plan a roadmap or process flow. This does not mean you have to have it all pla...
If you studied computer science or you're into programming, you most likely have come across the term Recursion. Quite a number of developer don't really use recursion, or perhaps it's the ones I know that don't use it. 🤔🤔
So, what is recursion?
Recursion is an iteration technique where a function calls itself. It's applicable when you need to call the same function repeatedly with different parameters from within a loop.
Search for Recursion on google and you get this.
Now, click against the suggested recursion.
What happens? You're redirected to the same page. That's recursion with a sense of humour.
Let's take a look at 2 scenarios
Scenario A- Loop
const pow = (x,n)=>{
let result = 1;
for (let i = 0; i < n; i++) {
result *= x;
}
return result;
}
console.log( pow(2, 3) ); // 8
Scenario B- Recursion
const pow = (x,n)=>{
if (n == 1) {
return x;
} else {
return x * pow(x, n - 1);
}
}
console.log( pow(2, 3) ); // 8
What do you note between these 2 scenarios? In scenario A, we loop over the variables i.e instructions are repeatedly executed while in scenario B, the function calls itself.
To calculate pow(2, 3) the recursive steps are as below:
pow(2, 3) = 2 pow(2, 2) => 2 4 = 8
pow(2, 2) = 2 pow(2, 1) => 2 2 = 4
pow(2, 1) = 2
Recursion reduces a function call to a simpler one until the result is gotten.
It's essential to define a breakpoint (n==1 as in the example above) else your function will run indefinitely.
Recursion is a powerful technique and is the most direct way to solve a complex problem. However, due to its shortcomings, we will need to be very careful about how and where we apply recursion.
If you like this article, feel free to comment and share. You can also reach out to me on Twitter | LinkedIn | Github
Ciao👋🏼