Call By Value
let p = proc (x) set x = 4
in let a = 3
in begin (p a); a end
- Under call-by-value, the denoted value associated with
x is a reference that initially contains the same value as the reference associated with a, but these references are distinct. Thus the assignment to x has no effect on the contents of a’s reference, so the value of the entire expression is 3.
- With call-by-value, when a procedure assigns a new value to one of its parameters, this cannot possibly be seen by its caller. Of course, if the parameter passed to the caller contains a mutable pair, then the effect of
setleft or setright will be visible to a caller. But the effect of a
set is not.
Call By Reference
- Though this isolation between the caller and callee is generally desirable, there are times when it is valuable to allow a procedure to be passed locations with the expectation that they will be assigned by the procedure.
- This may be accomplished by passing the procedure a reference to the location of the caller’s variable, rather than the contents of the variable. This parameter-passing mechanism is called call-by-reference.
- If an operand is simply a variable reference, a reference to the variable’s location is passed. The formal parameter of the procedure is then bound to this location.
- If the operand is some other kind of expression, then the formal parameter is bound to a new location containing the value of the operand, just as in call-by-value.
- Using call-by-reference in the above example, the assignment of
4 to x has the effect of assigning 4 to a, so the entire expression would return 4, not 3.
- When a call-by-reference procedure is called and the actual parameter is a variable, what is passed is the location of that variable, rather than the contents of that location, as in call-by-value.