# Function as an option to a function

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:

Example1:

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


Example2:

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?

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.

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
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:

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

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"

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

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, π}] 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.