Making Definitions for Functions

"Defining Functions" discusses how you can define functions in the Wolfram Language. In a typical case, you would type in to define a function . (Actually, the definitions in "Defining Functions" use the operator, rather than the one. "Immediate and Delayed Definitions" explains exactly when to use each of the and operators.)

The definition specifies that whenever the Wolfram Language encounters an expression that matches the pattern , it should replace the expression by . Since the pattern matches all expressions of the form f[anything], the definition applies to functions with any "argument".

Function definitions like can be compared with definitions like for indexed variables discussed in "Making Definitions for Indexed Objects". The definition specifies that whenever the particular expression occurs, it is to be replaced by . But the definition says nothing about expressions such as , where 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 , where the pattern object stands for any expression.

f[x]=valuedefinition for a specific expression x
f[x_]=valuedefinition for any expression, referred to as x

The difference between defining an indexed variable and a function.

Making definitions for or can be thought of as being like giving values to various elements of an "array" named . Making a definition for 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 as a mapping. When you define values for, say, and , you specify the image of this mapping for various discrete points in its domain. Defining a value for specifies the image of on a continuum of points.

This defines a transformation rule for the specific expression .
In[1]:=
Click for copyable input
Out[1]=
When the specific expression appears, it is replaced by . Other expressions of the form f[argument] are, however, not modified.
In[2]:=
Click for copyable input
Out[2]=
This defines a value for with any expression as an "argument".
In[3]:=
Click for copyable input
Out[3]=
The old definition for the specific expression is still used, but the new general definition for is now used to find a value for .
In[4]:=
Click for copyable input
Out[4]=
This removes all definitions for .
In[5]:=
Click for copyable input

The Wolfram Language allows you to define transformation rules for any expression or pattern. You can mix definitions for specific expressions such as or with definitions for patterns such as .

Many kinds of mathematical functions can be set up by mixing specific and general definitions in the Wolfram Language. As an example, consider the factorial function. This particular function is in fact built into the Wolfram Language (it is written n!). But you can use Wolfram Language definitions to set up the function for yourself.

The standard mathematical definition for the factorial function can be entered almost directly into the Wolfram Language, in the form . This definition specifies that for any n, f[n] should be replaced by , except that when n is , should simply be replaced by .

Here is the value of the factorial function with argument 1.
In[6]:=
Click for copyable input
Out[6]=
Here is the general recursion relation for the factorial function.
In[7]:=
Click for copyable input
Now you can use these definitions to find values for the factorial function.
In[8]:=
Click for copyable input
Out[8]=
The results are the same as you get from the builtin version of factorial.
In[9]:=
Click for copyable input
Out[9]=