+ 2

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

3rd Feb 2019, 11:09 PM
Mixia
Mixia - avatar
2 Respostas
+ 3
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.
4th Feb 2019, 12:12 PM
Schindlabua
Schindlabua - avatar
+ 1
Thank you for your precise answer
4th Feb 2019, 12:29 PM
Mixia
Mixia - avatar