Module
Details
- 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 constructs can be nested in any way, with inner variables being renamed if necessary.
- Module is a scoping construct that implements lexical scoping.
Examples
open all close allBasic Examples (1)
f[x0_] :=
Module[{x = x0},
While[x > 0, x = Log[x]];
x
]f[2.0]Applications (2)
Dynamic programming with a local function:
fib[n_] :=
Module[{f},
f[1] = f[2] = 1;
f[i_] := f[i] = f[i - 1] + f[i - 2];
f[n]
]fib[5]Euclid's algorithm for the GCD using initialized local variables:
gcd[m0_, n0_] :=
Module[{m = m0, n = n0},
While[n ≠ 0, {m, n} = {n, Mod[m, n]}];
m
]gcd[18, 21]Properties & Relations (8)
Every time a module is evaluated, a new temporary symbol is created:
{Module[{x}, x], Module[{x}, x]}Module symbols are temporary and are removed if they are no longer referenced:
Module[{x}, Print[x];Attributes[x]]Names["x$*"]Each use of Module increments $ModuleNumber:
{$ModuleNumber, Module[{x}, x], $ModuleNumber}If there is no need to assign to a local variable, a constant should be used instead:
With[{x = 2.0}, Sqrt[x] + 1]Timing[Do[Module[{x = 5}, x;], {10 ^ 5}]]Timing[Do[With[{x = 5}, x;], {10 ^ 5}]]Block localizes values only; it does not create new symbols:
x = 7;
Block[{x = 5}, Print[x]];xUnique creates new variables in a way similar to Module:
{Unique[x], Module[{x}, x]}Local variables are not affected by global ones, and vice versa:
x = 17;
Module[{x = x}, x = x + 1;x]xA symbol brought into the scope of Module is not affected by naming conflicts:
y = x ^ 2;Module[{x = 5}, x + y]Possible Issues (3)
Module is a scoping construct; inner local variables shield outer ones:
Module[{x}, Print[x];Module[{x}, Print[x]]]Variables are renamed in nested scopes:
Module[{e = Expand[(1 + x) ^ 5]}, Function[x, e]]%[10]Build the function from its parts to avoid the renaming:
Module[{e = Expand[(1 + x) ^ 5]}, Function@@{x, e}]%[10]Parallel assignment is not available for Module variables:
v = {a, b};Module[{{x, y} = v}, x ^ 2 + y ^ 2]Module[{x = v[[1]], y = v[[2]]}, x ^ 2 + y ^ 2]Related Guides
History
Introduced in 1991 (2.0)
Text
Wolfram Research (1991), Module, Wolfram Language function, https://reference.wolfram.com/language/ref/Module.html.
CMS
Wolfram Language. 1991. "Module." Wolfram Language & System Documentation Center. Wolfram Research. https://reference.wolfram.com/language/ref/Module.html.
APA
Wolfram Language. (1991). Module. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/Module.html
BibTeX
@misc{reference.wolfram_2026_module, author="Wolfram Research", title="{Module}", year="1991", howpublished="\url{https://reference.wolfram.com/language/ref/Module.html}", note=[Accessed: 15-June-2026]}
BibLaTeX
@online{reference.wolfram_2026_module, organization={Wolfram Research}, title={Module}, year={1991}, url={https://reference.wolfram.com/language/ref/Module.html}, note=[Accessed: 15-June-2026]}