Recursive calls inside the receive construct

After processing a message inside a receive construct, we call the function recursively if we want to continue processing the messages. Would not it result in some issues after processing a large number of messages? For example, deep recursively calls result in “Stack overflow” in languages such as C/C++/Java.


Lucky you, this is Erlang, not Java :grin:

Seriously though, the chapter on tail recursion in Learn You Some Erlang explains it all nicely :wink:


Thanks Jan for the pointer. I now understand that Tail Recursion consumes a fixed amount of memory irrespective of the input compared to linear recursion.

But, both types of recursions in Erlang do not result in any issue such as StackOverFlow seen with procedural language. Please clarify how is Erlang able to provide that capability.


No, not both types. Body recursion has the same problem of possible stack exhaustion in Erlang as anywhere else. Tail recursion however allows for TCO to be used, which (together with the related concept of LCO) is explained in the box at the very end of this section in LYSE.