Embedded Variable Declarations - Maple Programming Help

Online Help

All Products    Maple    MapleSim


Home : Support : Online Help : Programming : local_scopes

Embedded Variable Declarations

Rules for declaring variables inside procedures and modules

 

Description

Error Messages

Examples

Compatibility

Description

• 

Maple 2019 removes the requirement that all procedure and module local and global variables, and module exported variables, be declared at the beginning of the procedure or module before any executable statement.

• 

Exported variable declarations in a module can appear anywhere within the top-level statement sequence of the module body, but not within any nested statement sequences such as the branches of an if statement or the body of a for loop. (It is permitted to declare them within a use statement, so long as that statement itself is at the top level.)

• 

Global variable declarations in a procedure or module can appear anywhere that a statement is allowed.

• 

Local variable declarations in a procedure or module can appear anywhere that a statement is allowed, in the specification of the control variable of a for-loop, and in an in-line assignment within an expression. The following are all valid examples of local variable declarations:

  

local a := 1, b := 2;

  

for local i from 1 to 10 do ... end do;

  

for local ind, val in MyList do ... end do;

  

if (local a := f(x)) > 0 and a < 10 then ... end if;

• 

A variable declared within the body of a procedure or module has a limited visibility or scope. The general rule for visibility is that the variable is visible, or in scope, downward and to the right of its point of declaration. It is not visible above (before) its declaration nor is it visible in any less nested statements than the one it was declared in.

  

A variable declared at the level of a statement is visible from that statement onward in the statement sequence in which it appears. This includes any nested statement sequences such as the bodies of if statements and loops.

  

A variable declared as a controlling variable of a for-loop is visible only within the loop, which includes the statements comprising the body of the loop, and any while or until clause the loop might have.

  

A variable declared within an expression that is the condition of an if statement (or one of its elif clauses) is visible from that point onward in the if statement, including all subsequent elif clauses and statement sequences comprising the branches of the if statement. Thus, a multi-way test of a non-trivially computed value can be written like this:

  

if (local a := f(x)) = 1 then ...

  

elif a = 2 then ...

  

elif a = 3 then ...

  

...

  

else ...

  

end if;

  

The same rule applies to a variable declared within the condition of a while clause, namely the variable is only visible within the body of the loop it controls (and also the terminating until clause if present).

  

A variable declared during an assignment embedded in an expression other than the condition of an if, elif, or while is visible in the remainder of the expression, as well as any statements from that point onward in the statement sequence containing the expression.

  

Note that some expressions may be reordered during Maple's expression simplification process, so the meaning of remainder of the expression doesn't always correspond with what was written. The only cases where one can safely rely on ordering are Boolean expressions (for example, the left hand side of an and expression is always evaluated before the right hand side), entries of a list, operands of a function call, and bare expression sequences.

• 

The same local or global variable may be declared more than once in a procedure or module, so long as the following conditions hold:

1. 

Another declaration of the variable is not already visible at the point of declaration,

2. 

The variable is declared to be of the same kind (local or global) in all declarations,

3. 

If a type is given for a local variable in more than one declaration, the type must be the same in all declarations, and

4. 

If the variable is local, it must be initialized at each declaration.

  

A variable is considered to be initialized if it is assigned a value in its declaration (e.g. local a := 1), or it is a controlling variable of a for loop (e.g. for local i to 10 do).

  

These rules ensure that a variable declared in two disjoint scopes acts as if it were two separate variables, even though only a single variable is created.

• 

When a procedure containing embedded declarations is processed by Maple, all the declarations are resolved into a single set of declarations at the top of the procedure. Any initializations are converted into regular assignment statements which remain at the point where the declaration was originally written.

• 

These local scoping rules do not affect lexical scoping, which is performed after local scoping is completed. For example, a procedure within a module may refer to another procedure declared later in the same module simply by using that procedure's assigned name.

Error Messages

• 

The following error messages indicate declaration errors. In some of these messages, wherever the words local or global appear, any one of the words local, global, or exported may appear in its place, depending on the offending variable(s).

• 

local variable `x` is redeclared as a global variable

  

This indicates that multiple declarations for the same variable are not all of the same kind (local, global, or export).

• 

local variable `x`::integer is redeclared with type string

  

This message will result if multiple declarations of the same variable specify different types.

• 

multiply declared local variable `x` must be initialized at each declaration

  

If a variable is declared in more than one place, the variable must be initialized everywhere that it is declared, either explicitly (e.g. local a := 1) or implicitly by virtue of being declared as a controlling variable of a for loop.

• 

local variable `x` is used out of scope

  

When a variable used somewhere that no declaration for it is visible, this message is produced.

• 

declaration of local variable `x` masks an earlier global declaration

  

This message appears when a variable is declared somewhere that another declaration for it is still visible, such as earlier in the same statement sequence or an enclosing statement sequence.

• 

export `x` declaration is not allowed in a nested statement

  

Although the exported variables of a module need no longer all be declared at the beginning of the module definition, they must still all be declared within the top-level statement sequence, not within an if, try, or loop statement.

• 

exported variable `x` cannot be multiply declared

  

Unlike local or global variables, only one declaration of each exported variable may appear in a module.

Examples

In this example, one can see that embedded local variable declarations, after being checked for validity, are all collected into one sequence of declarations.

P := proc( x )
    if (local a := x^2) > 4 then
        print("too many")
    elif a = 4 then
        for local i to a do
            print(i)
        end do
    else
        for local i to a+1 do
            print(i,a)
        end do
    end if
end proc;

P:=procxlocala&comma;i&semi;if4<a:=x&Hat;2thenprinttoo manyelifa&equals;4thenforitoadoprintiend doelseforitoa&plus;1doprinti&comma;aend doend ifend proc

(1)

Compatibility

• 

Declaring variables within the body of a procedure or module was introduced in Maple 2019.

• 

This feature is currently not supported in 2-D math input in the Standard Interface. It is supported in 1-D math input in the Standard interface, as well as in the Command-line and Classic user interfaces.

See Also

lexical scoping

module

module[export]

module[local]

procedure