Using an explicit stack can also be a good idea because several languages have a recursion limit, or some implicit restriction. Often those aren't very easy to modify either. So an explicit stack is far better since the alternative can be that you just crash fairly arbitrarily.
Re:How Not to Teach Recursion
by polishdude20 on Sunday January 03, 2021 @01:32PM
(#25623786)
Can you explain the scheduling part in a bit more detail?
But in most languages, Euclid's Algorithm is actually better implemented with a loop, and it brings me to one thing I realized about recursion that isn't commonly taught.
At a low level, recursion is implemented with a stack, so if you have a stack, you don't actually need to call functions to implement a recursive algorithm, in fact, many recursive algorithms don't need a stack at all (tail recursion). A recursive function call just means "remember my position and all local variables" and a return means "go back to where I was".
One advantage of this "machine first" thinking that it can help with optimization. By thinking about the stack, you may realize that you don't need to remember all local variables, that the call is always made from the same place so you don't need to remember your position. You may even realize that a stack, which is the natural data structure of recursion is not actually the best, for example, an array might be better, which, by the way, is one of the basis of dynamic programming.
And if you get to functional programming later, understanding what the machine does behind the scenes can give you a better grasp of the idea.