You are wise to be interested in this syntax. It can significantly streamline code.
In my opinion it should be used more often.
There are two primary ways to build this kind of function: a SubValues definition, and Function
.
Either form will let you define and use f
as f = gain[1, 2]
.
SubValues
SubValues are created when you make a definition like this:
gain[b_, w_][v_] := 1/Sqrt[(w^2 - v^2)^2 + b^2 v^2]
SubValues[gain]
{HoldPattern[gain[b_, w_][v_]] :> 1/Sqrt[(w^2 - v^2)^2 + b^2*v^2]}
With this form you are not actually creating a function when you call gain[1, 2]
.
It simply sits unevaluated until the third argument is given.
Unlike the Function
method you retain full pattern capabilities with the additional arguments. You can for instance define this, which would require If
or Switch
in Function
:
f[a_, b_][n_?EvenQ] := a^b + n
f[a_, b_][n_?OddQ] := b^a - n
Function
The other common method is to actually build a Function
:
ClearAll[gain]
gain[b_, w_] := Function[v, 1/Sqrt[(w^2 - v^2)^2 + b^2 v^2]]
This definition is stored in DownValues
:
DownValues[gain]
{HoldPattern[gain[b_, w_]] :>
Function[v, 1/Sqrt[(w^2 - v^2)^2 + b^2*v^2]]}
With this form when you call gain[1, 2]
you get this:
Function[v$, 1/Sqrt[(2^2 - v$^2)^2 + 1^2*v$^2]]
One important use for this form is when you need the additional arguments (in your example v
) to be held unevaluated with HoldAll
, etc. The SubValues
form cannot do this*, but the third argument of Function
can:
stylePrint[style_] :=
Function[expr, Print@Style[HoldForm@expr, style], HoldAll]
printRed = stylePrint[{18, Red, Bold}];
printRed[2 + 3 + 1^2]

This form is also superior if your function can be partially evaluated because the result of the evaluation can be incorporated into the Function
, often using With
.
Additional notes
Be mindful of the fact that you can accidentally blend these forms in a potentially confusing way. If you define a SubValue
first (or later, if it is a form that does not evaluate on a pattern) and later a DownValue
with the same pattern you may get unexpected results.
ClearAll[gain]
gain[b_, w_][v_] := 1/Sqrt[(w^2 - v^2)^2 + b^2 v^2]
gain[x_, y_] := Sin[x y] + Cos[y/x]
gain[1, 2][3]
(Cos[2] + Sin[2])[3]
This could be particularly confusing (or powerful) if you use a constrained pattern like gain[x_Real, y_] := . . .
as you may have one pattern match in one case and the other in another.
While the two forms I show above are the most common and arguably best it is worth understanding that they are not exclusive. Because heads evaluate first, including x[y]
in x[y][z]
, any definition for x[y]
that returns a function can act upon z
. Here is stylePrint
again, using neither SubVales
nor Function
:
stylePrint2[style_] :=
Module[{fn},
SetAttributes[fn, HoldAll];
fn[expr_] := Print@Style[HoldForm@expr, style];
fn
]
"Pure functions" with machine-size arguments can often be auto-compiled in a way that pattern-based definitions (*Values
) cannot. This is attempted by various functions such as Map
, Table
, and Fold
as controlled by the values in SystemOptions["CompileOptions"]
.
Let us compare the performance of SubValues
and Function
using machine-size Real
numbers:
g1[b_, w_][v_] := 1/Sqrt[(w^2 - v^2)^2 + b^2 v^2]
g2[b_, w_] := Function[v, 1/Sqrt[(w^2 - v^2)^2 + b^2 v^2]]
Table[
f /@ RandomReal[14, 500000] // Timing // First,
{f, {g1[0.3, 0.7], g2[0.3, 0.7]}}
]
{1.466, 0.078}
* I said that SubValues definitions cannot handle Hold attributes for the additional arguments. Strictly this is true, but there are some clever workarounds, with limitations. Please see these Q&A's for examples and further discussion: