This is documentation for Mathematica 8, which was
based on an earlier version of the Wolfram Language.
View current documentation (Version 11.2)

Blocks and Local Values

Modules in Mathematica allow you to treat the names of variables as local. Sometimes, however, you want the names to be global, but values to be local. You can do this in Mathematica using Block.
Block[{x,y,...},body]evaluate body using local values for
Block[{x=x0,y=y0,...},body]assign initial values to

Setting up local values.

Here is an expression involving .
Click for copyable input
This evaluates the previous expression, using a local value for .
Click for copyable input
There is no global value for .
Click for copyable input
As described in "Modules and Local Variables", the variable x in a module such as Module is always set up to refer to a unique symbol, different each time the module is used, and distinct from the global symbol x. The x in a block such as Block is, however, taken to be the global symbol x. What the block does is to make the value of x local. The value x had when you entered the block is always restored when you exit the block. And during the execution of the block, x can take on any value.
This sets the symbol to have value .
Click for copyable input
Variables in modules have unique local names.
In blocks, variables retain their global names, but can have local values.
is given a local value inside the block.
Click for copyable input
When the execution of the block is over, the previous value of is restored.
Click for copyable input
Blocks in Mathematica effectively allow you to set up "environments" in which you can temporarily change the values of variables. Expressions you evaluate at any point during the execution of a block will use the values currently defined for variables in the block. This is true whether the expressions appear directly as part of the body of the block, or are produced at any point in its evaluation.
This defines a delayed value for the symbol .
Click for copyable input
If you evaluate outside a block, the global value for is used.
Click for copyable input
You can specify a temporary value for to use inside the block.
Click for copyable input
An important implicit use of Block in Mathematica is for iteration constructs such as Do, Sum, and Table. Mathematica effectively uses Block to set up local values for the iteration variables in all of these constructs.
Sum automatically makes the value of the iterator local.
Click for copyable input
The local values in iteration constructs are slightly more general than in Block. They handle variables such as , as well as pure symbols.
Click for copyable input
When you set up functions in Mathematica, it is sometimes convenient to have "global variables" that can affect the functions without being given explicitly as arguments. Thus, for example, Mathematica itself has a global variable $RecursionLimit that affects the evaluation of all functions, but is never explicitly given as an argument.
Mathematica will usually keep any value you define for a global variable until you explicitly change it. Often, however, you want to set up values that last only for the duration of a particular computation, or part of a computation. You can do this by making the values local to a Mathematica block.
This defines a function that depends on the "global variable" .
Click for copyable input
In this case, the global value of is used.
Click for copyable input
Inside a block, you can set up a local value for .
Click for copyable input
You can use global variables not only to set parameters in functions, but also to accumulate results from functions. By setting up such variables to be local to a block, you can arrange to accumulate results only from functions called during the execution of the block.
This function increments the global variable , and returns its current value.
Click for copyable input
If you do not use a block, evaluating changes the global value of .
Click for copyable input
With a block, only the local value of is affected.
Click for copyable input
The global value of remains unchanged.
Click for copyable input
When you enter a block such as Block, any value for x is removed. This means that you can in principle treat x as a "symbolic variable" inside the block. However, if you explicitly return x from the block, it will be replaced by its value outside the block as soon as it is evaluated.
The value of is removed when you enter the block.
If you return an expression involving , however, it is evaluated using the global value for .
Click for copyable input