Mathematica includes three functions that I know of which can be used to effectively (if not actually) define a variable and give it a value within a local scope: Module, Block, and With. What are the differences between these functions (and any other scoping constructs that may exist which I don't know about), and how do I decide which one is appropriate to use in any given instance?

The documentation does describe what the different constructs do to some extent, but I feel like it's insufficiently clear on how exactly they differ from each other. Having some short examples that demonstrate the differences would be great.

7 Answers
7

You will find a lot of information in this answer. I will add a few personal notes.

Module

Use Module when you want to localize variables inside your function`s body, and those variables will potentially acquire and/or change their values during the computation.

Basic use

For example:

f[x_]:=Module[{y=x^2},y=y+x;{x,y}]

Here, a local mutable variable (symbol) y is local to the Module, and is, indeed, a symbol with a unique name. This is the closest you have in Mathematica to say local variables in C.

Advanced uses

Module also has advanced uses. One of them is to create closures - functions with a persistent state. My third post in this thread illustrates many cases of that and has further references. One example I will steal from there: the following function will produce the next Fibonacci number on demand, and yet it will be as fast as the iterative loop implementation for generation of consecutive Fibonacci numbers (since Module is invoked only once, when the function is defined):

One problem with persistence created with Module-variables is that one should not generally serialize such state (definitions), for example by saving the state via Save or DumpSave. This is because, the uniqueness of names for Module-generated symbols is guaranteed only within a single Mathematica session.

Module also allows one to create local functions, which With does not (except pure functions). This is a very powerful capability. It is particularly useful for writing recursive functions, but not only. In the link mentioned above, there were examples of this. One problem with local functions created by Module is that these symbols won't be automatically garbage-collected when Module finishes (if they have DownValues, SubValues or UpValues. OwnValues are fine), and so may lead to memory leaks. To avoid that, one can Clear these symbols inside Module before returning the result.

With

Use With to define local constants, which can not be changed inside the body of your function.

Basic use

For example,

f[x_,y_]:=With[{sum = x+y},{sum *x, sum *y}]

It is instructive to trace the execution of f. You will notice that sum gets replaced by its value very early on, before the body starts evaluating. This is quite unlike Module, where variable entries get replaced by their values in the process of evaluation, just as it woud normally happen were the variables global.

Advanced uses

On an advanced level, With can be used to inject some evaluated code deep into some expression which is otherwise unevauated:

With[{x=5},Hold[Hold[x^2]]]
(*
Hold[Hold[5^2]]
*)

and is thus an important meta-programming tool. There are lots of uses for this feature, in particular one can use this to inject code into Compile at run-time right before compilation. This can extend the capabilities / flexibility of Compile quite a bit. One example can be found in my answer to this qustion.

The semantics of With is similar to that of rule substitutions, but an important difference is that With cares about inner scoping constructs (during varible name collisions), while rules don't. Both behaviors can be useful in different situations.

Module vs With

Both of these are lexical scoping constructs, which means that they bind their variables to lexical their occurrences in the code. Technically, the major difference between them is that you can not change the values of constants initialized in With, in the body of With, while you can change values of Module variables inside the body. On a deeper level, this is because With does not generate any new symbols. It does all the replacements before the body evaluates, and by that time no "constant symbols" are at all present, all of them replaced with their values. Module, OTOH, does generate temprary symbols (which are normal symbols with an attribute Temporary), which can store a mutable state.

Stylistically, it is better to use With if you know that your variables are in fact constants, i.e. they won't change during the code execution. Since With does not create extra (mutable) state, the code is cleaner. Also, you have more chances to catch an occasional erroneous attempt in the code to modify such a constant.

Performance-wise, With tends to be faster than Module, because it does not have to create new variables and then destroy them. This however usually only shows up for very light-weight functions. I would not base my preference of one over another on performance boosts.

Block

Basic use

Block localizes the value of the variable. In this example, a does not refer to iliterally inside Block, but still uses the value set by Block.

a:=i
Block[{i=2},a]
{a,i}

Block therefore affects the evaluation stack, not just the literal occurrences of a symbol inside the code of its body. Its effects are much less local than those of lexical scoping constructs, which makes it much harder to debug programs which use Block extensively. It is not much different from using global variables, except that Blockguarantees that their values will be restored to their previous values once the execution exists Block (which is often a big deal). Even so, this non-transparent and non-local manipulation of the variable values is one reason to avoid using Block where With and / or Module can be used. But there are more (see below).

In practice, my advice would be to avoid using Block unless you know quite well why you need it. It is more error-prone to use it for variable localization than With or Module, because it does not prevent variable name collisions, and those will be quite hard to debug. One of the reasons people suggest to use Block is that they claim it is faster. While it is true, my opinion is that the speed advantage is minimal while the risk is high. I elaborated on this point here, where at the bottom there is also an idiom which allows one to have the best of both worlds. In addition to these reasons, as noted by @Albert Retey, using Block with the Dynamic - related functionality may lead to nasty surprises, and errors resulting from that may also be quite non-local and hard to find.

One valid use of Block is to temporarily redefine some global system settings / variables. One of the most common such use cases is when we want to temporarily change the value of $RecursionLimit or $IterationLimit variables. Note however that while using Block[{$IterationLimit = Infinity}, ...] is generally Ok, using Block[{$RecursionLimit = Infinity}, ...] is not, since the stack space is limited and if it gets exhausted, the kernel will crash. A detailed discussion of this topic and how to make functions tail- recursive in Mathematica, can be found e.g. in my answer to this question.

It is quite interesting that the same ability of Block can be used to significantly extend the control the user has over namespaces / symbol encapsulation. For example, if you want to load a package, but not add its context to the $ContextPath (may be, to avoid shadowing problems), all you have to do is this:

Block[{$ContextPath}, Needs[Your-package]]

As another example, some package you want to load does modify some other function (say, System`SomeFunction), and you want to prevent that without changing the code of the package. Then, you use something like

