This is documentation for Mathematica 3, 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.6.2 Local Constants

Defining local constants.

Module allows you to set up local variables, to which you can assign any sequence of values. Often, however, all you really need are local constants, to which you assign a value only once. The MathematicaWith construct allows you to set up such local constants.

  • This defines a global value for t.
  • In[1]:= t = 17


  • This defines a function using t as a local constant.
  • In[2]:= w[x_] := With[{t = x + 1}, t + t^3]

  • This uses the definition of w.
  • In[3]:= w[a]


  • t still has its global value.
  • In[4]:= t


    Just as in Module, the initial values you define in With are evaluated before the With is executed.

  • The expression t+1 which gives the value of the local constant t is evaluated using the global t.
  • In[5]:= With[{t = t + 1}, t^2]


    The way With[


    , ...

    ,body] works is to take body, and replace every occurrence of x, etc. in it by , etc. You can think of With as a generalization of the /. operator, suitable for application to Mathematica

    code instead of other expressions.

  • This replaces x with a.
  • In[6]:= With[{x = a}, x = 5]


  • After the replacement, the body of the With is a=5, so a gets the global value 5.
  • In[7]:= a


  • This clears the value of a.
  • In[8]:= Clear[a]

    In some respects, With is like a special case of Module, in which each local variable is assigned a value exactly once.
    One of the main reasons for using With rather than Module is that it typically makes the Mathematica programs you write easier to understand. In a module, if you see a local variable x at a particular point, you potentially have to trace through all of the code in the module to work out the value of x at that point. In a With construct, however, you can always find out the value of a local constant simply by looking at the initial list of values, without having to trace through specific code.
    If you have several With constructs, it is always the innermost one for a particular variable that is in effect. You can mix Module and With. The general rule is that the innermost one for a particular variable is the one that is in effect.

  • With nested With constructs, the innermost one is always the one in effect.
  • In[9]:= With[{t = 8}, With[{t = 9}, t^2]]


  • You can mix Module and With constructs.
  • In[10]:= Module[{t = 8}, With[{t = 9}, t^2]]


  • Local variables in inner constructs do not mask ones outside unless the names conflict.
  • In[11]:= With[{t = a}, With[{u = b}, t + u]]


    Except for the question of when x and body are evaluated, With[


    ,body] works essentially like body/.x->. However, With behaves in a special way when the expression body itself contains With or Module constructs. The main issue is to prevent the local constants in the various With constructs from conflicting with each other, or with global objects. The details of how this is done are discussed in Section 2.6.3


  • The y in the inner With is renamed to prevent it from conflicting with the global y.
  • In[12]:= With[{x = 2 + y}, Hold[With[{y = 4}, x + y]]]