Default

Default can be used with one two or three arguments, and each form is discussed below.

Default[f]

In the next cell (f) has the default value 0.  Depending on the pattern used f[e] can be treated as f[e,0] or  f[0,e].  If the second argument in the pattern is optional f[e] is treated as f[e,0].  If the first argument in the pattern is optional f[e] is treated as f[0,e].  All throughout this discussion on Default details the functions that are given default values will have no attributes. However, the values assigned to Default[f] have a different effect when (f) has the OneIdentity attribute.  This is covered in the discussion of OneIdentity.

ClearAll[f, g, e] ; Default[f] = 0 ; f[e]/.f[a_, b_.] g[a, b]

g[e, 0]

f[e]/.f[a_., b_] g[a, b]

g[0, e]

Default[f,i]

In the cell below values are given for Default[f,2],  Default[f,3] and definitions for evaluating (f) are given.

ClearAll[f] ; Default[f, 2] = 4 ; Default[f, 3] = 1/8 ; f[a_Integer, b_., c_] := {a, b, c} f[a_Rational, b_Rational, c_.] := {a, b, c}

The definitions for (f) above are used in the input cells below.  For the cells below the default values are not needed because the definitions for (f) could be used directly.

f[10, 11, 12]

{10, 11, 12}

f[1/2, 1/2, 1/2]

{1/2, 1/2, 1/2}

Now for the next input (f) has only two arguments, and the definition for integer arguments is used.  However, the definition can only be used if a default value is inserted for the optional argument.  The applicable definition indicates that the second argument is optional.  The default value for the second argument of (f) is 4 so the value 4 is used for the second argument.

f[10, 11]

{10, 4, 11}

Then another input is evaluated where (f) has three arguments,and the definition for rational arguments is used.Again the definition can only be used if a default value is inserted for the optional argument.The applicable definition indicates the third argument is optional.The default value for the third argument of (f) is 1/8 so the value 1/8 is used for the third argument.

f[1/2, 1/2]

{1/2, 1/2, 1/8}

In the next cell an attempt to make a rule for (f) with the first argument optional is not permitted.  That's because (f) only has default values for the second and third arguments.

f[a_., b_Real, c_Real] := {a, b, c}

Default[f,i,n]

We can also use Default[f,i,n] to specify the default value to use when argument (i) out of (n) arguments is optional.  New default values and definitions for (f) are given below to demonstrate this.  The input below uses definitions for (f) with integer arguments and rational arguments.  

ClearAll[f] ; Default[f, 2, 3] = 8 ; Default[f, 2, 4] = 1/16 ; f[a_Integer, b_., c_Integer] := {a, b, c} f[a_Rational, b_., c_Rational, d_Rational] := {a, b, c, d}

In each case below the use of a default values were not needed because the definitions for (f) could be used directly.

f[11, 12, 13]

{11, 12, 13}

f[1/2, 1/2, 1/2, 1/2]

{1/2, 1/2, 1/2, 1/2}

Now in the next input (f) has two integer arguments.  The definition can only be used when a default value is inserted for the optional (second) argument.  Once a default value is inserted for the optional argument we will have three arguments.  The default value for the second of three arguments in (f) is 8 so a value of 8 is inserted as the second argument.

f[11, 12]

{11, 8, 12}

Then for another input (f) has three rational arguments.  Again the definition for rational arguments can only be used if a default value is inserted for the optional (second) argument.  Once a value is inserted for the second argument we will have four arguments.  The default value for the second of four arguments for (f) is 1/16 so a value of 1/16 is inserted as the second argument.

f[1/2, 1/2, 1/2]

{1/2, 1/16, 1/2, 1/2}

In the next cell we can't give a rule where the second of two arguments is optional.  That is not permitted because we don't have a value assigned to Default[f, 2]  or  Default[f, 2, 2].

f[a_, b_.] := {a, b}

Pattern :: nodef : No default setting found for f in position 2 when length is 2 .

Pattern :: nodef : No default setting found for f in position 2 when length is 2 .


Created by Mathematica  (May 16, 2004)

Back to Ted’s Tricks index page