Block[{SomeFunction}, Needs[That-package]]

and this makes sure that all those modifications did not affect actual definitions for SomeFunction - see this answer for an example of this.

Advanced uses

Block is a very powerful metaprogramming device, because you can make every symbol (including system functions) temporarily "forget" what it is (its definitions and other global properties), and this may allow one to change the order of evaluation of an expression involving that symbol(s) in non-trivial ways, which may be hard to achieve by other means of evaluation control (this won't work on Locked symbols). There are many examples of this at work, one which comes to mind now is the LetL macro from my answer to this question.

Another more advanced use of Block is to ensure that all used variables would be restored to their initial values, even in the case of Abort or exception happening somewhere inside the body of Block. In other words, it can be used to ensure that the system will not find itself in an illegal state in the case of sudden failure. If you wrap your critical (global) variables in Block, it will guarantee you this.

A related use of Block is when we want to be sure that some symbols will be cleared at the end. This question and answers there represent good examples of using Block for this purpose.

Variable name conflicts

In nested scoping constructs, it may happen that they define variables with the same names. Such conflicts are typically resolved in favor of the inner scoping construct. The documentation contains more details.

Block vs Module/With

So, Block implements dynamic scoping, meaning that it binds variables in time rather than in space. One can say that a variable localized by Block will have its value during the time this Block executes (unless further redefined inside of it, of course). I tried to outline the differences between Block and With/Module (dynamic vs lexical scoping) in this answer.

Some conclusions

For most common purposes of variable localization, use Module

For local constants, use With

Do not ordinarily use Block for introducing local variables

All of the scoping constructs under discussion have advanced uses. For Module this is mostly creating and encapsulating non-trivial state (persistent or not). For With, this is mostly injecting inside unevaluated expressions. For Block, there are several advanced uses, but all of them are, well, advanced. I'd be worried if I found myself using Block a lot, but there are cases when it is indispensable.

@acl Thanks! I knew there will be many answers, but just could not resist:). I actually consider two of my strongest mma areas to be scoping and performance-tuning, so whenever there is a discussion on those, I go out of control, as you correctly noted some few days ago :)
–
Leonid ShifrinJan 23 '12 at 23:40

