How to | Work with Pure Functions

The ability to define and use your own functions is part of what gives the Wolfram Language such power. It is often inconvenient to have to explicitly name a function for every small operation that you wish to perform. The Wolfram Language 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:

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

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

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

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

    

Pure functions become quite powerful when used together with Map.

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

This turns the ordered pairs back into complex numbers:

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

  • Pure Functions
  • Functional Operations
  • Variables in Pure Functions and Rules
  • How to: Work with Variables and Functions
  • Function  Map  Apply  Listable

  • "How to" Topics