Swap by name

Root Boy Jim rbj at icst-cmr.ARPA
Fri Jul 4 03:25:17 AEST 1986


	In article <1836 at brl-smoke.ARPA> gwyn at brl.arpa (Doug Gwyn
	(VLD/VMB) <gwyn>) writes:
	>It may be amusing and/or instructive to contemplate the fact that
	>there is no way to write a function that exchanges the contents of
	>two variables in a language where parameters are passed "by name".
	
	How so?  It seems rather simple.  I have here a C program that effects
	call-by-name and does indeed perform a swap:

	[Example using `thunks' deleted]

Yow! Are we talking about the same thing yet? I seem to remember three types
(possibly four) of parameter passing:

	1) Call by value	C scalars
	2) Call by reference	C pointers, arrays, Fortran variables
	3) Call by name		Algol call by name (it also does val & ref)

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. Thus if we had a declaration and call:
(I forgot Algol so I'm bastardizing C)

	int a[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
	int i = 5;

	main()
	{	snafu(a[i]);
	}

	snafu(bar) call_by_name bar;
	{
		printf("%d\n",bar);		/* prints 5 */
	i = 7;	printf("%d\n",bar);		/* prints 7 */
	}

At least that's what Dr. Vic the BASILIsk told me.

Now it does seem that Chris's thunktions will provide a similar effect, so
forgive me if this whole article is unnecessary. However, while I could
write a thunktion similar to snafu to return the address of any single
array element, call by name would allow me to pass a multidimensioned
array reference to the same function as well and muck with it.

I have never understood the motivation for this technique, or if I ever
did, I probably didn't like it. Can you say interpreter?

The fourth method I have heard is `Call by value-result'. It seems to be
a variation on call by reference or value depending on how it is done.

	(Root Boy) Jim Cottrell		<rbj at icst-cmr.arpa>
	RHAPSODY in Glue!



More information about the Comp.lang.c mailing list