You mentioned that Module-locale symbols with DownValues don't get garbage collected, which sounded worrying, so I tried Module[{a}, a[1] = 1; ] in a fresh kernel. In this case a (or rather its renamed version) does get garbage collected. Can you elaborate?
–
SzabolcsJan 24 '12 at 0:00

@Szabolcs You need to refer to such function by some global object first, which you can later Remove - it is in such cases that we get into trouble. For example: ClearAll[f];Module[{a}, a[1] = 1; f[x_] := a[x]];Remove[f]. Even though we later removed f, the local a was not collected. Clearing $HistoryLenth does not seem to help. I was actually bitten by this quite seriously a few times, when I worked in an OO fashion and abandoned lots of such objects. So, I ended up with modified Module where I could clean up manually later.
–
Leonid ShifrinJan 24 '12 at 0:06

I got it now. I can also reproduce by playing with Temporary. It is an interesting (non-obvious) observation you make that Clear[a] will allow a to get garbage collected. I'll add that ClearAll will not (because it removes the Temporary attribute). Can you edit your post and change ClearAll to Clear?
–
SzabolcsJan 24 '12 at 0:13

Module: the work-horse of scoping constructs -- use it unless you have special needs

Block: use it when you need to temporarily change the definition of an existing symbol

With: use it when you need to insert values into held expressions

More details follow...

Module

Module evaluates its second argument after replacing all of the "local symbols" (x in this case) with freshly generated unique temporary symbols. Thus,

Module[{x = "local"}, {x, f[], Hold[x]}]

is roughly equivalent to:

x$123 = "local"; {x$123, f[], Hold[x$123]}

yielding the result:

{"local", "global", Hold[x$123]}

Block

Block does not generate "local" symbols. Instead, temporarily it clears all of the definitions and attributes associated with the supplied symbols, evaluates the second argument, and then restores the cleared definitions. Thus:

In practice, the clearing and restoration of the symbol x is done more efficiently by the Mathematica kernel. Take careful note that f[] evaluates to "local". The global definition of x is changed for the duration of the evaluation of the Block expression. This behaviour can be very surprising, so be sure that this is your intent before using Block.

With

With replaces all occurrences of the listed symbols in the second argument with the corresponding values, and then evaluates the result. Thus:

With[{x = "local"}, {x, f[], Hold[x]}]

is equivalent to:

{"local", f[], Hold["local"]}

with the result:

{"local", "global", Hold["local"]}

Note especially that With differs from the other two constructs in that it can change the contents of held expressions.

Undocumented Goodies

Internal`InheritedBlock acts like Block, except that it does not clear the existing definition first. This is extremely useful if you want to temporarily augment the functionality of an existing symbol instead of temporarily replacing it. I wish this were not "internal".

Due to an implementation coincidence, Block (but notWith or Module) can be used as a nifty trick to force evaluation on the right hand side of a rule, e.g.

The last bit, forcing evaluation on the rhs of a rule, is interesting. There isn't much wiggle room, if any. The condition must be inside the block expression, not outside, so the documentation of Condition is little help understanding the trick. It must be RuleDelayed and not Rule, which is a bit easier to understand; you want the trick evaluated after substitution, not before. So it looks like you're getting a RuleDelayed that's going to eval a Condition; the Block incidentally forces one more round of evaluation before the test of the Condition -- something along these lines.
–
Reb.CabinAug 8 '12 at 13:02

@Reb.Cabin Trace will reveal the "implementation coincidence" that these expressions invoke RuleCondition in a manner useful to this context. See also the brief discussion here.
–
WReachAug 8 '12 at 13:36

I'll cover a few typical uses of Block, neither of which is possible using Module or With.

Temporarily removing definitions

When you do

Block[ {a = x}, ... ]

the original definition of a is effectively replaced by whatever new definition is given in the first argument of Block, for the duration of the evaluation of Block only. If we give no definition, Block temporarily "undefines" whatever was passed to it as first argument. This works even for built-ins:

Block[ {Print}, Print["boo!"]; ]

prints nothing.

Temporarily redefining symbols

Block is also commonly used to temporarily change the value of system variables. Take for example this simple recursive implementation of a list maximum function:

Using Block made sure that this function will work even if i has a value globally.

Internal`InheritedBlock

