In Maple, how do I define local and global variables when programming?
In Maple, you can declare variables as either local or global. Any variable in a procedure that is not explicitly declared either local or global is automatically declared local if:
- The variable appears on the left side of an assignment
(
:=) statement.
- The variable is used as an index of a
fororseqstatement.
Otherwise, in all cases, the variable is declared global. Whenever a
variable is declared local automatically by Maple, a warning message
is issued. You can control the display of these warnings with the
warnlevel option to the interface command.
See the online help for more information about warnlevel.
It is always safer to explicitly declare variables to be local or global within a procedure body, for example:
> f:= proc() > local h; > global k; > h:= rand(); > k:= h/2; > end;Formal parameters to a procedure are used as local variables within the procedure. You can do type checking within the formal parameter list, for example:
> f:= proc (n::integer, x::nonneg) > x^n; > end; f := proc(n::integer, x::nonneg) x^n end > > f(2, 2/3); 4/9 > > f(1/2, 4); Error, f expects its 1st argument, n, to be of type integer, but received 1/2 > > f(2, -3); Error, f expects its 2nd argument, x, to be of type nonneg, but received -3For more about statistical and mathematical software, email the UITS Stat/Math Center, visit the center's web page, or phone 812-855-4724 (IUB) or 317-278-4740 (IUPUI). The center is located in Bloomington at 410 N. Park Avenue, and is open for consultation by appointment Monday-Friday 9am-5pm.
Last modified on March 17, 2011.







