Editing Recursion
The edit can be undone. Please check the comparison below to verify that this is what you want to do, and then publish the changes below to finish undoing the edit.
Latest revision | Your text | ||
Line 81: | Line 81: | ||
There is a caveat with recursive programs: Each function call takes up stack space. The deeper you recurse, the more likely you are to run out of stack space and crash your program. This makes recursion useless in most programming languages. | There is a caveat with recursive programs: Each function call takes up stack space. The deeper you recurse, the more likely you are to run out of stack space and crash your program. This makes recursion useless in most programming languages. | ||
However there is a compromise: If a return in a function is just a call to another function then | However there is a compromise: If a return in a function is just a call to another function then return is a 'tail call'. Languages that implement tail call optimization will re-use the current function call's stack for the tail call, solving the issue of stack space. | ||
All the examples on this page use tail calls and run in Lua which implements tail call optimization. This means every program on this page is immune to stack overflows. | All the examples on this page use tail calls and run in Lua which implements tail call optimization. This means every program on this page is immune to stack overflows. |