This is documentation for Mathematica 8, which was
based on an earlier version of the Wolfram Language.
View current documentation (Version 11.2)

Module

Module
specifies that occurrences of the symbols x, y, ... in expr should be treated as local.
Module
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 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 can be returned from modules.
  • You can use Module as the right-hand side of a transformation rule with a condition attached.
  • Module constructs can be nested in any way, with inner variables being renamed if necessary.
  • Module is a scoping construct that implements lexical scoping.
Dynamic programming with a local function:
Euclid's algorithm for the GCD using initialized local variables:
Every time a module is evaluated, a new temporary symbol is created:
Module symbols are temporary and are removed if they are no longer referenced:
Each use of Module increments $ModuleNumber:
If there is no need to assign to a local variable, a constant should be used instead:
With is faster than Module:
Block localizes values only; it does not create new symbols:
Unique creates new variables in a way similar to Module:
Local variables are not affected by global ones, and vice versa:
A symbol brought into the scope of Module is not affected by naming conflicts:
Module is a scoping construct; inner local variables shield outer ones:
Variables are renamed in nested scopes:
Build the function from its parts to avoid the renaming:
Parallel assignment is not available for Module variables:
New in 2