Check out a free preview of the full Data Structures and Algorithms in JavaScript course:
The "Template for a Recursive Function" Lesson is part of the full, Data Structures and Algorithms in JavaScript course featured in this preview video. Here's what you'd learn in this lesson:

Bianca summarizes her recursion introduction by sharing the recipe for a recursive function. You identify the base case, identify the recursive case, return when appropriate, and write procedures that bring you closer to the base case.

Get Unlimited Access Now

Transcript from the "Template for a Recursive Function" Lesson

[00:00:00]
>> Bianca Gandolfo: Recursion, so that's cool, we explored that. Here's a recipe that you've probably heard before. You have an if statement of some base case. Otherwise, do some recursion and then return or, like we were doing, return some value, right, which you can set up here. You can set out, and you can accumulate it.

[00:00:22] There are different ways of preserving that value, cool. And I almost forgot, there's actually the work part that you wanna do that's useful. So far, examples have been super not useful. It's just an oversimplified version, and so ideally in this if statement, you want to get to a state that's closer to your base case which is gonna be the smallest,

[00:00:54]
>> Bianca Gandolfo: Which is at the end or where you want your recursion to end. In this case, we want it to end after three loops, right? So that's our base case right here and return, right? We have some condition that gets us closer to our base case. Otherwise, if we didn't do that, we would still have an infinite loop and we would have problems.

[00:01:20] And so that's what that means.
>> Bianca Gandolfo: And you need a return where needed.
>> Bianca Gandolfo: Cool, yeah, question?
>> Speaker 2: In the previous example, the callMe anytime returns undefined, right?
>> Bianca Gandolfo: In this example, it doesn't. So here, it returned undefined. When we add the return statement on the last line there, it's gonna return loops, because it's gonna return up the stack, just like we were doing over here.

[00:02:03]
>> Bianca Gandolfo: So we have the stack as it returns. It's gonna pop off and it's either gonna return something or not. That's what you can do. You can either return something or it just disappears. Well, you could do side effects. But in this case, it just disappears.
>> Speaker 2: If we don't use return until 3 loop, it will bring us on undefined, right?

[00:02:25]
>> Bianca Gandolfo: Yeah, exactly, so if there's no return, it's just gonna return undefined, cuz that's the default.
>> Speaker 2: Up to three loops, and then return loops, right?
>> Bianca Gandolfo: Sorry, I can't, say it one more time.
>> Speaker 2: Up to 3 recursion, for 3 times, it returns undefined on the third one, it returns the 3 loops, right?

[00:02:50]
>> Bianca Gandolfo: Yeah, so the very, very first one returns-
>> Speaker 2: Undefined.
>> Bianca Gandolfo: Loops, loops actually gets returned first and then if there is a return statement here, this return gets last.