# How to write a function in Asymptote

I have a code that draws a couple of rings on a plane and I would like to be able to define a function, taking the radius, center and color as an argument to be able to draw a substantial number of rings without the code repetition. Is this possible in Asymptote?

The code is something like:

``````settings.outformat = "png";
settings.render=16;
size(345.0pt,0);
import graph3;
currentprojection = perspective(30*dir(0,90));
real r1=5, r2=3, r0=0.1;
int nu = 36, nv = 36;

pen colorFunctionred(int u, real theta) {
real z = sin(u/nu * 2pi);
real t = (z + 1) / 2;
return t*red + (1-t)*lightblue;
}
pen colorFunctiongreen(int u, real theta) {
real z = sin(u/nu * 2pi);
real t = (z + 1) / 2;
return t*green + (1-t)*lightblue;
}

path3 crossSection = Circle(r=r0, c=(r1,0,0), normal=Y, n= nu);
path3 crossSection2 = Circle(r=r0, c=(r2,0,0), normal=Y, n= nu);

surface torus = surface(crossSection, c=(0,0,0), axis=Z, n=nv,
angle1=0, angle2=360, color=colorFunctionred);
surface torus2 = surface(crossSection2, c=(1,0,1), axis=Z, n=nv,
angle1=0, angle2=360, color=colorFunctiongreen);

draw(torus);
draw(torus2);
``````

and produces

Look up the word `functions` in the asymptote manual index to find some simple examples. Here is a simple function called `drawcircle` that draws a circle. Note that default arguments may be defined. In the example, if a pen is not defined, then black is used.

``````unitsize(1cm);

void drawcircle(pair center, real radius, pen p = black)
{
}

drawcircle((0,0), 1, 3+red);
drawcircle((1,1), 1, blue);
drawcircle((0.5,0.5), 0.5);
``````