How does implict return work in functions?

Not only does it have implicit return, there is no alternative, no return keyword in Elixir.

Functions in elixir (and erlang) always return some value. That value can be simply nil or :ok or whatever the output of the last statement executed in the function, but it will have a return value. If you only care about the side effects of calling the function, you may ignore its return value at the call site.

Note that if, case and cond all return the last value from their executed path as well.

The concept of referential transparency has some relevance here; though elixir doesn’t have strict referential transparency because of side effects, when there’s no explicit or implicit message passing to other processes or IO, I believe it does. Just thought worth mentioning, you asked “how does that work”, and depending on what sense you meant that in, there are a lot of answers, but it works great!

5 Likes