[GAP Forum] Argument values in function calls

Justin C. Walker justin at mac.com
Tue Aug 14 00:20:45 BST 2007


Dear Laurent and Forum,

On Aug 13, 2007, at 6:27 AM, Laurent Bartholdi wrote:

> Dear Anvita,
> A new variable is allocated in fuction calls, and therefore in Example
> 2 the variable l is not affected. However, lists are passed by
> reference, and not copied; therefore, the contents of l are changed in
> Example 1.
>
> Consider Example 1.5:
>
> gap> f1:=function(l)
>> l := l+[1];
>> return l;
>> end;
> function( l ) ... end
>
> Then this example behaves just like Example 2.
> (I don't think that any programming language would consider copying
> lists before passing them to a function; this is usually too expensive
> in computational time).

This may be off-topic, but I want to comment on the statement above.

The treatment of "large" or "structured" parameter values is not  
straight-forward, and is generally part of the language  
specification.  The approaches to this go under the names "call by  
value", "call by name", "call by reference", with a panoply of  
modifications to deal with specific issues in language design.

Some languages define the treatment once for all, with no programmer  
control (python, for example).  Others (Magma, familiar to many here)  
let the programmer decide.

The language C in fact will copy structures, but not arrays, when  
used as arguments to functions.  Defining the function foo as, say,

int foo(struct xx *x)

requires the language to pass a reference.  Defining it as

int foo(struct xx x)

more or less requires that any modification to 'x' inside foo() can't  
be reflected in the copy at the call site.

Arrays in C are special, in that they are, implicitly, references: an  
array of ints is "int *", so there's no (obvious) way to pass an  
array by value.

Regards,

Justin

--
Justin C. Walker, Curmudgeon-At-Large, Director
Institute for the Enhancement of the Director's Income
--------
The path of least resistance:
it's not just for electricity any more.
--------





More information about the Forum mailing list