Module
✖
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 allclose allBasic Examples (1)Summary of the most common use cases

https://wolfram.com/xid/0mq6vq-i2fv4k

https://wolfram.com/xid/0mq6vq-khx9d9

Applications (2)Sample problems that can be solved with this function
Dynamic programming with a local function:

https://wolfram.com/xid/0mq6vq-carga

https://wolfram.com/xid/0mq6vq-ilc4w

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

https://wolfram.com/xid/0mq6vq-kgez2t

https://wolfram.com/xid/0mq6vq-hefccb

Properties & Relations (8)Properties of the function, and connections to other functions
Every time a module is evaluated, a new temporary symbol is created:

https://wolfram.com/xid/0mq6vq-j20eck

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

https://wolfram.com/xid/0mq6vq-g19k9b


https://wolfram.com/xid/0mq6vq-ux3pk

Each use of Module increments $ModuleNumber:

https://wolfram.com/xid/0mq6vq-jjks0b

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

https://wolfram.com/xid/0mq6vq-crvx03


https://wolfram.com/xid/0mq6vq-rf4b1


https://wolfram.com/xid/0mq6vq-elsu9r

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

https://wolfram.com/xid/0mq6vq-kcvf9c

Unique creates new variables in a way similar to Module:

https://wolfram.com/xid/0mq6vq-c4vp4u

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

https://wolfram.com/xid/0mq6vq-gug7k4


https://wolfram.com/xid/0mq6vq-pzc8d6

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

https://wolfram.com/xid/0mq6vq-3i761

https://wolfram.com/xid/0mq6vq-hwp9ge

Possible Issues (3)Common pitfalls and unexpected behavior
Module is a scoping construct; inner local variables shield outer ones:

https://wolfram.com/xid/0mq6vq-cxpmmo
Variables are renamed in nested scopes:

https://wolfram.com/xid/0mq6vq-ur44b


https://wolfram.com/xid/0mq6vq-e9fdbp

Build the function from its parts to avoid the renaming:

https://wolfram.com/xid/0mq6vq-c56zuo


https://wolfram.com/xid/0mq6vq-cnb70

Parallel assignment is not available for Module variables:

https://wolfram.com/xid/0mq6vq-c2pr7

https://wolfram.com/xid/0mq6vq-dlipn6



https://wolfram.com/xid/0mq6vq-ek10ws

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
]}
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
]}