Setting Up Functions with Optional Arguments

When you define a complicated function, you will often want to let some of the arguments of the function be "optional". If you do not give those arguments explicitly, you want them to take on certain "default" values.

Built-in Mathematica functions use two basic methods for dealing with optional arguments. You can choose between the same two methods when you define your own functions in Mathematica.

The first method is to have the meaning of each argument determined by its position, and then to allow one to drop arguments, replacing them by default values. Almost all built-in Mathematica functions that use this method drop arguments from the end. For example, the built-in function Flatten[list, n] allows you to drop the second argument, which is taken to have a default value of Infinity.

You can implement this kind of "positional" argument using patterns.

f[x_,k_:kdef]:=valuea typical definition for a function whose second argument is optional, with default value kdef

Defining a function with positional arguments.

This defines a function with an optional second argument. When the second argument is omitted, it is taken to have the default value Infinity.
In[1]:=
Click for copyable input
Here is a function with two optional arguments.
In[2]:=
Click for copyable input
Mathematica assumes that arguments are dropped from the end. As a result here gives the value of , while has its default value of .
In[3]:=
Click for copyable input
Out[3]=

The second method that built-in Mathematica functions use for dealing with optional arguments is to give explicit names to the optional arguments, and then to allow their values to be given using transformation rules. This method is particularly convenient for functions like Plot which have a very large number of optional parameters, only a few of which usually need to be set in any particular instance.

The typical arrangement is that values for "named" optional arguments can be specified by including the appropriate transformation rules at the end of the arguments to a particular function. Thus, for example, the rule Joined->True, which specifies the setting for the named optional argument Joined, could appear as ListPlot[list, Joined->True].

When you set up named optional arguments for a function f, it is conventional to store the default values of these arguments as a list of transformation rules assigned to Options[f].

f[x_,OptionsPattern[]]:=valuea typical definition for a function with zero or more named optional arguments
OptionValue[name]the value of a named optional argument in the body of the function

Named arguments.

This sets up default values for two named optional arguments and in the function .
In[4]:=
Click for copyable input
Out[4]=
Here is the definition for a function which allows zero or more named optional arguments to be specified.
In[5]:=
Click for copyable input
With no optional arguments specified, the default rule for is used.
In[6]:=
Click for copyable input
Out[6]=
If you explicitly give a rule for , it will override the default rules stored in Options[fn].
In[7]:=
Click for copyable input
Out[7]=
FilterRules[opts,Options[name]]the rules in opts used as options by the function name
FilterRules[opts,Except[Options[name]]]
the rules in opts not used as options by the function name

Filtering options.

Sometimes when you write a function you will want to pass on options to functions that it calls.

Here is a simple function that solves a differential equation numerically and plots its solution.
In[8]:=
Click for copyable input
With no options given, the default options for NDSolve and Plot are used.
In[9]:=
Click for copyable input
Out[9]=
This changes the method used by NDSolve and the color in the plot.
In[10]:=
Click for copyable input
Out[10]=
New to Mathematica? Find your learning path »
Have a question? Ask support »