({{, (RefLink[Module, paclet:ref/Module])[{x,y,…},expr]
specifies that occurrences of the symbols x, y, … in expr should be treated as local. }, {, (RefLink[Module, paclet:ref/Module])[{x=(Subscript[x, 0]),…},expr]
defines initial values for x, …. }})

OpenersNotesSection(Spacer1)Details

(RefLink[Module, paclet:ref/Module]) allows you to set up local variables with names that are local to the module.

(RefLink[Module, paclet:ref/Module]) creates new symbols to represent each of its local variables every time it is called.

(RefLink[Module, paclet:ref/Module]) creates a symbol with name xxx$nnn to represent a local variable with name xxx. The number nnn is the current value of (RefLink[$ModuleNumber, paclet:ref/$ModuleNumber]).

The value of (RefLink[$ModuleNumber, paclet:ref/$ModuleNumber]) is incremented every time any module is used.

Before evaluating expr, (RefLink[Module, paclet:ref/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 (RefLink[Module, paclet:ref/Module]) carry the attribute (RefLink[Temporary, paclet:ref/Temporary]).

Symbols created by (RefLink[Module, paclet:ref/Module]) can be returned from modules.

You can use (RefLink[Module, paclet:ref/Module])[{vars},body/;cond] as the right-hand side of a transformation rule with a condition attached.

(RefLink[Module, paclet:ref/Module]) has attribute (RefLink[HoldAll, paclet:ref/HoldAll]).

(RefLink[Module, paclet:ref/Module]) constructs can be nested in any way, with inner variables being renamed if necessary.

(RefLink[Module, paclet:ref/Module]) is a scoping construct that implements lexical scoping.

OpenersPrimaryExamplesSection(Spacer1)Examples  (14)

OpenersExampleSection0(Spacer1)Basic Examples  (1)

f[x0_]:=
Module[{x=x0},
While[x>0,x=Log[x]];
x
]

f[2.0]

OpenersExampleSection1(Spacer1)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]

OpenersExampleSection2(Spacer1)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 (RefLink[Module, paclet:ref/Module]) increments (RefLink[$ModuleNumber, paclet:ref/$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]

(RefLink[With, paclet:ref/With]) is faster than (RefLink[Module, paclet:ref/Module]):

Timing[Do[Module[{x=5},x;],{10^5}]]

Timing[Do[With[{x=5},x;],{10^5}]]

(RefLink[Block, paclet:ref/Block]) localizes values only; it does not create new symbols:

x=7;
Block[{x=5},Print[x]];x

(RefLink[Unique, paclet:ref/Unique]) creates new variables in a way similar to (RefLink[Module, paclet:ref/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]

x

A symbol brought into the scope of (RefLink[Module, paclet:ref/Module]) is not affected by naming conflicts:

y=x^2;

Module[{x=5},x+y]

OpenersExampleSection3(Spacer1)Possible Issues  (3)

(RefLink[Module, paclet:ref/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 (RefLink[Module, paclet:ref/Module]) variables:

v={a,b};

Module[{{x,y}=v},x^2+y^2]

Module[{x=v[[1]],y=v[[2]]},x^2+y^2]

({{DocumentationFooterIconsRelatedFunction.png, ({{See Also}, {(RefLink[With, paclet:ref/With])  ▪  (RefLink[Block, paclet:ref/Block])  ▪  (RefLink[DynamicModule, paclet:ref/DynamicModule])  ▪  (RefLink[Unique, paclet:ref/Unique])  ▪  (RefLink[GeneratedParameters, paclet:ref/GeneratedParameters])}})}})

({{DocumentationFooterIconsRelatedTutorial.png, ({{Tutorials}, {▪(RefLinkPlain[Functions as Procedures, paclet:tutorial/FunctionsAsProcedures])}, {▪(RefLinkPlain[Modules and Local Variables, paclet:tutorial/ModulesAndLocalVariables])}, {▪(RefLinkPlain[Variables in Pure Functions and Rules, paclet:tutorial/VariablesInPureFunctionsAndRules])}, {▪(RefLinkPlain[Blocks Compared with Modules, paclet:tutorial/BlocksComparedWithModules])}, {▪(RefLinkPlain[Introduction to Dynamic, paclet:tutorial/IntroductionToDynamic])}})}})

({{DocumentationFooterIconsRelatedGuide.png, ({{Related Guides}, {▪(RefLinkPlain[Scoping Constructs, paclet:guide/ScopingConstructs])}, {▪(RefLinkPlain[Package Development, paclet:guide/PackageDevelopment])}, {▪(RefLinkPlain[Language Overview, paclet:guide/LanguageOverview])}, {▪(RefLinkPlain[Procedural Programming, paclet:guide/ProceduralProgramming])}, {▪(RefLinkPlain[Namespace Management, paclet:guide/NamespaceManagement])}, {▪(RefLinkPlain[Package Bulletproofing, paclet:guide/PackageBulletproofing])}, {▪(RefLinkPlain[Attributes, paclet:guide/Attributes])}})}})

({{(Spacer1), ({{(Spacer1)Related Links}, {(WebLink[DocumentationFooterIconsRelatedLinks-FastIntro.png(Spacer1)Fast Introduction for Programmers: Procedures, http://www.wolfram.com/language/fast-introduction-for-programmers/procedures/])}, {(WebLink[DocumentationFooterIconsRelatedLinks-EIWL.png(Spacer1)An Elementary Introduction to the Wolfram Language, https://www.wolfram.com/language/elementary-introduction/38-assigning-names-to-things.html])(WebLink[: Assigning Names to Things, https://www.wolfram.com/language/elementary-introduction/38-assigning-names-to-things.html])}, {(WebLink[DocumentationFooterIconsRelatedLinks-EIWL.png(Spacer1)An Elementary Introduction to the Wolfram Language, https://www.wolfram.com/language/elementary-introduction/47-debugging-your-code.html])(WebLink[: Debugging Your Code, https://www.wolfram.com/language/elementary-introduction/47-debugging-your-code.html])}})}})