11.07.2015 Views

Advanced Programming Guide

Advanced Programming Guide

Advanced Programming Guide

SHOW MORE
SHOW LESS
  • No tags were found...

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

50 • Chapter 3: <strong>Programming</strong> with ModulesLocal Variables You can refer to variables that are local to the moduledefinition by using the local declaration. Its format is the same as forprocedures. Here is a variant on the previous Hello module which uses alocal variable.> Hello := module()> local loc;> export say;> loc := "HELLO WORLD!";> say := proc()> print( loc )> end proc;> end module:Local variables are not visible outside the definition of the modulein which they occur. They are private to the module, and are exactlyanalogous to local variables of procedures.A local variable in a module (or procedure) is a distinct object froma global variable with the same name. Local variables are normally shortlivedvariables; the normal lifetime of a local variable is the execution timeof the body of code (a module or procedure body) to which it is local.(Local variables may persist once execution of the scope in which they occurhas completed, but they are normally inaccessable and will eventuallybe recycled by Maple’s automatic storage management system.)Exported Local VariablesProcedures and modules both support local variables. Only modules supportexported local variables, often referred to simply as exports.Module exports are declared using the export declaration. It beginswith the keyword export, after which follows a (nonempty) sequence ofsymbols. A name is never exported implicitly; exports must be declared.The result of evaluating a module definition is a module. You canview a module as a collection of its exports, which are also referred toas members of the module. These are simply names that can (but neednot) be assigned values. You can establish initial values for the exportsby assigning to them in the body of the module definition.The word export is short for exported local variable. In most respects,a module export is a local variable (such as those declared via the localdeclaration.) The crucial difference is that you can access the exportedlocal variables of a module after it has been created.To access an export of a module, use the :- member selection operator.Its general syntax is:

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!