Scoping

By default, all symbols are “global” in Mathics, 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. Mathics provides two ways to support this:

Module[{vars}, expr]
localizes variables by giving them a temporary name of the form name$number, where number is the current value of $ModuleNumber. Each time a module is evaluated, $ModuleNumber is incremented.
Block[{vars}, expr]
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:

Block can also be used to temporarily change the value of system parameters:

It is common to use scoping constructs for function definitions with local variables: