Recursion: Recursive Steps
Recursion: Recursive Steps
Not many people can argue there's a problem duct tape (or WD-40) can't solve.
- Hole in your shoe? Duct tape.
- Fan broken? Duct tape the blade back to the base.
- In need of a prom dress? Duct tape your way to a scholarship.
Some things (like duct tape in real life and recursion in computer life) are inherently useful no matter what kind of situation you're in.
In recursion, the goal—the end condition—is known as the base case. It's like that carboard tube you hit at the end of a roll of duct tape; no more calls get made (just like no more duct tape can be added to that kicking Prom dress). Say the problem is the fact that you need a prom dress and you only have a couple of rolls of duct tape to make it happen.
As you build the dress, you're working your way down the roll of duct tape, which is exactly what happens in a recursive call. When you call a recursive function, it's going to call itself again and again until hitting the base case (the cardboard roll at the bottom of the tape).
Unlike duct tape, though, if a recursive method calls itself, it has to stop when it hits the base case and work its way back up to the original method. That's about where the metaphor ends so we're going to abandon it for now. (Sorry, duct tape.)
When the base case becomes true, the result of the function floats back to the top of the call stack, the list that keeps track of all those methods called in the recursive procedure.
Wait, what?
Don't worry, we'll explain it soon…like in-the-next-paragraph kind of soon.
The whole point of a recursive function is to call itself to get the answer. That might sound like a crazy, dangerous thing and…it kind-of is. Like if you've got this code:
FUNCTION growPlant (SeedCount) { SeedCount = SeedCount * 3 growPlant (SeedCount) END FUNCTION
See anything wrong? Look really closely at the statements. Try writing out how it works on paper and you might get:
growPlant(1) → growPlant(3) → growPlant(9) → growPlant(27) → to infinity
That's the definition of infinite recursion. Just like an infinite loop, infinite recursion's going to run forever. Unlike an infinite loop, it's usually unclear that the code's going to run infinitely.
Not good.
That's where our hero, base case, swoops in to save the day. It's the final step of recursion, the condition where the function knows that it can stop calling itself. The recursion then ends because the function…stops calling itself. Your program's saved—and ideally you get the answer to the question you asked.
To help it save the recursive day (one psychedelic image at a time) the base case has the recursive case. The recursive case's job is to chip away at the data you're working with. That way, the recursive case can get to the end of the process instead of continuing forever. If there's more to be done, the recursive case calls the method again until it reaches the base case.
If you get to the end of the function and the conditions you want still haven't been met (like when you've got 27 duct tape dresses but you're holding out for 100), then the recursive case runs your method again to get it closer to that base case.
That recursive step isn't always the most effective, though. Sometimes it needs a short circuit to give the function an early exit in case whatever data you started with won't ever end up at the base case. The short circuit kicks the code out of the recursion before it has a chance to infinitely recurse.
If you're into that kind of thing, you could think of a short circuit as a conditional. In fact, it's the short circuit that lets you define the conditions for the base and recursive cases.
Now that you've looked at some terms in the Dictionary of Recursion™, let's look at how you can recreate the seed problem using recursion.
FUNCTION growPlant (SeedCount): IF SeedCount >= 100: RETURN SeedCount END function END IF #Short Circuit IF SeedCount < 0: END function END IFSeedCount = SeedCount * 3
growPlant (SeedCount)
END FUNCTION
The growPlant()
function here uses a standard format for recursion with a single recursive case. The first thing the method does is check to see if it hit the target. If it did, the method's going to return that value and do nothing else. Yep, that's a base case if we've ever seen one.
Since the method definitely ends by returning when seedCount
is at least 100 and the short circuit catches any potential infinite recursion, the rest of the function is basically an ELSE
to that first condition. There's no need to write the ELSE
. You can totally still throw in the other condition. Whether you end up writing it out or not is a stylistic preference, really.
The last piece of the function is the recursive case because…it calls itself (growPlant()
). Check out how it returns the returned value from the call of itself.
Please, hold your applause till the end of the learning guide.
The returned value from the current function call (which is based on the return value of the last function call) is then used in the next function call—on and on until it hits the base case. That's a common thing in methods that use recursion to calculate mathy things. The code will run down the rabbit hole, repeating the recursive case while calculating new return values, (ideally) approaching the right value.
When it reaches that coveted value, it climbs back through that rabbit hole, up to the top. Now if only if Alice had spent less time staring in the mirror and more time duct taping dresses writing recursion. Recursion sounds much easier than trying to figure out which bottle will help her shrink, ioho.