Local Stack vs. Call Stack – Differences and Usage

recursionstack

On the matter of Recursion:

When you create a recursive function, you create a call stack. Ok, no problem; However, a comment on this page (look for comments by "LKM") triggered my curiosity (and google was no help):

  • What is a local stack?
  • Why would it be faster than a call stack?
  • How would you actually implement it (pseudo-code, js/php/ruby/python are ok)?

Subsidiary question (might deserve another question, but I don't know on which (.*\.)?stack.*\.com to ask):

In these conversations about programming, I often see the "recursion" theme and how bad/newbie programmers don't grok it. I'm self-taught, and I never understood what the big fuss is all about. I use recursion a lot in my everyday coding, both for solving problems and sometimes just because I think they are beautiful. But these articles make me feel like maybe there is something there I just don't see. So:

  • What's all the fuss about recursion? What's there not to grok?

Best Answer

A "local" stack, as that comment uses it, means stack implicitly declared as a local variable. I would differentiate it from the call stack by calling it an "explicit" stack.

Iterating on an explicit stack can be faster than recursion in languages that don't support recursion related optimizations such as tail recursion. Or a language may have limited stack depth.

Here is a good explanation with an example by Eric Lippert.