How to | Work with Pure Functions

The ability to define and use your own functions is part of what gives Mathematica such power. It is often inconvenient to have to explicitly name a function for every small operation that you wish to perform. Mathematica lets you declare functions inline (called pure functions) to get around this.

The most transparent way to define a pure function is with Function. The first argument is a list of arguments, and the second is a function. This function adds its two arguments together:

In[1]:=
Click for copyable input
Out[1]=
In[2]:=
Click for copyable input
Out[2]=

You do not need to give the function a name to use it:

In[3]:=
Click for copyable input
Out[3]=

A common shorthand notation uses an to mark the end of the pure function, with argument positions specified by , , and so on:

In[4]:=
Click for copyable input
Out[4]=
In[5]:=
Click for copyable input
Out[5]=

The advantage of a pure function is that it does not require a separate definition or a name:

In[6]:=
Click for copyable input
Out[6]=

If the pure function only has one argument, you can use instead of . This function squares its argument:

In[7]:=
Click for copyable input
Out[7]=
    

Pure functions become quite powerful when used together with Map.

This makes a list of complex numbers into a list of ordered pairs:

In[8]:=
Click for copyable input
Out[8]=

This turns the ordered pairs back into complex numbers:

In[9]:=
Click for copyable input
Out[9]=

You can use as a shorthand for Map. Use a pure function to create a list of clickable buttons:

In[2]:=
Click for copyable input
Out[2]=
New to Mathematica? Find your learning path »
Have a question? Ask support »