({{, body& or (RefLink[Function, paclet:ref/Function])[body]
is a pure (or "anonymous") function. The formal parameters are # (or #1), #2, etc. }, {, (RefLink[Function, paclet:ref/Function])[x,body]
is a pure function with a single formal parameter x. }, {, (RefLink[Function, paclet:ref/Function])[{(Subscript[x, 1]),(Subscript[x, 2]),…},body]
is a pure function with a list of formal parameters. }, {, (RefLink[Function, paclet:ref/Function])[params,body,attrs]
is a pure function that is treated as having attributes attrs for purposes of evaluation.}})

OpenersNotesSection(Spacer1)Details

When (RefLink[Function, paclet:ref/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 onward. »

When applied to an association, #name is equivalent to #["name"], and picks out elements in the association.

In the form #name, the characters in name can be any combination of alphanumeric characters not beginning with digits.

(RefLink[Function, paclet:ref/Function])[x,body] can be input as x|->body, where the character |-> is entered as (Key0[esc]) fn (Key0[esc]) or (RefLink[\[Function], paclet:ref/character/Function]).

(RefLink[Function, paclet:ref/Function]) is analogous to λ in LISP or formal logic.

(RefLink[Function, paclet:ref/Function]) has attribute (RefLink[HoldAll, paclet:ref/HoldAll]). The function body is evaluated only after the formal parameters have been replaced by arguments.

The named formal parameters (Subscript[x, i]) in (RefLink[Function, paclet:ref/Function])[{(Subscript[x, 1]),…},body] are treated as local, and are renamed (Subscript[x, i])$ when necessary to avoid confusion with actual arguments supplied to the function. »

(RefLink[Function, paclet:ref/Function]) constructs can be nested in any way. Each is treated as a scoping construct, with named inner variables being renamed if necessary. »

In (RefLink[Function, paclet:ref/Function])[params, body, attrs], attrs can be a single attribute or a list of attributes.  »

(RefLink[Function, paclet:ref/Function])[(RefLink[Null, paclet:ref/Null]),body,attrs] represents a function in which the parameters in body are given using # etc.

OpenersPrimaryExamplesSection(Spacer1)Examples  (43)

OpenersExampleSection0(Spacer1)Basic Examples  (4)

Pure function with one parameter:

Function[u,3+u][x]

Function[3+#][x]

(3+#)&[x]

Pure function with two parameters:

Function[{u,v},u^2+v^4][x,y]

(#1^2+#2^4)&[x,y]

Set f to be a pure function:

f=(3+#)&

Use the pure function:

{f[a],f[b]}

Pick out named arguments from an association:

f[#u,#v,#u]&[<|"u"->x,"v"->y|>]

OpenersExampleSection1(Spacer1)Scope  (15)

OpenersExampleSubsection0(Spacer1)Use a Pure Function as an Argument  (5)

Map a pure function over a list:

g[#,#^2]&/@{x,y,z}

Select with a pure function:

Select[{1,-1,2,-2,3},#>0&]

Use a pure function as a predicate:

Cases[{1,-1,2,-2,3},_Integer?(#>0&)]

Create an array from a pure function:

Array[1+#^2&,10]

Sort by comparing the second part of each element:

Sort[{{a,2},{c,1},{d,3}},#1[[2]]<#2[[2]]&]

OpenersExampleSubsection1(Spacer1)Use a Pure Function as an Option Value  (3)

Specify a custom comparison function in (RefLink[FixedPoint, paclet:ref/FixedPoint]):

FixedPoint[(# + 2/# )/2&,1`20,SameTest->(Abs[#1-#2]<1*^-10&)]

Specify a custom color function:

DensityPlot[Sin[x y],{x,0,3},{y,0,3},ColorFunction->(RGBColor[1-#,#,1-#]&)]

Provide a custom distance function:

Nearest[{1,2,4,8,16},5,DistanceFunction->((#1-#2)^2&)]

OpenersExampleSubsection2(Spacer1)Return a Pure Function as a Result  (4)

Derivative of a pure function:

Function[x,x^2]'

Derivative of (RefLink[Tan, paclet:ref/Tan]):

Tan'

Solutions of differential equations may be expressed as pure functions:

DSolve[{y'[x]==ay[x],y[0]==1},y,x]

Difference equations may return pure functions:

RSolve[{a[n+1]-2a[n]==1,a[0]==1},a,n]

OpenersExampleSubsection3(Spacer1)Function and Associations  (3)

#name is effectively a short form of #["name"]:

#x&[<|"x"->a,"y"->b|>]

#["x"]&[<|"x"->a,"y"->b|>]

#name always refers to the association in the first argument:

#y&[<|"x"->1,"y"->2|>, <|"x"->3,"y"->4|>]

Extract from an association slot other than the first:

#2["y"]&[<|"x"->1,"y"->2|>, <|"x"->3,"y"->4|>]

OpenersExampleSection2(Spacer1)Generalizations & Extensions  (4)

## stands for all arguments:

f[##]&[a,b,c,d]

f[X,##,Y,##]&[a,b,c,d]

##n stands for arguments n and onward:

f[##2]&[a,b,c,d]

f[##1,X,##2,Y,##3,Z,##4]&[a,b,c,d]

Create a pure function with attribute (RefLink[Listable, paclet:ref/Listable]):

Function[{u},g[u],Listable][{a,b,c}]

Function[{u},g[u]][{a,b,c}]

#0 stands for the whole pure function:

f[#0]&[x]

A recursive definition for factorial using #0:

f=If[#1==1,1,#1 #0[#1-1]]&

f[10]

OpenersExampleSection3(Spacer1)Applications  (3)

Turn a function that takes several arguments into one that takes a list of arguments:

cplus=Plus@@#&

cplus[{a,b,c}]

A function that returns a function that multiplies its argument by n:

makef[n_]:=Function[x,n x]

f2=makef[2]

f2[5]

Preserve arguments in unevaluated form:

Select[Hold[x,$MaxMachineNumber],Function[symbol,Context[symbol]==="System`",HoldAll]]

OpenersExampleSection4(Spacer1)Properties & Relations  (11)

#1 uses only the first argument supplied; the rest are ignored:

f[#1]&[x,y,z]

Not using any arguments results in a constant pure function:

17&/@{1,2,3}

Replacements can be done inside pure functions:

(p+#)& /.p->q

%[x]

Formal parameters are renamed whenever there is a possibility of confusion:

Function[{x},Function[{y},f[x,y]]][y]

Function[{x},Function[{y},f[x,y]]][a]

Function[{x},Function[{y},Function[{z},f[x,y,z]]]][a]

The names of the parameters do not matter:

Function[x,Function[y,x^y]][x][y]

Function[y,Function[x,y^x]][x][y]

However, reusing a name introduces a new scope:

Function[x,Function[x,x^x]][x][y]

Nested functions take their arguments one at a time:

Function[x,Function[y,x^y]][a][b]

Function[{x,y},x^y][a,b]

f[#]& is the same as simply f in the univariate case:

f[#]&[a]

In general, f[##]& is the same as f:

f[##]&[a,b,c]

Turn a formula involving a variable x into a pure function:

formula=(1+x)^2;

Function[x,Evaluate[formula]]

Use a formula in (RefLink[Table, paclet:ref/Table]):

Table[i^2,{i,10}]

Use the corresponding pure function in an equivalent (RefLink[Array, paclet:ref/Array]) expression:

Array[#^2&,10]

Special-purpose function constructs include (RefLink[InterpolatingFunction, paclet:ref/InterpolatingFunction]):

f=Interpolation[{1,2,4,5,6}]

f[4]

(RefLink[CompiledFunction, paclet:ref/CompiledFunction]):

f=Compile[{x},x^2]

f[5.0]

(RefLink[NearestFunction, paclet:ref/NearestFunction]):

f=Nearest[{1,2,3,4,5}]

f[3.1]

(RefLink[LinearSolveFunction, paclet:ref/LinearSolveFunction]):

f=LinearSolve[{{1,2},{3,4}}]

f[{5,6}]

OpenersExampleSection5(Spacer1)Possible Issues  (4)

& binds more loosely than ->, so it usually needs parentheses in rules:

FullForm[x->y&]

FullForm[x->(y&)]

& binds more loosely than ?, so it usually needs parentheses in pattern tests:

Cases[{1,2,3,4},_?(OddQ[#/2]&)]

(RefLink[Function, paclet:ref/Function]) does not evaluate its body until the function is applied:

(#+#+#)&

%[a]

Supplying fewer than the required number of arguments generates an error:

#2&[x]

OpenersExampleSection6(Spacer1)Neat Examples  (2)

Define the recursion operator of recursion theory [(WebLink[more info, http://www.wolframscience.com/nksonline/page-907b-text])]:

r[g_,h_]=If[#1==0,g[##2],h[#0[#1-1,##2],#1-1,##2]]&

Use it to define the factorial function:

r[1&,#1(#2+1)&][10]

Newton's formula for finding a zero of a function:

NewtonZero[f_,x0_]:=FixedPoint[#-f[#]/f'[#]&,x0]

NewtonZero[BesselJ[2,#]&,5.0]

({{DocumentationFooterIconsRelatedFunction.png, ({{See Also}, {(RefLink[Apply, paclet:ref/Apply])  ▪  (RefLink[Construct, paclet:ref/Construct])  ▪  (RefLink[Curry, paclet:ref/Curry])  ▪  (RefLink[CompiledFunction, paclet:ref/CompiledFunction])  ▪  (RefLink[InterpolatingFunction, paclet:ref/InterpolatingFunction])  ▪  (RefLink[Slot, paclet:ref/Slot])  ▪  (RefLink[SlotSequence, paclet:ref/SlotSequence])}})}})

({{DocumentationFooterIconsRelatedTutorial.png, ({{Tutorials}, {▪(RefLinkPlain[Pure Functions, paclet:tutorial/PureFunctions])}, {▪(RefLinkPlain[Variables in Pure Functions and Rules, paclet:tutorial/VariablesInPureFunctionsAndRules])}, {▪(RefLinkPlain[Some General Notations and Conventions, paclet:tutorial/SomeGeneralNotationsAndConventions])}})}})

({{DocumentationFooterIconsRelatedGuide.png, ({{Related Guides}, {▪(RefLinkPlain[Functional Programming, paclet:guide/FunctionalProgramming])}, {▪(RefLinkPlain[Scoping Constructs, paclet:guide/ScopingConstructs])}, {▪(RefLinkPlain[Attributes, paclet:guide/Attributes])}, {▪(RefLinkPlain[Function Composition & Operator Forms, paclet:guide/FunctionCompositionAndOperatorForms])}, {▪(RefLinkPlain[Wolfram Language Syntax, paclet:guide/Syntax])}, {▪(RefLinkPlain[Computation with Structured Datasets, paclet:guide/ComputationWithStructuredDatasets])}, {▪(RefLinkPlain[Language Overview, paclet:guide/LanguageOverview])}})}})

({{(Spacer1), ({{(Spacer1)Related Links}, {(WebLink[DocumentationFooterIconsRelatedLinks-FastIntro.png(Spacer1)Fast Introduction for Programmers: Pure Functions, http://www.wolfram.com/language/fast-introduction-for-programmers/pure-functions/])}, {(WebLink[DocumentationFooterIconsRelatedLinks-EIWL.png(Spacer1)An Elementary Introduction to the Wolfram Language, https://www.wolfram.com/language/elementary-introduction/26-pure-anonymous-functions.html])(WebLink[: Pure Anonymous Functions, https://www.wolfram.com/language/elementary-introduction/26-pure-anonymous-functions.html])}, {(WebLink[DocumentationFooterIconsRelatedLinks-NKS.png(Spacer1)NKS|Online, http://www.wolframscience.com/nksonline/index/search.cgi?SearchIndex=Function]) ((WebLink[A New Kind of Science, http://www.wolframscience.com/nksonline/]))}})}})