What are "tail recursion" and "inline recursion" and what are their advantages?

2/3/2019 11:09:18 PM


2 Answers

New Answer


Every time you call a function you have a bit of overhead. You need to set up a "stack frame" that contains all the function's variables and which line of code to jump back to after the function is done and things like that. And when the function is done, the stack frame is teared down. It's not a lot of overhead but if you run the function a million times a second it's noticeable. However! If a function calls itself, and that recursive call happens at the end of the function as the very last thing you do, you can reuse that same stack frame and save time. This is called tail call optimization. I have never heard about inline recursion though.


Thank you for your precise answer