This is documentation for Mathematica 3, which was
based on an earlier version of the Wolfram Language.
View current documentation (Version 11.2)
 Documentation / Mathematica / Built-in Functions / Programming / Functional Programming  /

  • Function[ body ] or body & is a pure function. The formal parameters are # (or #1), #2, etc.
  • Function[ x , body ] is a pure function with a single formal parameter x.
  • Function[ , , ... , body ] is a pure function with a list of formal parameters.
  • Example: (# + 1)&[x].
  • Map[(# + 1)&, x, y, z ].
  • When Function[ body ] or body & is applied to a set of arguments, # (or #1) is replaced by the first argument, #2 by the second, and so on. #0 is replaced by the function itself.
  • If there are more arguments supplied than # i in the function, the remaining arguments are ignored.
  • ## stands for the sequence of all arguments supplied.
  • ## n stands for arguments from number n on.
  • f[##, ##2]& [x, y, z].
  • Function is analogous to in LISP or formal logic.
  • Function has attribute HoldAll. The function body is evaluated only after the formal parameters have been replaced by arguments.
  • The named formal parameters in Function[ , ... , body ] are treated as local, and are renamed $ when necessary to avoid confusion with actual arguments supplied to the function.
  • Function is treated as a scoping construct (see Section A.3.8).
  • Function[ params , body , , , ... ] represents a pure function that is to be treated as having attributes for the purpose of evaluation.
  • See the Mathematica book: Section 2.2.5.
  • See also: Apply, CompiledFunction.

    Further Examples

    Here is a pure function that squares its argument.



    You can pass an argument to pure functions just as if they were ordinary functions like Sin or Min.





    These use the shorthand notation for a pure function.





    If your function needs several parameters, you specify them explicitly.