Continuing my attempts to recycle articles written elsewhere… This stuff was originally from comp.lang.python. Its objective is to define the terms ‘pass-by-value’ and ‘pass-by-reference’ in ways which are:
- reasonably clear,
- language-independent, and
- consistent with widely-held expectations.
With a bit of luck and a following wind, the framework here can be extended to cover other weirder argument passing mechanisms, e.g., Algol’s pass-by-name.
If anything here is particularly controversial then I’ve failed.
Executive summary
Here’s the soundbite version.
Pass by value: The callee’s parameters are new variables, initialized as if by assignment from the values of the caller’s argument expressions.
So, we ought to be able to replace
function mumble(a, b, c) { stuff in terms of a, b, and c } ... mumble(1 + 2, xyz, whatever)
with
... fresh_a = 1 + 2 fresh_b = xyz fresh_c = whatever stuff in terms of fresh_a, fresh_b and fresh_c
with no observable difference.
Pass by reference: The callee’s parameters are merely new names for the caller’s argument variables — to the extent that this makes sense.
There’s a caveat there for argument expressions which don’t correspond directly to variables. Anyway, the idea is that you should be able to replace
function mumble(a, b) { stuff in terms of a and b) ... mumble(xyz, whatever)
by
... stuff in terms of xyz and whatever
without observable difference.