Advice: One level evaluation for local variables

Inside a procedure, Maple uses one-level evaluation on local variables, instead of the full evaluation which is used on global variables. In most cases this is all that is needed, and in some cases it results in substantially improved efficiency.

> qproc:= proc(x) local a,b;
b:= a+1;
a:= x+1;
b;
end:
qproc(y);

[Maple Math]

Here is what happens when qproc(y) is executed:

First the argument y is evaluated, and the formal parameter x is replaced by the result (which is [Maple Math] ) throughout the body of the procedure.

Now b is assigned the value [Maple Math] (note that a has no value other than itself yet).

Then [Maple Math] , which is the replacement of x+1 , is assigned to a .

Finally, the local variable b is evaluated one level (obtaining [Maple Math] ), but the local variable a is not further evaluated there. The value this local variable had is not lost (even though the procedure that owned it has terminated): we can have it evaluated by using ".

> ";

[Maple Math]

Under most circumstances we want the results our procedures return to be fully evaluated, and not to contain local variables. Careful consideration of the order in which assignments are made can usually remedy the situation. If necessary, you can fully evaluate an expression using eval . Thus the procedure above could be rewritten:

> qproc2:= proc(x) local a,b;
b:= a+1;
a:= x+1;
eval(b);
end:
qproc2(y);

[Maple Math]

See also: Assignments do not commute , eval , Last name evaluation , Local procedures don't access local variables

Maple Advisor Database, R. Israel 1997