Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am new to mathematica ,so this question may seem a little naive.

I know how to use the option as an argument to a function, we can define an option by using Options[]and SetOptions[], but the following usage confused me by passing a function as an option to a function .Here are two examples:


GraphPlot3D[{2 -> 3, 2 -> 4, 2 -> 5, 3 -> 4, 3 -> 5, 4 -> 5}, 
 VertexRenderingFunction -> (Sphere[#, 0.1] &)]


Graph[{1 \[UndirectedEdge] 2, 2 \[UndirectedEdge] 3, 
  3 \[UndirectedEdge] 1}, VertexShapeFunction -> "Square", 
 VertexSize -> 0.2]

My question is:

  1. What is the difference between VertexRenderingFunction -> (Sphere[#, 0.1] &),VertexShapeFunction -> "Diamond" and VertexSize -> 0.2 as examples display.

  2. The function as an option seems to be useful, so what is the right time to use it? How to apply it to the functions defined by myself?

share|improve this question
up vote 15 down vote accepted

Short answer:

You supply a pure function to an option when you want to override the built-in options. In this case, "Diamond" and 0.2 resolve to certain functions or are used as values in certain functions internally which is then used for the respective option. The short names are merely a convenient way for you to remember and enter the option.

Longer answer:

It might help to understand what is done internally, so consider this simple example. We construct a function f that takes in a numeric argument x, and an optional value p (with default value 1) and we raise x to power p:

f[x_?NumericQ, opts : OptionsPattern[p -> 1]] := x^OptionValue[p]

f[2, p -> 2]
f[2, p -> 3]
(* 2, 4, 8 *)

So far so good. Now let's say you'd like to supply arguments like "square" and "cube" instead of 2 and 3 and let the function figure out what to do with it. So you do something like:

f[x_?NumericQ, opts : OptionsPattern[p -> 1]] := Module[{pow},
    pow = OptionValue[p] /. {"square" -> 2, "cube" -> 3};

f[2, p -> "square"]
f[2, p -> "cube"]
(* 4, 8 *)

You have now built a definition for f that can take simple options for p both as "square" or as 2, which gives you additional flexibility.

Extending the idea to a function that accepts functions as option values. I'll define a function g, taking inputs as g[x, p -> func], returning func[x] and also let it take arguments as g[x, p -> "string"] for some values of "string"

g[x_?NumericQ, opts : OptionsPattern[p -> (# &)]] := Module[{func},
    func = OptionValue[p] /. {"square" -> (#^2 &), "cube" -> (#^3 &)};

g[2, p -> "square"]
g[2, p -> "cube"]
(* 4, 8 *)

Suppose you get bored of the vanilla pre-defined options and want to fancy it up, you can do that by supplying your own pure function as an option to p. For example:

Plot[g[x, p -> (Sin[Exp[#]] &)], {x, 0, π}]

enter image description here

By now, you can begin to see how this is useful. If you had a complicated function that needed to be input as an option or is used repeatedly, you'd want to make things simple for the end user (most likely yourself) and so you pre-define it and provide a short form that is easy to remember and conveys the intent without ambiguity. Any time this needs to be overridden (possibly for a one time use case), you can supply your own function.

Also read this answer for why you need to wrap your custom pure functions in parentheses when supplying as an option value.

share|improve this answer
Thanks for your help – withparadox2 Apr 7 '12 at 12:56

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.