Indiana University
University Information Technology Services
  
What are archived documents?
Login>>
Login

Login is for authorized groups (e.g., UITS, OVPIT, and TCC) that need access to specialized Knowledge Base documents. Otherwise, simply use the Knowledge Base without logging in.

Close

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 for or seq statement.

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 -3

For 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.

This is document affj in domain all.
Last modified on March 17, 2011.

Comments/Questions/Corrections

Use this form to offer suggestions, corrections, and additions to the Knowledge Base. We welcome your input!

If you are affiliated with Indiana University and would like assistance with a specific computing problem, please use the Ask a Consultant form, or contact your campus Support Center.

Contact Information

Note: We will reply to your comment at this address. If your message concerns a problem receiving email, please enter an alternate email address.