Use options in user-defined functions.

Define a functions options

Define the options for a function named shout, giving the options default values:

Define the function, including OptionsPattern[] as the last argument

Begin the definition of the shout function, giving OptionsPattern[] as the last argument. OptionsPattern[] indicates that options can occur in that position:

Refer to option values in the function body

Complete the function definition, referring to option values in the body of the function with OptionValue:

  • OptionValue may be used anywhere in the body of a function.

Use the functions options

Invoking the function without options uses the default values of the options:

In[3]:=
Click for copyable input
Out[3]=

Give options to change the defaults:

In[4]:=
Click for copyable input
Out[4]=