Let's talk recursion šŸ˜€

there is nothing but the compiler left to execute that function and it is getting executed otherwise it would show a function as a final value and not the results of it

A new function is one of the possible return values of this function. That function may be later called. At no point does any function call itself.

const addThree = addTogether(3); 

Right here, the return value is a function.

im telling you the language of calling itself meaning it executes itself at the heap instead of just returning an orphaned function

Iā€™m telling you that you are wrong. A function is returned.

const addThree = addTogether(3);
let someResult = addThree(5); // normal function call

There is no recursion involved in using the returned function.

if you log those variables they show as uncalled functions becuase the heap discards unused variables

functions execute

Yes, this function returns another function. No, a function returning a function is not recursion.

recusion a function being defined is applied within its own definition

Nope. Recursion is a function calling itself.

A function creating a new function roughly fits under the category of code generation.

wellā€¦it is what it is I guess

This seems like a pretty good explanation of a recursive function. Technically, it does not have to call itself but it still has to be involved in a cycle of functions which come back to itself.

The function discussed here does neither. Hence, not recursive.

1 Like

I donā€™t know that Iā€™ve seen practical examples of indirect recursion in the wild. Do you know of any in actual production code? Iā€™d generally be inclined to slap that down in code review over fragility concerns, but I also just push for sparing use of recursion in general.

Edit: I guess some parsers use it?

No, I havenā€™t, and to be honest, I didnā€™t even realize that was a thing until just now :slight_smile: But since the function clearly isnā€™t calling itself that would be the only other argument for it being recursive and so I wanted to include it in the definition to make it clear that that argument isnā€™t valid either.

1 Like

Fair enough. The Google tells me that some parsers use indirect recursion, but parsers run on magic and tears, so Iā€™d believe that. Iā€™ve only used direct recursion myself, but I havenā€™t (fortunately) needed to make more than what amount to toy parsers.

1 Like

Yeah, this discussion definitely meandered off point a bit. This is probably the best breakdown of that return value case in the discussion so far:

which executes by itā€™s own definition

As @bbsmooth mentioned, this is off topic. If you want to ask questions about the differences between functions writing other functions and recursion, please make a new thread.

well it explains what is happening and why this solution is working.

No. This is not recursion. Further discussion about what is recursion needs to be moved to a different thread or it will be marked as off topic conversation. This is derailing the original question.

im simply stating why there is a return value in this instance and not discussing recursion at this point. I though that was clear so I appologize if it wsnā€™t

Not on purpose.

1 Like