Block

Block[{x, y}, ....] is normally used to make the kernel to temporarily forget about any values that symbols (x,y) may have. Contrary to what Module does the variables (x,y) inside Block are the same variables (x,y) outside the Block. The only thing Block does is temporarily change or clear the values of these variables. Some applications are given in the grouped cells below.

The next cell clears any previous definitions that might interfere with the examples below.

Temporarily clear the meaning of built in functions

If you need to write a program that you know may post messages you can suppress the display of messages during intermediate steps of your program. As a specific example the expression in the next cell causes the kernel to evaluate Message[Prime::intpp, 2.25] inside the Block. The head Message has no meaning while inside the Block, so the message is never printed. Try evaluating Prime[2.25] without using Block and a message will be printed (unless you turned off the message earlier).

For another example of making the kernel forget what a built-in function does consider the expression below which is wrapped in HoldForm.

First we use ReleaseHold to see what the expression evaluates to without HoldForm.

Suppose you want to evaluate this expression, but you don't want to evaluate or . This can be done by using Block to make the kernel temporarily forget what Power does! Recall has the FullForm Power[n, 1/2]. Once inside Block we change the head of (expr) from HoldForm to List, and let it evaluate. After evaluation is finished we change the head back to HoldForm so that and don't evaluate after we get out of the Block. The curious thing about this trick is that we didn't have to Unprotect Power. Another method of allowing only certain parts of an expression to evaluate is given in the discussion of Fold.

Temporarily change the value of $DisplayFunction

The following line indicates the Default value Plot uses for the DisplayFunction option.

Besides making the kernel forget that a symbol has values you can use Block to temporarily use a new value for symbols. We can use Block to temporarily change the value of $DisplayFunction as in the cell below. This creates the satisfying result of presenting only a single graphic, without typing (DisplayFunction->Identity) four times!

Temporarily decrease $RecursionLimit and/or $IterationLimit

When you are writing preliminary drafts of a program you might want to use a lower value of $RecursionLimit and/or Iteration limit. Using the code below you can make this change only take effect inside the Block.

Temporarily clear the numeric value of a symbol

In the first cell below (demo) is an expression in (x). In the second cell (x) has a numeric value is assigned and demo will turn out to be a constant if evaluated. Since demo is a constant (provided x has a numeric value) the derivative of demo is zero. Worse than that we see below that D[demo,x] doesn't even evaluate as we would like.

In the next cell we can evaluate the derivative of (demo) as if (x) had no numeric value. This was possible because Block[{x}, ....] ensured that (x) had no values. Apply[HoldForm,....] had to be used to prevent evaluating the result with numeric values for (x).

In the next cell ReleaseHold is used to allow complete evaluation of the above derivative.

Finally in the next cell Block[{x=a}, ....] causes (x) to temporarily evaluate to (a), and then evaluate the derivative of demo.

Modify a built-in function

Block can be used to modify the way built-in kernel functions evaluate in ways that are not otherwise possible. For example suppose you wanted to change 'D' so that D[expr,x] would Expand expr before using the built-in definition of 'D'.

The definition for 'D' in the next cell is only used when the symbol $DoThis is True. When this definition is used Block temporarily clears all values from the symbol $DoThis, and then evaluates D[Expand[expr],x]. If it were not for the use of Block{$DoThis},...] we would have infinite recursion since the new definition would call itself. However, once inside the Block $DoThis is not True, so the built-in definition of 'D' is used instead.

Created by Mathematica (May 16, 2004)