Mathematica HowTo
How to | Create Definitions for Variables and Functions
Mathematica has a very general notion of functions, as rules for arbitrary transformations. Values for variables are also assigned in this manner. When you set a value for a variable, the variable becomes a symbol for that value.
Here is a simple transformation rule. It says: whenever you see x, replace it by 3:
In[31]:=
Click for copyable input
Out[31]=
The variable x has a value of 3.
Whenever you evaluate an expression, 3 is substituted for x:
In[32]:=
Click for copyable input
Out[32]=
You can remove the rule by defining a new one:
In[33]:=
Click for copyable input
Out[33]=
The new rule says: whenever you see x, replace it by y^2. So far there are no rules associated with y, so its value is itself.
Assign a value to y:
In[34]:=
Click for copyable input
Out[34]=
Now if you evaluate x, the rule for x says to replace x by y^2, and the rule for y says to replace y by 4, so the result is 4^2, or 16:
In[35]:=
Click for copyable input
Out[35]=
If you change the value of y, then the value of x changes:
In[36]:=
Click for copyable input
Out[36]=
In[37]:=
Click for copyable input
Out[37]=
Now assign a value to z, like this:
In[38]:=
Click for copyable input
Out[38]=
Since y has already been assigned the value 3, the rule you have defined is "replace z by 9", not "replace z by y^2". So z does not depend on y:
In[39]:=
Click for copyable input
Out[39]=
In[40]:=
Click for copyable input
Out[40]=
This happened because when a rule is defined using = (Set), the right-hand side is evaluated before the rule is defined.
You can also define rules using := (SetDelayed), like this:
In[41]:=
Click for copyable input
When a rule is defined with := the right-hand side is not evaluated before the rule is defined. So even if y already has a value, this new rule says: whenever you see z, replace it with y^2. So in this case, z depends on y:
In[42]:=
Click for copyable input
Out[42]=
In[43]:=
Click for copyable input
Out[43]=
In[44]:=
Click for copyable input
Out[44]=
Functions in Mathematica are defined by rules that act on patterns. Here is a simple one:
In[45]:=
Click for copyable input
f[x_] is a pattern in which x_ stands for any expression (which we represent on the right-hand side by the name x). The rule says: if you have f of any expression, replace it by that expression squared:
In[46]:=
Click for copyable input
Out[46]=
In[58]:=
Click for copyable input
Out[58]=
Here is a function with two arguments:
In[48]:=
Click for copyable input
In[49]:=
Click for copyable input
Out[49]=
Always use := to define functions, otherwise the variables on the right-hand side may not represent the associated expressions on the left-hand side, since they will be evaluated before the rule is defined:
In[50]:=
Click for copyable input
Out[50]=
That happened because x is 9 and y is 3. This rule says that anything matching the pattern h[x_, y_] is replaced by 90:
In[59]:=
Click for copyable input
Out[59]=
Ask a question about this page  |  Suggest an improvement  |  Leave a message for the team