Is there any way to define pure functions with optional arguments?

As far as I know there is no way to do this with the named parameter form of Function but you can use destructuring methods with SlotSequence (##):

f = {##} /. {u_: 1, v_: 0} :> body[u, v] &;

f[]
f[7]
f[7, 8]
body[1, 0]

body[7, 0]

body[7, 8]

It is possible to give your pure function Attributes using an undocumented form.
For Hold attributes you could use Hold or HoldComplete:

g = Function[Null, Hold[##] /. _[u_: 1, v_: 0] :> HoldForm[{u, v}], HoldAll];

g[1 + 1]
{1 + 1, 0}

I have left bottom part of this answer as a warning against not thinking :)

Here is my improvement. It is not so handy but it allows us to specify which argument has to take it's optional value.

edit - scoping.

f = Module[{x, y, z},
    Function[{u, v, g}, x^2 y^4 + z^4 /. {x -> (u /. Null -> 3), 
                                          y -> (v /. Null -> 2), 
                                          z -> g}] (*3rd arg has no opt. value*)
          ]

f[2, 1, 1]
f[, , 1]
f[1, , 1]
5

145

17

I do not know if it is worth the effort but one may want to scope x, y, z and make replacement more compact.


the warning :)

The following method will work only when the result of the function has different value than the value of one of arguments. I such case the result is will be replaced by it's optinal value.

Following examples are only lucky coincidence

This approach is not identical as standard optional arguments but it is interesting (but not working :) ).

f = Function[{u, v}, u^2 + v^4 /. {u -> 1, v -> 1}]

f[1,2]
17
f[,]
2

As you see, it is not like with standard optional arguments because you have to put comma inside. Thats because

f = Function[{u,v} ...]


In this answer, I focus on the case of pure functions with one or more Slot in their body, which is different than the other answers.

First I present the function optionalFu which works just like Function, but which turns of a message. This message is the one that is generated when not all slots can be filled from the given input, like in {#,#2}&[5]. It is interesting to interpret this message as a warning, rather than an error and to ignore it, because Mathematica still fills all the slots it can. So {#,#2}&[5] evaluates to {5,#2} and it remains to be seen what to do with #2. The second function in this answer, optFuWithDefaults, replaces such slots (#2) with default values.

Be warned that the code for optionalFu and optFuWithDefaults are both very unreadable, but they appear to work nicely and it definitely more informative to read the examples below than the code.

optionalFu =
 Function[Null,
  ReplacePart[
   Function @@ Hold[
     Null,
     Quiet[## &, {Function::slotn}],
     HoldAll
     ]
   ,
   {2, 1, 0} -> Function @@ Hold[##]
   ], HoldAll]

optFuWithDefaults = 
 Function[{functionBody, slotDefaultRules, nSlots},
  Module[
   {fuToken, argsHeld},
   argsHeld = Hold @@ Slot /@ Range[nSlots] /. slotDefaultRules;
   ReplaceAll[
    Function[Null,
     (Function @@ {
         Null,
         fuToken[##],
         HoldAll
         }) @@ argsHeld, HoldAll]
    ,
    Join[
     {fuToken -> optionalFu @@ Hold @@ Unevaluated@functionBody},
     OwnValues@argsHeld
     ]
    ]
   ], HoldAll]

There are three arguments in this last function: {functionBody, slotDefaultRules, nSlots}.

functionBody is really a list (or you can use any head) of arguments that you would normally pass to Function. So if we set functionBody -> {{#, #2}} then this corresponds to {#, #2}&, while functionBody -> {Null, {#, Hold[#2]}, HoldAll} corresponds to Function[Null,{#, Hold[#2]}, HoldAll].

slotDefaultRules is a list of rules that sets default values for slots, if they are not present.

nSlots is the number of slots that are present in functionBody. I have chosen not to infer this from functionBody, because there may be functions inside functionBody and I don't want to mess with those. Instead, the user has to specify nSlots.

Simple example

fu1 = optFuWithDefaults[{{#, #2}}, {#2 -> 3}, 2];
fu1[1]
fu1[1, 2]
{1,3}
{1,2}

To quite some extent, functions in the functionBody argument are preserved.

fu2 = optFuWithDefaults[{{#,#2, {#,#2+5}&[#2,5]}}, {#2-> 3},2];
fu2[1]
fu2[1,2]
{1,3,{3,10}}
{1,2,{2,10}}

It also works with HoldAll and RuleDelayed

fu3 = optFuWithDefaults[
         {Null, {#, Hold[#2]}, HoldAll}, 
         {#2 :> Print[hello]}, 2];
fu3[1]
fu3[1, Print[goodbye]]
{1,Hold[Print[hello]]}
{1,Hold[Print[goodbye]]}

When using Rule and when not using HoldAll, arguments are also evaluated at appropriate times

fu4 = optFuWithDefaults[{{#, Hold[#2]}}, {#2 -> 2 + 2}, 2];
fu4[1]
fu4[1, 5 + 4]
{1,Hold[4]}
{1,Hold[9]}

As mentioned, you can use any head in the first argument (functionBody). This means you can also use Function and get a bit of syntax highlighting (but only in the first argument)

fu5 = optFuWithDefaults[
   {#, #2} &, {#2 -> 3}, 2];
fu5[1]
fu5[1, 2]