Lexical Scoping - Maple Help

Online Help

All Products    Maple    MapleSim


Home : Support : Online Help : Programming : Modules : Lexical Scoping

Lexical Scoping

Maple allows you to create nested procedure definitions. This means that procedures can be defined within other procedures or can return procedures as expected output. Lexical scoping allows a nested procedure to access the variables that are located in surrounding procedures. You can now program in Maple and achieve better encapsulation.

 

Because Maple is symbolic and can manipulate objects such as procedures and unevaluated local variables from procedures, lexical scoping also provides a mechanism to do object-oriented programming.

Illustration of Rules

restart;

The following example is used to illustrate these rules.

proc( p1 )
    local l1;
    global g1;
    il2 := l1 + g1;
    proc( p2 )
        local l3;
        global g2;
        l1 := l3 + g2;
        il2 := g2 + l3;
        l4 := l1 + il2 + g4;
        proc( p3 )
            l1 + il2 + l3 + g1 + g2 + g3 + g4;
        end proc;
    end proc;
end proc:

Warning, `il2` is implicitly declared local to procedure

Warning, `l4` is implicitly declared local to procedure


The outermost procedure has one parameter (p1), one explicit local (l1), one implicit local (il2), and one global (g1).


The second procedure has one parameter (p2), one explicit local (l3), one implicit local (il2), and one global (g2). Notice that the il2 in the second procedure is the same as the il2 in the outermost procedure, and that the l1 in the second procedure is the same as the l1 in the outer procedure.


In the innermost procedure, l1 and il2 are the local ones from the outermost procedure and l3 is the local one from the second procedure. The four variables g1, g2, g3, and g4 are all global. Variable g1 is global because it is declared as such in the outermost procedure. Variable g2 is global because it is declared as such in the second procedure. Variables g3 and g4 are global by the default rules.

Return to Index for Example Worksheets