2.5.6 Making Definitions for Functions
Section 1.7.1 discussed how you can define functions in Mathematica. In a typical case, you would type in f[x_] = x^2 to define a function f. (Actually, the definitions in Section 1.7.1 used the := operator, rather than the = one. Section 2.5.8 will explain exactly when to use each of the := and = operators.)
The definition f[x_] = x^2 specifies that whenever Mathematica encounters an expression which matches the pattern f[x_], it should replace the expression by x^2. Since the pattern f[x_] matches all expressions of the form f[anything], the definition applies to functions f with any "argument".
Function definitions like f[x_] = x^2 can be compared with definitions like f[a] = b for indexed variables discussed in the previous subsection. The definition f[a] = b specifies that whenever the particular expression f[a] occurs, it is to be replaced by b. But the definition says nothing about expressions such as f[y], where f appears with another "index".
To define a "function", you need to specify values for expressions of the form f[x], where the argument x can be anything. You can do this by giving a definition for the pattern f[x_], where the pattern object x_ stands for any expression.
The difference between defining an indexed variable and a function.
Making definitions for f[2] or f[a] can be thought of as being like giving values to various elements of an "array" named f. Making a definition for f[x_] is like giving a value for a set of "array elements" with arbitrary "indices". In fact, you can actually think of any function as being like an array with an arbitrarily variable index.
In mathematical terms, you can think of f as a mapping. When you define values for, say, f[1] and f[2], you specify the image of this mapping for various discrete points in its domain. Defining a value for f[x_] specifies the image of f on a continuum of points.
This defines a transformation rule for the specific expression f[x].
In[1]:= f[x] = u
Out[1]=
When the specific expression f[x] appears, it is replaced by u. Other expressions of the form f[argument] are, however, not modified.
In[2]:= f[x] + f[y]
Out[2]=
This defines a value for f with any expression as an "argument".
In[3]:= f[x_] = x^2
Out[3]=
The old definition for the specific expression f[x] is still used, but the new general definition for f[x_] is now used to find a value for f[y].
In[4]:= f[x] + f[y]
Out[4]=
This removes all definitions for f.
In[5]:= Clear[f]
Mathematica allows you to define transformation rules for any expression or pattern. You can mix definitions for specific expressions such as f[1] or f[a] with definitions for patterns such as f[x_].
Many kinds of mathematical functions can be set up by mixing specific and general definitions in Mathematica. As an example, consider the factorial function. This particular function is in fact built into Mathematica (it is written n!). But you can use Mathematica definitions to set up the function for yourself.
The standard mathematical definition for the factorial function can be entered almost directly into Mathematica, in the form: f[n_] := n f[n1]; f[1] = 1. This definition specifies that for any n, f[n] should be replaced by n f[n1], except that when n is 1, f[1] should simply be replaced by 1.
Here is the value of the factorial function with argument 1.
In[6]:= f[1] = 1
Out[6]=
Here is the general recursion relation for the factorial function.
In[7]:= f[n_] := n f[n1]
Now you can use these definitions to find values for the factorial function.
In[8]:= f[10]
Out[8]=
The results are the same as you get from the builtin version of factorial.
In[9]:= 10!
Out[9]=
