Recursion is fun. However, in safety-critical application it is regarded as a dangerous thing (because of stack overflow I suppose?).
Imagine you need to deal with a your favorite language’s subset, that would not allow recursion – would that a disaster for you?
Formal question: for every recursive function there could be created a totally equivalent non-recursive one – is that true? is there a theorem about it or something?
Yes every such function can be rewritten as a non-recursive one. I’m not sure there’s a formal way to do it, but it is referred to as “recursion unrolling” or “recursion unfolding” in literature.
In most situations, recursion is to be avoided. Especially in the mentioned kind of systems, MISRA-C bans recursion for example.
There are a few rare situations where recursion is handy and makes the code more efficient, some binary tree implementations etc.
But the main purpose of recursion is to teach it to confused students, so that when they become experienced, they can teach it to confused students, so that-…
🙂