DEV Community

Adam Crockett πŸŒ€
Adam Crockett πŸŒ€

Posted on

But what is: Recursion? Recursion? Recursion? Recursion?

Google recursion and you will find out exactly what it is, but if you have a moment I will try to explain what it is without going round in circles.

// Recursion is the name for an activity that could go on and on.
Enter fullscreen mode Exit fullscreen mode

True recursion never ends, in JavaScript that should mean a function calls itself over and over.

Now the effect is that a synchronous recursive function will block the program bellow this code (or above) from running until the recursion ends and we leave this function behind for garbage collection. Rucursion never ends unless we provide an exit condition otherwise you will get an overflow, imagine your program is a bathtub, it can only contain a finite amount of water. We say that every 1ms we turn the tap on and then off again filling the bathtub with a drop of water, the water represents memory used and the tub well that's the total memory available to your program 😊 (I know it's not quite like that) if we don't say "when the bathtub is full or reaches maybe 75% capacity, stop running the tap" we get an overflow, more memory was used than was available to your program, without such limits perhaps your computer will catch on fire πŸ”₯.

Here is a recursive function:

// Exotic fruit tree from ebay
const pearAppleTree = {
   fruit: 'apple',
   tallerBranch: {
      fruit: 'pear',
      tallerBranch: null
   }
}

const treeClimber = (branch) => {
   if (branch.tallerBranch) { // exit condition
       console.log(branch.fruit);
       treeClimber(branch.tallerBranch);
   }
}

treeClimber(pearAppleTree);

console.shout('not blocked will carry on with program')
Enter fullscreen mode Exit fullscreen mode

I'm excited to tell you that we climbed to the top of the tree and found some fruit all the way up apart from the top so we stopped and climbed down.

On the first branch, we saw an apple 🍎
On the second branch, we saw a pear 🍐
Then somebody shouted, "your all done climbing that tree? Okay.. well I'm just going to carry on with what I'm doing, il call the garbage person to deal with that fruit because all you did was log it to console for some reason, k bye."

Hope that helps you learn some more programming experience, if not I failed, I failed, I fai...

Top comments (12)

Collapse
 
briwa profile image
briwa • Edited

Google recursion and you will find out exactly what it is

When you google that, you'll find one of Google's witty jokes instead:

πŸ˜‚ or wait, maybe you actually meant to refer to this and I was completely missing the point...

Anyway, nice insights on the recursion! Thinking in recursion helps me reuse many parts of the code and simplify what could be a complex nested process.

Collapse
 
adam_cyclones profile image
Adam Crockett πŸŒ€

Surprise πŸ₯³πŸ€£

Collapse
 
pankajtanwarbanna profile image
Pankaj Tanwar

Interviewer - Explain recursion, I will hire you.
Me - Hire me, I will explain recursion to you.

Collapse
 
iarmankhan profile image
Arman Khan

It's more like deadlock

Collapse
 
adam_cyclones profile image
Adam Crockett πŸŒ€

Indeed technically it's recursive as it does just go around in circles.

Collapse
 
adam_cyclones profile image
Adam Crockett πŸŒ€

The interview lasted for 65 years, unfortunately everyone died.

Collapse
 
marcoslooten profile image
Marco Slooten

Nice article, great to see some tree traversal. I also published an article about recursion three days ago (marcoslooten.com/blog/how-to-use-r...) and I wasn't sure if I could explain it with a tree so I used other examples, but you definitely made it understandable!

Collapse
 
adam_cyclones profile image
Adam Crockett πŸŒ€

Good on you Marcos, il certainly checkout your blog.

Collapse
 
coderific profile image
Yip

Another great recursion explanation at codeburst.io/learn-and-understand-...

Collapse
 
adam_cyclones profile image
Adam Crockett πŸŒ€ • Edited

Il take the implication that I did a good job too haha thanks πŸ˜… I tend to write silly impactful 2min reads as I only blog on my phone.

Collapse
 
mikenikles profile image
Mike

Also check this article for a great explanation: dev.to/adam_cyclones/but-what-is-r...

Collapse
 
adam_cyclones profile image
Adam Crockett πŸŒ€

Ha!!! Very good πŸ¦„