# Pass function call (including parameters) as parameter to user defined function

**URL:** <https://community.wolfram.com/t/pass-function-call-including-parameters-as-parameter-to-user-defined-function/25571>\
**Category:** Questions\
**Created:** [June 15, 2025, 5:06am UTC](https://community.wolfram.com/t/pass-function-call-including-parameters-as-parameter-to-user-defined-function/25571 "2025-06-15T05:06:43Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![rschenn](https://avatars.discourse-cdn.com/v4/letter/r/f475e1/32.png) [@rschenn](https://community.wolfram.com/u/rschenn)\
**Post date:** [June 15, 2025, 5:06am UTC](https://community.wolfram.com/t/pass-function-call-including-parameters-as-parameter-to-user-defined-function/25571/1 "2025-06-15T05:06:43Z")

</div>

How can i write a function that i can call like this?

```
(* will have some postprocessing of graphics created by plot later... *)

MyPlotWrapper[Plot[Sin[x], {x,0,Pi}]]

```

So with each MyPlotWrapper call i want to be able to specify the function that is used to plot (Plot, Plot3D…) and also the function that should be plotted (including all the additional parameters to specify range, lengends… which may be of arbitrary number).

---

<div class="post-metadata">

**Author:** ![rschenn](https://avatars.discourse-cdn.com/v4/letter/r/f475e1/32.png) [@rschenn](https://community.wolfram.com/u/rschenn)\
**Post date:** [June 15, 2025, 5:57am UTC](https://community.wolfram.com/t/pass-function-call-including-parameters-as-parameter-to-user-defined-function/25571/2 "2025-06-15T05:57:56Z")

</div>

Calling it like this would also be fine for me if this is easier to achieve

```
MyPlotWrapper[Plot, {Sin[x], {x,0,Pi}, PlotLegends->"Expressions"}]

```

---

<div class="post-metadata">

**Author:** ![eric3](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/eric3/32/12760_2.png) [@eric3](https://community.wolfram.com/u/eric3)\
**Post date:** [June 15, 2025, 1:28pm UTC](https://community.wolfram.com/t/pass-function-call-including-parameters-as-parameter-to-user-defined-function/25571/3 "2025-06-15T13:28:41Z")

</div>

There are many ways, but without knowing what you’re trying to do, it’s difficult to give complete and precise guidance. Here is just one example that will hopefully get you started.

```
SetAttributes[MyPlotWrapper, HoldFirst];
MyPlotWrapper[Plot[fns_List, range_, options___]] := 
 ToString[
  StringForm[
    "I will plot `` over `` with `` options", 
    Head /@ fns, Rest@range, Length[{options}]]]

```

Demonstration:

```
MyPlotWrapper[Plot[{Sin[x], Cos[x]}, {x, 0, Pi}, PlotLegends -> "Expressions"]]
(* "I will plot {Sin, Cos} over {0, Pi} with 1 options" *)

```

---

<div class="post-metadata">

**Author:** ![rschenn](https://avatars.discourse-cdn.com/v4/letter/r/f475e1/32.png) [@rschenn](https://community.wolfram.com/u/rschenn)\
**Post date:** [June 15, 2025, 3:06pm UTC](https://community.wolfram.com/t/pass-function-call-including-parameters-as-parameter-to-user-defined-function/25571/4 "2025-06-15T15:06:22Z")

</div>

Thank you for the answer. I hope that now it is clear how to get the arguments that were supplied to Plot.

But how can i get the (head of) function passed as argument? Using Head[Plot] and adding an underscore like this MyPlotWrapper[Plot\_[fns\_List, range\_, options\_\_\_]] does not seem to work

---

<div class="post-metadata">

**Author:** ![eric3](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/eric3/32/12760_2.png) [@eric3](https://community.wolfram.com/u/eric3)\
**Post date:** [June 15, 2025, 3:18pm UTC](https://community.wolfram.com/t/pass-function-call-including-parameters-as-parameter-to-user-defined-function/25571/5 "2025-06-15T15:18:07Z")

</div>

I assume you’re trying to distinguish between `Plot` and `Plot3D`. One way is to simply overload the wrapper to have two separate definitions.

```
MyPlotWrapper[Plot[fns_List, range_, options___]] := ...
MyPlotWrapper[Plot3D[fns_List, range_, options___]] := ...

```

Another way would be to use a pattern for the head.

```
MyPlotWrapper[p_[fns_List, range_, options___]] :=
 Switch[
  p,
  Plot, "a 2d plot",
  Plot3D, "a 3d plot",
  _, "this is not a plot!"]

```

---

<div class="post-metadata">

**Author:** ![eric3](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/eric3/32/12760_2.png) [@eric3](https://community.wolfram.com/u/eric3)\
**Post date:** [June 15, 2025, 3:21pm UTC](https://community.wolfram.com/t/pass-function-call-including-parameters-as-parameter-to-user-defined-function/25571/6 "2025-06-15T15:21:38Z")

</div>

Also, my example only handles a plot where the first argument is a list (because it made it easier to demonstrate).

And I should say, if you provide more information about your context we can probably come up with a better way. This approach feels a bit risky.

---

<div class="post-metadata">

**Author:** ![rschenn](https://avatars.discourse-cdn.com/v4/letter/r/f475e1/32.png) [@rschenn](https://community.wolfram.com/u/rschenn)\
**Post date:** [June 15, 2025, 4:10pm UTC](https://community.wolfram.com/t/pass-function-call-including-parameters-as-parameter-to-user-defined-function/25571/7 "2025-06-15T16:10:38Z")

</div>

Is there any possibility to use the pattern p\_ and interpret p as a function head and call that function whatever its name name may be (passing it the arguments that were supplied for p)? There are more then two Plot functions and at the end i may also want to call other (non plotting) functions this way. Because of that i would prefer to avoid a switch with a limited number of cases. p\_ may be any function that may be passed as argument to Export[] to create an image of it.

---

<div class="post-metadata">

**Author:** ![eric3](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/eric3/32/12760_2.png) [@eric3](https://community.wolfram.com/u/eric3)\
**Post date:** [June 15, 2025, 5:32pm UTC](https://community.wolfram.com/t/pass-function-call-including-parameters-as-parameter-to-user-defined-function/25571/8 "2025-06-15T17:32:04Z")

</div>

Sure! But it would really help to know what you want to do with these plot expressions in your function. Each plot function could have different options or different argument patterns, so if you’re inspecting the expression inside your function, you probably can’t avoid some special-casing.

Anyway, the function `MyPlotWrapper` that I showed you has “knowledge” of all the parts of the plot expression, so you can just assemble the pieces together when you’re ready to execute the plot. Or, you can just name the whole argument pattern, and avoid having to reassemble the pieces.

```
SetAttributes[MyPlotWrapper, HoldFirst];
MyPlotWrapper[p_[fns_, range_, options___]] :=
 Column[
   {"head: " <> ToString[p], 
    "function(s): " <> ToString[fns], 
    "range: " <> ToString[range], 
    "options: " <> ToString[{options}], 
    Row[{"the plot", p[fns, range, options]}]}]

```

Or

```
SetAttributes[MyPlotWrapper, HoldFirst];
MyPlotWrapper[expr : p_[fns_, range_, options___]] :=
  Column[
    {"head: " <> ToString[p], 
     "function(s): " <> ToString[fns], 
     "range: " <> ToString[range], 
     "options: " <> ToString[{options}], 
     Row[{"the plot", expr}]}]

```

---

<div class="post-metadata">

**Author:** ![gianlucagorni](https://avatars.discourse-cdn.com/v4/letter/g/ec9cab/32.png) [@gianlucagorni](https://community.wolfram.com/u/gianlucagorni)\
**Post date:** [June 15, 2025, 5:33pm UTC](https://community.wolfram.com/t/pass-function-call-including-parameters-as-parameter-to-user-defined-function/25571/9 "2025-06-15T17:33:27Z")

</div>

Have you tried

```
Hold[Plot[Sin[x], {x, 0, Pi}]]

```

---

<div class="post-metadata">

**Author:** ![rschenn](https://avatars.discourse-cdn.com/v4/letter/r/f475e1/32.png) [@rschenn](https://community.wolfram.com/u/rschenn)\
**Post date:** [June 16, 2025, 5:05am UTC](https://community.wolfram.com/t/pass-function-call-including-parameters-as-parameter-to-user-defined-function/25571/10 "2025-06-16T05:05:57Z")

</div>

Thanks, this helped get me started. Even when i have a valid personal Mathematica license i also use the free Wolfram engine and wanted to get limited Plot support without requiring to fiddle with Jupyter or other alternatives of similar complexity and size.

At the moment i have this which is not finished yet but a good point to start:

(_do not evaluate the first argument of ExpGraphShow_)  
SetAttributes[ExpGraphShow, HoldFirst];

(\*export graphics to temp. file and open the GIF image in MS paint   
the parameter can be a call to some Plot function   
ExpGraphShow2[Plot[{Sin[x],Cos[x]},{x,0,2Pi},PlotLegends-\>“Expressions”]] _)  
ExpGraphShow2[head\_[func\_, rng\_, opt\_\_\_]] :=  
Module[{},  
(concat directory path for GIF files)  
t = $HomeDirectory \<\> “\Documents\Mathe\Mathematica\Tmp\”;  
(get date string in custom format)  
d = DateString[{“Year”, “Month”, “Day”, “Hour”, “Minute”, “Second”,“Millisecond”}];  
(concat full path of GIF file)  
f = t \<\> d \<\> “.gif”;  
(without FileNames[] the files in t directory are not deleted)  
DeleteFile[FileNames[t \<\> "_.gif"]];  
(_export graphics to GIF file_)  
Export[f, head[func, rng, opt]];  
(\*run MS paint to open the GIF file \*)  
Run[“start /b mspaint”, f]];
