Scoping
By default, all symbols are “global” in Symja, i.e. they can be read and written in any part of your program. However, sometimes “local” variables are needed in order not to disturb the global namespace. Symja provides two ways to support this:
- lexical scoping by
Module
, and - dynamic scoping by
Block
.
localizes variables by giving them a temporary name of the form
name$number
, where number is the current internal Symja value of “module number”. Each time a module is evaluated, “module number” is incremented.
temporarily stores the definitions of certain variables, evaluates
expr
with reset values and restores the original definitions afterwards.
Both scoping constructs shield inner variables from affecting outer ones:
Module
creates new variables:
Block
does not:
Thus, Block
can be used to temporarily assign a value to a variable:
It is common to use scoping constructs for function definitions with local variables: