Optional and Default Arguments
Sometimes you may want to set up functions where certain arguments, if omitted, are given "default values". The pattern x_:v stands for an object that can be omitted, and if so, will be replaced by the default value v.
|x_:v||an expression which, if omitted, is taken to have default value v|
|x_h:v||an expression with head h and default value v|
|x_.||an expression with a built‐in default value|
Some common Wolfram Language functions have built‐in default values for their arguments. In such cases, you need not explicitly give the default value in x_:v, but instead you can use the more convenient notation x_. in which a built‐in default value is assumed.
Because Plus is a flat function, a pattern such as x_+y_ can match a sum with any number of terms. This pattern cannot, however, match a single term such as a. However, the pattern x_+y_. contains an optional piece, and can match either an explicit sum of terms in which both x_ and y_ appear, or a single term x_, with y taken to be 0.
Using constructs such as x_., you can easily construct single patterns that match expressions with several different structures. This is particularly useful when you want to match several mathematically equal forms that do not have the same structure.
Sometimes it is convenient not to assign a default value to an optional argument; such arguments can be specified with the help of PatternSequence.
|p|PatternSequence||optional pattern p with no default value|