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

Documentation / Mathematica / The Mathematica Book / Principles of Mathematica / Modularity and the Naming of Things /

2.7.6 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.

Setting up local values.

Here is an expression involving x.

In[1]:= x^2 + 3

Out[1]=

This evaluates the previous expression, using a local value for x.

In[2]:= Block[{x = a + 1}, %]

Out[2]=

There is no global value for x.

In[3]:= x

Out[3]=

As described in the sections above, the variable x in a module such as Module[x, body] 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[x, body] 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 t to have value 17.

In[4]:= t = 17

Out[4]=

Variables in modules have unique local names.

In[5]:= Module[{t}, Print[t]]

In blocks, variables retain their global names, but can have local values.

In[6]:= Block[{t}, Print[t]]

t is given a local value inside the block.

In[7]:= Block[{t}, t = 6; t^4 + 1]

Out[7]=

When the execution of the block is over, the previous value of t is restored.

In[8]:= t

Out[8]=

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 u.

In[9]:= u := x^2 + t^2

If you evaluate u outside a block, the global value for t is used.

In[10]:= u

Out[10]=

You can specify a temporary value for t to use inside the block.

In[11]:= Block[{t = 5}, u + 7]

Out[11]=

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 t local.

In[12]:= Sum[t^2, {t, 10}]

Out[12]=

The local values in iteration constructs are slightly more general than in Block. They handle variables such as a[1], as well as pure symbols.

In[13]:= Sum[a[1]^2, {a[1], 10}]

Out[13]=

When you set up functions in Mathematica, it is sometimes convenient to have "global variables" which can affect the functions without being given explicitly as arguments. Thus, for example, Mathematica itself has a global variable $RecursionLimit which 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 which 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 which depends on the "global variable" t.

In[14]:= f[x_] := x^2 + t

In this case, the global value of t is used.

In[15]:= f[a]

Out[15]=

Inside a block, you can set up a local value for t.

In[16]:= Block[{t = 2}, f[b]]

Out[16]=

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 t, and returns its current value.

In[17]:= h[x_] := (t += x^2)

If you do not use a block, evaluating h[a] changes the global value of t.

In[18]:= h[a]

Out[18]=

With a block, only the local value of t is affected.

In[19]:= Block[{t = 0}, h[c]]

Out[19]=

The global value of t remains unchanged.

In[20]:= t

Out[20]=

When you enter a block such as Block[x, body], 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 t is removed when you enter the block.

In[21]:= Block[{t}, Print[Expand[(t + 1)^2]]]

If you return an expression involving t, however, it is evaluated using the global value for t.

In[22]:= Block[{t}, t^2 - 3]

Out[22]=