WOLFRAM

Module[{x,y,},expr]

specifies that occurrences of the symbols x, y, in expr should be treated as local.

Module[{x=x0,},expr]

defines initial values for x, .

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 righthand 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 allclose all

Basic Examples  (1)Summary of the most common use cases

Out[2]=2

Applications  (2)Sample problems that can be solved with this function

Dynamic programming with a local function:

Out[2]=2

Euclid's algorithm for the GCD using initialized local variables:

Out[2]=2

Properties & Relations  (8)Properties of the function, and connections to other functions

Every time a module is evaluated, a new temporary symbol is created:

Out[1]=1

Module symbols are temporary and are removed if they are no longer referenced:

Out[1]=1
Out[2]=2

Each use of Module increments $ModuleNumber:

Out[1]=1

If there is no need to assign to a local variable, a constant should be used instead:

Out[1]=1

With is faster than Module:

Out[2]=2
Out[3]=3

Block localizes values only; it does not create new symbols:

Out[1]=1

Unique creates new variables in a way similar to Module:

Out[1]=1

Local variables are not affected by global ones, and vice versa:

Out[1]=1
Out[2]=2

A symbol brought into the scope of Module is not affected by naming conflicts:

Out[2]=2

Possible Issues  (3)Common pitfalls and unexpected behavior

Module is a scoping construct; inner local variables shield outer ones:

Variables are renamed in nested scopes:

Out[1]=1
Out[2]=2

Build the function from its parts to avoid the renaming:

Out[3]=3
Out[4]=4

Parallel assignment is not available for Module variables:

Out[2]=2
Out[3]=3
Wolfram Research (1991), Module, Wolfram Language function, https://reference.wolfram.com/language/ref/Module.html.
Wolfram Research (1991), Module, Wolfram Language function, https://reference.wolfram.com/language/ref/Module.html.

Text

Wolfram Research (1991), Module, Wolfram Language function, https://reference.wolfram.com/language/ref/Module.html.

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.

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

Wolfram Language. (1991). Module. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/Module.html

BibTeX

@misc{reference.wolfram_2025_module, author="Wolfram Research", title="{Module}", year="1991", howpublished="\url{https://reference.wolfram.com/language/ref/Module.html}", note=[Accessed: 29-March-2025 ]}

@misc{reference.wolfram_2025_module, author="Wolfram Research", title="{Module}", year="1991", howpublished="\url{https://reference.wolfram.com/language/ref/Module.html}", note=[Accessed: 29-March-2025 ]}

BibLaTeX

@online{reference.wolfram_2025_module, organization={Wolfram Research}, title={Module}, year={1991}, url={https://reference.wolfram.com/language/ref/Module.html}, note=[Accessed: 29-March-2025 ]}

@online{reference.wolfram_2025_module, organization={Wolfram Research}, title={Module}, year={1991}, url={https://reference.wolfram.com/language/ref/Module.html}, note=[Accessed: 29-March-2025 ]}