Next: Recurrence Equations and Up: Maple Procedures Previous: Scope Rules: Parameters

Evaluation Rules: Actual and Formal Parameters, Locals, Globals

Consider the function call f(x1,x2,...,xn). The execution of this function call proceeds as follows. The function name f is evaluated. Next the arguments x1, x2, ..., xn are evaluated from left to right. Then if f evaluated to a procedure, the procedure is executed on the evaluated arguments. There are only 6 exceptions to this, including eval, assigned, and seq. Now, what about the evaluation of variables inside procedures? Consider the procedure


f := proc() local p; p := x^2+4*x+4; x := 5; p end

Here is a local variable and there are no parameters. But what is the variable ? Because it is neither a parameter nor a local variable, Maple defines it to be a global variable. Or if you prefer, a user variable. What are the evaluation rules for parameters, locals, and global variables? In the introduction we considered the problem


> p := x^2+4*x+4;
                                     2
                               p := x  + 4 x + 4

> x := 3;
                                     x := 3

> p;
                                       25

Here and are global variables. Global variables are evaluated fully, i.e. recursively, hence the result is . What if is a local variable as in our procedure f above? I.e. what is the result of executing


f();

And what if is a parameter as in the following procedure


> x := 'x'; # Make x a symbol first

                                     x := x

> g := proc(p) x := 5; p end:
> g(x^2+4*x+4);

                                   2
                                  x  + 4 x + 4

For reasons of efficiency and desirability, the Maple designers have decided that local variables and parameters evaluate one level, i.e. the value of in the above two examples is the polynomial not the value . Full evaluation only occurs for global variables. The eval function can be used to get full evaluation for local variables and parameters, and one level evaluation of global variables should you ever need it. For example


> x := 'x'; # Make x a symbol first

                                     x := x

> g := proc(p) x := 5; eval(p) end:
> g(x^2+4*x+4);

                                       49


bondaren@thsun1.jinr.ru