This is documentation for Mathematica 3, which was
based on an earlier version of the Wolfram Language.
View current documentation (Version 11.2)
 Documentation / Mathematica / Built-in Functions / Programming / Scoping  /

  • Module[ x , y , ... , expr ] specifies that occurrences of the symbols x, y, ... in expr should be treated as local.
  • Module[ x = , ... , expr ] defines initial values for x, ... .
  • Module allows you to set up local variables with names that are local to the module.
  • Module creates new symbols to represent each of its local variables every time it is called.
  • Module creates a symbol with name xxx $ nnn to represent a local variable with name xxx. The number nnn is the current value of $ModuleNumber.
  • The value of $ModuleNumber is incremented every time any module is used.
  • Before evaluating expr, Module substitutes new symbols for each of the local variables that appear anywhere in expr except as local variables in scoping constructs.
  • Symbols created by Module carry the attribute Temporary.
  • Symbols created by Module can be returned from modules.
  • You can use Module[ vars , body /; cond ] as the right-hand side of a transformation rule with a condition attached.
  • Module has attribute HoldAll.
  • Module is a scoping construct (see Section A.3.8).
  • Module constructs can be nested in any way.
  • Module implements lexical scoping.
  • See the Mathematica book: Section 1.7.2Section 2.6.1.
  • See also: With, Block, Unique.

    Further Examples

    This defines the global variable t to have the value 17.



    The t inside the Module is local, so it can be treated independently of the global t.



    The global t still has value 17.



    The most common way that modules are used is to set up temporary or intermediate variables inside functions you define. It is important to make sure that such variables are kept local. If they are not, then you will run into trouble whenever their names happen to coincide with the names of other variables.
    The intermediate variable t is specified to be local to the module.


    This runs the function t.



    The global t still has value 17.



    You can treat local variables in modules just like other symbols. Thus, for example, you can us them as names for local functions, you can assign attributes to them and so on.
    This sets up a module which defines a local function f.


    In this case, the local function f is just an ordinary factorial.



    In this case, f is set up as a generalized factorial.



    When you set up a local variable in a module, Mathematica initially assigns no value to the variable. This means that you can use the variable in a purely symbolic way, even if there was a global value defined for the variable outside the module.
    This uses the global value of t defined above, and so yields a number.



    Here, Length simply receives a number as its argument.



    The local variable t has no value, so it acts as a symbol, and Expand produces the anticipated algebraic result.