# Returning (not displaying) graphics from a Module

I want to create a Module which puts several parametric surfaces together as one object. For example, say I want to create a module which returns a cylinder that has a top, bottom, and cylindrical "middle" whose dimensions are based on some given values. I tried the following:

cyl[x0_, x1_] := Module[{r, mid, left, right, cylinder},
mid = (x0 + x1) / 2;
r = Sqrt[1 - mid^2];
left =
ParametricPlot3D[{x0, u, v}, {u, -r, r}, {v, -Sqrt[1 - u^2],
Sqrt[1 - u^2]}];
right =
ParametricPlot3D[{x1, u, v}, {u, -r, r}, {v, -Sqrt[1 - u^2],
Sqrt[1 - u^2]}];
cylinder =
ParametricPlot3D[{u, r*Cos[v], r*Sin[v]}, {u, x0, x1}, {v, 0,
2 Pi}];
Graphics3D[{left, cylinder, right}]
];


So, what I'm doing is making 'left' a 'cylinder' and a 'right', and I want to stick them all together. I do not want to instantly display them, however. I want to be able to use a Table to call this cyl with several different parameters and have them all displayed together.

Try changing your last line.

cyl[x0_, x1_] := Module[{r, mid, left, right, cylinder},
mid = (x0 + x1)/2;
r = Sqrt[1 - mid^2];
left =
ParametricPlot3D[{x0, u, v}, {u, -r, r},
{v, -Sqrt[1 - u^2], Sqrt[1 - u^2]}];
right =
ParametricPlot3D[{x1, u, v}, {u, -r, r},
{v, -Sqrt[1 - u^2], Sqrt[1 - u^2]}];
cylinder =
ParametricPlot3D[{u, r*Cos[v], r*Sin[v]},
{u, x0, x1}, {v, 0, 2 Pi}];
Normal@*First /@ {left, right, cylinder}]


e.g.

Graphics3D[{cyl[0.12, 0.3], cyl[0.002, 0.5]}]


• When I try that I get "SetDelayed::write: Tag Graphics3D in ()[x0_,x1_] is Protected." followed by an output of "\$Failed". – Chris Johnson Aug 28 at 20:12
• Perhaps you included the example call in the Module? – Chris Degnen Aug 28 at 20:25
• No; I get the same error message when I copy/paste your code. – Chris Johnson Aug 28 at 20:39
• @ChrisJohnson - try running ClearAll[cyl] to remove old definitions before adding new definitions – Jason B. Aug 28 at 20:49
• @JasonB That did it, thanks! – Chris Johnson Aug 28 at 21:20