Advice: Don't use formal parameters as local variables

It is possible to assign a value to a formal parameter of a procedure (assuming the actual parameter is something that can be assigned a value). This is useful because it allows such parameters to be used for output as well as input. However, because of Maple's evaluation rules, you should not attempt to access that assigned value within the procedure. Thus the formal parameter should not be used as a local variable.

Examples:

This one is OK: the formal parameter c is used for output. The evaln means that the actual parameter will be evaluated only to a name, so any value that the actual parameter u already has will be overwritten.

> f:= proc(a,b,c::evaln) c:= a+b end:
u:= v: f(1,2,u):
u;

3

The next one does not work as expected: the formal parameter is first assigned a value, but then an attempt is made to access that value to form the result. It doesn't work.

> f:= proc(a,b::evaln) b:= a+1; b-1 end:
f(c,d);

d-1

The reason is Maple's evaluation rules for formal parameters. The actual parameter is evaluated to the appropriate level and then substituted for the formal parameter in the body of the procedure. Thus in this case the body of the procedure becomes

d:= c+1; d-1

But these parameters are not evaluated again during the execution of the procedure. Thus, even though after the first statement of the procedure d has the value c+1 , the d in the second statement is not evaluated to c+1 but is left as d .

There are various situations in which this behaviour may or may not be harmless, but the simplest rule to avoid any trouble is: after assigning a value to a formal parameter, don't try to access the value of that parameter. Instead, use a local variable to store any value that you will need to access. Thus you could say

> f:= proc(a,b::evaln) local v; v:=a+1; b:= v; v-1 end:
f(c,d);

c

See also: evaln , parameter , procedure

Maple Advisor Database R. Israel, 2000