I've just accidentally came across this answer about inlined functions and I'd like to know how this affects call stack. But I can't add comments because I don't have enough rep so I decided to ask this question. Also recently I discovered Tail Call Optimization (TCO) and now I want to learn more about this topic. And since TCO is also related to call stack, I actually have 2 question.
- AFAIK every function call adds a new stack frame into a call stack. But what about functions inlined by a compiler? As this IBM article says:
An inline function is one for which the compiler copies the code from the function definition directly into the code of the calling function
It makes me think that in this case we don't really use a function at all. We use it's inner code but an actual function call doesn't happen. So does it mean that a new stack frame isn't created for such function? Hence "every function call creates a new stack frame" is a false claim?
- TCO allows a runtime to use the same stack frame for multiple recursive function calls. New stack frames are not added into a call stack. Instead the runtime just swap data inside the same stack frame - data that was necessary for the previous recursive call is replaced with data that is necessary for the next recursive call. Does it mean that in this case we can intentionally create an infinite recursion and we won't have a stack overflow since we always have a fixed amount of stack frames in a call stack?
Those 2 question are not language specific. And since different languages have different compilers, and every language can have multiple compilers that work differently, I'd like to get answers about any language and any compiler you used and know about.