Editing Recursion

Warning: You are not logged in. Your IP address will be publicly visible if you make any edits. If you log in or create an account, your edits will be attributed to your username, along with other benefits.

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 that return call is a 'tail call'. Languages that implement tail call optimization will re-use the current function call's stack for the function you're calling, solving the issue of stack space.
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.
Please note that all contributions to JookWiki are considered to be released under the Creative Commons Zero (Public Domain) (see JookWiki:Copyrights for details). If you do not want your writing to be edited mercilessly and redistributed at will, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource. Do not submit copyrighted work without permission!

To edit this page, please answer the question that appears below (more info):

Cancel Editing help (opens in new window)