# Functions with both optional arguments and options [duplicate]

I have the following codes:

plotContour[func_,xRange_:{-2,2},yRange_:{-2,2},opts___]:=
ContourPlot[Re[func[x+I*y]],{x,xRange[[1]],xRange[[2]]},{y,yRange[[1]],yRange[[2]]}, opts]


opts passes the optioanl arguments like ContourStyle, ImageSize to the built-in ContourPlot function. I have provided default argument values to xRange and yRange.

But now if I run the following:

plotContour[func,ContourStyle->Red]


I run into problems because it treats ContourStyle->Red as the arguments xRange, so I receive errors (Limiting value ContourStyle in ... is not a machine-sized real number.)

How should I modify the codes so that it works when I provide optional arguments but skip the arguments with default values?

• I would not thread what is in essence an option (the range to plot over) as a normal function argument in the first place; better use OptionsPattern and friends for that. Aug 18 '16 at 9:34
• I corrected the title. An "option" is something that looks like name -> value. This is different from an "optional argument" with a default value, which is like x_ : 1. Aug 18 '16 at 10:30

As already said in a comment, I would not treat what feels more like an option as an usual argument to the function. Instead you can use OptionsPattern and friends.

For a generic function with both own arguments, and potential arguments supplied to functions used within you can use the following design pattern

Define your own function's options in one of the following fashions

1. with (protected) symbol (offers the advantage of auto-completion)

Protect[xRange, yRange];
Options[myContourPlot] = {xRange -> {-2, 2}, yRange -> {-2, 2}}

2. with Strings

Options[myContourPlot] = {"xRange" -> {-2, 2}, "yRange" -> {-2, 2}}


myContourPlot[func_, opts : OptionsPattern[{myContourPlot, ContourPlot}]] :=
Module[{x1, x2, y1, y2},
{x1, x2} = OptionValue[xRange] ;
{y1, y2} = OptionValue[yRange];

ContourPlot[Re[func[x + I*y]], {x, x1, x2}, {y, y1, y2},
Evaluate@FilterRules[{opts}, Options@ContourPlot]]]


Now you can call your function with

myContourPlot[Exp, xRange -> {-3, 3}, ContourStyle -> Dashed]


or (depending on your choice concerning symbols or strings as option names)

myContourPlot[Exp, "xRange" -> {-3, 3}, ContourStyle -> Dashed]


This is a common problem when we want to allow both options (name -> value) and optional arguments (arguments that take a default value when omitted) in a function.

The usual solution is to make the patterns for the optional arguments specific enough that they will not match any options.

I would do it like this:

plotContour[
func_,
xRange : {_?NumericQ, _?NumericQ} : {-2,2} , yRange : {_?NumericQ, _?NumericQ} : {-2,2},
opt : OptionsPattern[]
] := ...


Things to pay attention to:

1. When we add a name to a generic Pattern, and we also make the corresponding argument optional, we must use two colons like this:

name : pattern : defaultValue

2. Make sure that defaultValue itself will match the pattern (related)

3. Keep in mind that OptionsPattern[] does not only match a sequence of options such as a -> 1, b :> 2, but also a list of options such as {a -> 1, b :> 2}. Thus when making the pattern specific, it is not sufficient to require it to be a list. This is why I used _?NumericQ instead of _.

As suggested in the comment by Sascha, you can use options for the ranges as well as the standard options of ContourPlot, like this:

Options[plotContour] = {xRange -> {-2, 2}, yRange -> {-2, 2}}~Join~Options[ContourPlot];
plotContour[func_, opts : OptionsPattern[]] :=
Block[{x, y},
With[{xr = {x}~Join~OptionValue[xRange],
yr = {y}~Join~OptionValue[yRange]},
ContourPlot[Re[func[x + I*y]], xr, yr,
Evaluate@FilterRules[{opts}, Options[ContourPlot]]]]]


And then for example:

plotContour[#^2 &, ContourStyle -> Red, xRange -> {-1, 1}]

• Maybe add Block[] Aug 18 '16 at 10:11
• @Feyre Block can be dangerous and should best not be used where a simple With is sufficient Aug 18 '16 at 10:15
• @Sascha sure, but that would be a rewrite of the answer. Aug 18 '16 at 10:17