[erlang-questions] is there "return" in Erlang.

Jachym Holecek <>
Mon Feb 28 13:58:20 CET 2011

# Wang Wei 2011-02-28:
> Hello, I has a question about how to convert the bellow C program into Erlang.
> [...]
> I think about "case" and "if" construct, but none of it seems work fine, thanks for help.

And one more option:

  judge() ->
      judge(get_a(), get_b()).

  judge(A, _) when A == conf1 ->
  judge(_, B) when B == conf2 ->
  judge(_, _) ->

This assumes get_b() doesn't have some kind of side effects you'd like to
suppress in case the get_a() branch hits -- usually not the case in Erlang,
but if so, the solution with nested cases would be more appropriate.

Point of this is you can break your code into small trivial functions that
solve clearly defined parts of the problem, instead of writing long and
complex functions that try to handle the whole thing. Being a lazy person,
I find the former style easier to read, but YMMV.

And about return in general -- in Erlang (and other functional languages)
program is composed of expressions that get evaluated to values, there are
no statements. This is the reason why you do things like:

   A = case (X rem 2) of
	   0 ->
	   1 ->

which quite annoyingly you can't do in C. Now if you're thinking "Yeah,
that's nice, but you're still a liar because exceptions don't really
evaluate to anything, they just perform weird stuff with the call stack,
don't they?" -- well, conceptually, you can convert them (and any other
forms of control transfer) to tail function calls[1] and everything is
back to normal.

	-- Jachym

[1] Nice presentation on this by Marc Feeley, author of Gambit-C Scheme:


    Quality of the recording isn't great, but the content is worth it.

More information about the erlang-questions mailing list