[erlang-questions] call by value

Richard O'Keefe <>
Fri May 20 11:17:38 CEST 2011


On 20/05/2011, at 3:36 AM, Martin Dimitrov wrote:

> Hi,
> 
> How will we have to interpret this citation from "Erlang Programming" by
> Francesco Cesarini and Simon Thompson (page 30):
> 
> "All calls with variables in Erlang are call by value: all arguments to
> a function call are evaluated before the body of the function is
> evaluated. The concept of call by reference does not exist, ..."
> 
> I know that variables are not copied when making a function call and are
> copied when sending messages to processes. So why do the authors say all
> calls are call by value? Aren't the function calls actually call by
> reference?

No.

Here's call by reference:

	subroutine swap(x, y)
	   real x, y, t
	   t = x
	   x = y
	   y = t
	end

	program main
	   real x, y
	   x = 1.0
	   y = 2.0
	   call swap(x, y)
	   print x, y
	end

outputs 2, 1.  Call by reference is where when you pass a
variable to a procedure, it gets a reference to the variable,
not the current value of the variable, and it can use that
to change what the value of the variable is.

It isn't that Erlang doesn't *copy* variables, but rather
that it doesn't *have* variables in that sense.

Now an Erlang *value* may be implemented as a pointer to a
record in memory, but that implementation is NOT visible to
an Erlang program.  Erlang values could be implemented as
strings "under the hood" and only the cost model would change,
not what you can do.




More information about the erlang-questions mailing list