call by value:

CALL P(A);

P: PROC (X BYVALUE);  ...

/* this is not ALGOL ... */

the actual parameter A (which may be an expression)
is copied to the formal parameter X of the procedure P;
X is much like a local variable of P. If P changes X, only the
local variable X is changed. A remains unchanged.

call by reference:

CALL P(A);

P: PROC (X BYADDR);  ...

/* this is not ALGOL, ALGOL doesn't have call by reference */

the address of the actual parameter A (which can only be a variable)
is passed to the procedure P as parameter X. The address of A is evaluated
only once. Every time when P references X, it references in fact A.
So every change to X is in fact done to A.


call by name:

CALL P(A);

P: PROC (X BYNAME);  ...

/* this is not ALGOL ... */

A can be an expression or a simple variable.
At every occurence of X in the procedure P,
the expression A is instead evaluated ... as if the
parameter A (or the expression A) would replace
X (the "name" X) in the source code of P ...
but A, of course, is evaluated in the scope of the caller.

If you pass a single variable to P, call by name is much
the same as call by reference. But if you pass more complicated
expressions (involving array references or functions), very
strange things can happen.


(IMO, this "call by name" is a very mathematical mechanism of
parameter substitution; the call by reference in contrast is a somehow
"technical" mechanism. We got so accustomed to this call by reference
in the last 40 years, that call by name seems very strange to us now,
although for a mathematical trained person it could be very natural).

HTH, kind regards

Bernd



Am 09.02.2018 um 22:23 schrieb Charles Mills:
As I understand it, call-by-name means the following:

Suppose for example if you coded a subroutine that expected some sort of 
parameter, and called it with a random number function, the random number 
function would (in most languages) get evaluated once before your subroutine 
was called, and your subroutine would see it as a constant. If you printed it 
three times in a loop it would be the same all three times.

With call-by-name, 'RAND()' (or whatever) would not get evaluated by the caller but 
rather passed to your subroutine "as-is." It would get evaluated whenever your 
subroutine referenced it. If you printed it three times in a loop you would get three 
different values.

It's not really "call by name" but rather "call with function" as opposed to "call 
with value of function."

Charles

Reply via email to