Here it's also worth mentioning Internal`InheritedBlock. Like with Block, any changes made to the local symbols of InheritedBlock are lost when it finishes evaluating. However, unlike Block, it keeps the original definition of the localized symbols too.

This is useful for modifying existing (or built-in) functions temporarily. Here's an example to illustrate:

There will no doubt be plenty of answers for this one. However the short answer is:

Use With for local constants that you don't have to change subsequently.

Use Module for local variables that are local to that piece of code.

Use Block for local variables that are local to that sequence of evaluation.

This tutorial in the Mathematica documentation discusses the difference between Block and Module in more depth. The essential difference is that Block substitutes in the local value of the variable even if the piece of code is calling some other function that was defined outside that Block statement, and that uses the same variable name.

In my view, With is the least well understood. It is marginally faster for cases where what you have is really a constant, e.g. an intermediate calcuation in a larger function definition. For example, consider a function that returns the contribution of each element to the total, the average value of each element and the sum of squares normalized by the length of the list. The intermediate values of the length and total of the list don't subsequently change, so you can use With rather than Module.

f[x_List]:= With[{n=Length[x],t=Total[x]},
{x/t, t/n, Total[x^2]/n} ]

Other times, it is easier to use Module even if the local entity is a constant (i.e. not redefined), because it builds on other local variables. Consider:

Other uses would be if you need to precalculate a local constant (Sin[20°] etc) to save processing time when the value is needed more than once.

Block and Module seem like two things for the same purpose on first sight: scoping. However, the way they assign the variables is different: Module replaces only explicit appearances of variables, while Block also takes into account implicit ones.

The thing is again how Mathematica stores variables: a=1 does not assign the value 1 to a - it creates a pattern defining that every time a appears in the code, it is to be replaced by 1. In the sense of programming, this means that Mathematically dynamically changes its own code throughout the evaluation. The difference from this behavior to normal programming languages is why there's Block and Module. Module takes a snapshot of the code inside and starts evaluating it, replacing explicit occurences of the local variables; Block really does the replacement, and then starts going through the code. Memory hook: *B*lock comes before *M*odule in the alphabet, so it does the replacement first, and then starts evaluating.

I am not sure about "Block really does the replacement, and then starts going through the code", as for instance Block[{x = a}, Hold[x]] returns Hold[x].
–
aclJan 23 '12 at 22:36

... that is correct. Can we settle on a "for practical purposes"? :-)
–
DavidJan 23 '12 at 22:41

But I think that for practical purposes this is really the distinction between With and Block ("this"=whether it's better to think of it as literal replacement or a scoping construct). But what do I know :)
–
aclJan 23 '12 at 22:45

As you see, the global value of a is temporarily assigned to b, then inside changed to 3. That is, not only the a inside the Block, but also the value of a in the function call from the Block is modified. Outside the block, the change is undone, including any change done inside the block (the a=3).

As you can see, the a=3 now globally assigns 3 to b! That's because With actually replaces a with the "assigned" value, i.e. b in the whole body. That is, whereever the body contains a it's as if there was written b instead. But again, the value of a in the called function f is not affected.

From this, one can get the following guidelines:

In the general case, you want Module because its effect is most localized. You want Block is you explicitly want to temporarily change a global value, e.g. Block[{$RecursionLimit=10000},RecursiveFunction[10000]]. And With should be reserved for cases where you actually want a literal replacement.

Mathematica is a registered trademark of Wolfram Research, Inc. While the mark is used herein with the limited permission of Wolfram Research, Stack Exchange and this site disclaim all affiliation therewith.