It is important to note that both deep recursion and deep call chains do not fundamentally cause a stack overflow. The reason for the overflow is that each call allocates a new stack stack, thereby adding to the use of stack space.
Many languages ββallow arbitrarily deep recursion / calls via tail call optimization. Some languages, such as ML and Haskell, will internally convert some (when the call is at the end of the calling function) functions / recursive calls to avoid using the extra stack space, which allows efficiently infinite recursion. The idea here is that if the call is at the very end of the calling function, the stack space of the calling functions is no longer required and can be fixed for use by the called function.
source share