Immediate and Delayed Definitions

You may have noticed that there are two different ways to make assignments in Mathematica: and . The basic difference between these forms is when the expression rhs is evaluated. is an immediate assignment, in which rhs is evaluated at the time when the assignment is made. , on the other hand, is a delayed assignment, in which rhs is not evaluated when the assignment is made, but is instead evaluated each time the value of lhs is requested.

lhs=rhs (immediate assignment)rhs is evaluated when the assignment is made
lhs:=rhs (delayed assignment)rhs is evaluated each time the value of lhs is requested

The two types of assignments in Mathematica.

This uses the operator to define the function .
In[1]:=
Click for copyable input
Because was used, the definition is maintained in an unevaluated form.
When you make an assignment with the operator, the right-hand side is evaluated immediately.
In[3]:=
Click for copyable input
Out[3]=
The definition now stored is the result of the Expand command.
When you execute , the Expand is performed.
In[5]:=
Click for copyable input
Out[5]=
simply substitutes its argument into the already expanded form, giving a different answer.
In[6]:=
Click for copyable input
Out[6]=

As you can see from the example above, both and can be useful in defining functions, but they have different meanings, and you must be careful about which one to use in a particular case.

One rule of thumb is the following. If you think of an assignment as giving the final "value" of an expression, use the operator. If instead you think of the assignment as specifying a "command" for finding the value, use the operator. If in doubt, it is usually better to use the operator than the one.

lhs=rhsrhs is intended to be the "final value" of lhs (e.g. )
lhs:=rhsrhs gives a "command" or "program" to be executed whenever you ask for the value of lhs (e.g. f[x_]:=Expand[1-x^2])

Interpretations of assignments with the and operators.

Although is probably used more often than in defining functions, there is one important case in which you must use to define a function. If you do a calculation, and get an answer in terms of a symbolic parameter , you often want to go on and find results for various specific values of . One way to do this is to use the operator to apply appropriate rules for in each case. It is usually more convenient, however, to use to define a function whose argument is .

Here is an expression involving .
In[7]:=
Click for copyable input
Out[7]=
This defines a function whose argument is the value to be taken for .
In[8]:=
Click for copyable input
Out[8]=
Here is the result when is taken to be .
In[9]:=
Click for copyable input
Out[9]=

An important point to notice in the example above is that there is nothing special about the name that appears in the pattern. It is just a symbol, indistinguishable from an that appears in any other expression.

f[x_]=exprdefine a function which gives the value expr for any particular value of x

Defining functions for evaluating expressions.

You can use and not only to define functions, but also to assign values to variables. If you type , then value is immediately evaluated, and the result is assigned to x. On the other hand, if you type , then value is not immediately evaluated. Instead, it is maintained in an unevaluated form, and is evaluated afresh each time is used.

This evaluates RandomReal[] to find a pseudorandom number, then assigns this number to .
In[10]:=
Click for copyable input
Out[10]=
Here RandomReal[] is maintained in an unevaluated form, to be evaluated afresh each time is used.
In[11]:=
Click for copyable input
Here are values for and .
In[12]:=
Click for copyable input
Out[12]=
The value of never changes. Every time is used, however, a new pseudorandom number is generated.
In[13]:=
Click for copyable input
Out[13]=

The distinction between immediate and delayed assignments is particularly important when you set up chains of assignments.

This defines to be .
In[14]:=
Click for copyable input
Out[14]=
Here is evaluated to give , and the result is assigned to be the value of .
In[15]:=
Click for copyable input
Out[15]=
Here is maintained in an unevaluated form, to be evaluated every time the value of is requested.
In[16]:=
Click for copyable input
In this case, and give the same values.
In[17]:=
Click for copyable input
Out[17]=
Now the value of is changed.
In[18]:=
Click for copyable input
Out[18]=
Now uses the new value for , while keeps its original value.
In[19]:=
Click for copyable input
Out[19]=

You can use delayed assignments such as to set up variables whose values you can find in a variety of different "environments". Every time you ask for , the expression rhs is evaluated using the current values of the objects on which it depends.

The right-hand side of the delayed assignment is maintained in an unevaluated form.
In[20]:=
Click for copyable input
This sets to , then finds the value of .
In[21]:=
Click for copyable input
Out[21]=
Here is 6.
In[22]:=
Click for copyable input
Out[22]=

In the example above, the symbol acts as a "global variable", whose value affects the value of . When you have a large number of parameters, many of which change only occasionally, you may find this kind of setup convenient. However, you should realize that implicit or hidden dependence of one variable on others can often become quite confusing. When possible, you should make all dependencies explicit, by defining functions which take all necessary parameters as arguments.

lhs->rhsrhs is evaluated when the rule is given
lhs:>rhsrhs is evaluated when the rule is used

Two types of transformation rules in Mathematica.

Just as you can make immediate and delayed assignments in Mathematica, so you can also set up immediate and delayed transformation rules.

The right-hand side of this rule is evaluated when you give the rule.
In[23]:=
Click for copyable input
Out[23]=
A rule like this is probably not particularly useful.
In[24]:=
Click for copyable input
Out[24]=
Here the right-hand side of the rule is maintained in an unevaluated form, to be evaluated every time the rule is used.
In[25]:=
Click for copyable input
Out[25]=
Applying the rule causes the expansion to be done.
In[26]:=
Click for copyable input
Out[26]=

In analogy with assignments, you should typically use when you want to replace an expression with a definite value, and you should use when you want to give a command for finding the value.

New to Mathematica? Find your learning path »
Have a question? Ask support »