r/ProgrammerHumor • • 8d ago

Meme recursion

Post image
957 Upvotes

41 comments sorted by

View all comments

Show parent comment

-10

u/[deleted] 8d ago

A big reason to use recursion is tail call optimization, which you lose by flattering it to a loop. Additionally recursion often works as a sort of reverse loop(the common example is a Fibonacci calculator as a recursive function) so while it could often technically get 'flattened' to a loop, it would require significantly more work.

12

u/Wonderful-Habit-139 8d ago

Bruh, tail call optimization is what allows recursion to be on the same level as a flat loop. You don't "lose" it when you flatten it, you end up not needing it in the first place.

4

u/RedAndBlack1832 7d ago

Tail recursion just means not creating a new stack frame... which loops don't do...