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

ContentsLocal Constants

2.6.1 Modules and Local Variables

Mathematica normally assumes that all your variables are global. This means that every time you use a name like x, Mathematica normally assumes that you are referring to the same object.

Particularly when you write programs, however, you may not want all your variables to be global. You may, for example, want to use the name x to refer to two quite different variables in two different programs. In this case, you need the x in each program to be treated as a local variable.

You can set up local variables in Mathematica using modules. Within each module, you can give a list of variables which are to be treated as local to the module.

Creating modules in Mathematica.

This defines the global variable t to have value 17.

In[1]:= t = 17


The t inside the module is local, so it can be treated independently of the global t.

In[2]:= Module[{t}, t=8; Print[t]]

The global t still has value 17.

In[3]:= t


The most common way that modules are used is to set up temporary or intermediate variables inside functions you define. It is important to make sure that such variables are kept local. If they are not, then you will run into trouble whenever their names happen to coincide with the names of other variables.

The intermediate variable t is specified to be local to the module.

In[4]:= f[v_] := Module[{t}, t = (1 + v)^2; t = Expand[t] ]

This runs the function f.

In[5]:= f[a + b]


The global t still has value 17.

In[6]:= t


You can treat local variables in modules just like other symbols. Thus, for example, you can use them as names for local functions, you can assign attributes to them, and so on.

This sets up a module which defines a local function f.

In[7]:= gfac10[k_] :=

Module[{f, n}, f[1] = 1; f[n_] := k + n f[n-1]; f[10]]

In this case, the local function f is just an ordinary factorial.

In[8]:= gfac10[0]


In this case, f is set up as a generalized factorial.

In[9]:= gfac10[2]


When you set up a local variable in a module, Mathematica initially assigns no value to the variable. This means that you can use the variable in a purely symbolic way, even if there was a global value defined for the variable outside the module.

This uses the global value of t defined above, and so yields a number.

In[10]:= Expand[(1 + t)^3]


Here Length simply receives a number as its argument.

In[11]:= Length[Expand[(1 + t)^3]]


The local variable t has no value, so it acts as a symbol, and Expand produces the anticipated algebraic result.

In[12]:= Module[{t}, Length[Expand[(1 + t)^3]]]


Assigning initial values to local variables.

This specifies t to be a local variable, with initial value u.

In[13]:= g[u_] := Module[{ t = u }, t += t/(1 + u)]

This uses the definition of g.

In[14]:= g[a]


You can define initial values for any of the local variables in a module. The initial values are always evaluated before the module is executed. As a result, even if a variable x is defined as local to the module, the global x will be used if it appears in an expression for an initial value.

The initial value of u is taken to be the global value of t.

In[15]:= Module[{t = 6, u = t}, u^2]


Using local variables in definitions with conditions.

When you set up /; conditions for definitions, you often need to introduce temporary variables. In many cases, you may want to share these temporary variables with the body of the right-hand side of the definition. Mathematica allows you to enclose the whole right-hand side of your definition in a module, including the condition.

This defines a function with a condition attached.

In[16]:= h[x_] := Module[{t}, t^2 - 1 /; (t = x - 4) > 1]

Mathematica shares the value of the local variable t between the condition and the body of the right-hand side.

In[17]:= h[10]


ContentsLocal Constants