Swap by name

Root Boy Jim rbj at icst-cmr.ARPA
Sat Jul 5 09:37:27 AEST 1986


Previously I wrote:

	Call by reference went something like this: the calling
	routine would pass the string representation of the argument
	and the caller would parse it to figure out the argument.

Oops! I meant Call By Name. I seem to have proven one of Murphy's
Laws, addressing it to my old prof. We all know what call by reference is.

The rest of this article is a response to Chris Torek.

	From: Doug Gwyn (VLD/VMB) <seismo!BRL.ARPA!gwyn>

	No, you just implemented an elaborate form of "call by reference".

> I merely chose a poor example.  Call by reference and call by name
> often have identical effects. 

That doesn't mean they are the same. In fact, call by {value,reference}
have the same effect if one does not modify the arguments.

> See my followup for a better example.
> (Incidentally, my expression `example', contained in a comment,
> has a different problem: it returns the address of a stack variable
> that is no longer active by the time the caller uses it.  `t' should
> have been declared `static'.  This is what comes of dashing off an
> article just before bed . . . .)

You mean you wrote this at noon? :-)

> It is possible to implement full call by name in C, but it is difficult,

I was going to say no, but you need one thunk for each argument you pass.
The distinguishing characteristic about call by name is that you evaluate
your argument every time you use it. Thunks seem to qualify.

> for nonlocal reference is difficult; and no one ever
> used call by name after Algol anyway---most likely because everyone
> thinks it is terrible.  Nonlocal reference *is* possible, but
> probably best left to a compiler.  Kludges are not too hard to
> write, but are error-prone: the programmer must keep track of the
> lexical level of each routine, and know how many levels up to look,
> and at what offset, for each nonlocal variable.

I would dispute that no one uses call by name. Consider that in APL you
can pass a character string and execute it to get the result. I suppose
I am splitting hairs, but the effect is the same. I imagine one could
do the same thing in LISP, or any interpretive language.

> Call by value-result is also easy enough in C, though as with call
> by reference, it requires cooperation of both caller and callee:

I was thinking of the caller squirrelling away the value in a temporary
location, passing it by reference (thus allowing modification by the
callee) and then copying the result to the real variable.

As the fortune program says, "A fifth theory is held by idiots, but it
is doubtful whether they know anything more than the others do". Which
brings me to Univac's PLUS compiler. It stored its arguments directly
into the callee's statically declared variables and grabbed the results
after the callee returned. Ask zben at umd[25] about this if you care.

	(Root Boy) Jim Cottrell		<rbj at icst-cmr.arpa>
	I'd like MY data-base JULIENNED and stir-fried!

P.S. Zippy whipped up this saying just for Chris :-)



More information about the Comp.lang.c mailing list