- Local variables in a procedure are those
- declared in the parameter list,
- declared in a scalar or integer statement in a begin-end block,
- bound in the right-hand side of a where statement
- implicitly introduced in a for statement .
These are valid only inside their local context. scalar variables are
initialized to nil, integer to 0 unless they have a special initialization
value (property initvalue* ).
symbolic procedure my_adder(x,y);
begin integer r;
r:=x+y;
return r:
end;
In this routine x,y and r are local variables.
In algebraic mode the where statment is used to activate one more
more rules locally. In symbolic mode mode only rules of the form
<name>=<value> are allowed the the right-hand side of a where
statement.
- Global variables have to be declared in a statement global′(v1 v2 );
These can be accessed from everywhere once they have been declared.
Important: declare them before you use them for the first time in a session.
They are initially set to nil.
global ’(my_support!* my_quotient!*);
It is a common practice to use a trailing asterisk in names for
global and fluid variables such that they easily can be distinguished
from locals. Names of global variables may not be used as local
variables.
- Fluid variables are similar to global variables as they can be accessed from
everywhere. But in contrast to globals a fluid variable can occur as a local
variable in a procedure. It then has temporarily the value assigned in the
procedure (we say “it is rebound”); this value will be accessible globally by
nested procedures as long as the rebinding procedure is not yet
terminated. After termination the previous value is reassigned. Fluid
variables are declared in a statement fluid′(v1 v2 );. Like global
variables they must be declared before use and they are initially set to
nil.
fluid ’(my_var!*);
my_var!*:=’x;
procedure compute(ex,my_var!*);
do_some_work(ex);
In this case the variable my_var* has been initialized to the symbol x. If
then a call compute(′a,′z) is performed, the variable my_var* will be set to
z during the execution of the body of compute.
- A switch can be declared using the switch statement switchs1,s2;
where s1,s2, are symbols. REDUCE automatically connects a fluid
variable with each switch by prefixing an asterisk to the symbol name. This
variable represents the value of the switch, which is either nil or t and is
set by the statements on and off. E.g. the switch nat has the associated
global variable *nat.
- share variables are also global variables which are handled specially by the
REDUCE evaluator such that their symbolic mode values are identical with
their role as symbols in algebraic mode.