next up previous contents
Next: Another example functions (of Up: Section 2: Introduction to Previous: Section 2: Introduction to   Contents

An example function (of one variable)

For example, you have seen that IDL uses radians. You might want to convert from radians back to degrees. You can do that using,

a_rad=0.5 
a_deg=a_rad*180./!pi
but that's not very neat. If you use this a lot you will have to type a lot and maybe make mistakes.

We can define a small program that does this for us. Open a file called rad_to_deg.pro

Put the lines below in it.

Function rad_to_deg,input_rad

;Function to convert radians to degrees

output_deg=input_rad*180./!pi
return,output_deg

end
Note that the ``;'' symbol is a comment and ignored by IDL and you should always put a comment at the start of a program saying what it does.

We can use our program by for example typing,

.compile rad_to_deg
degrees=rad_to_deg(!pi/2.)
print,degrees
IDL puts ``!pi/2.'' into the function (the input parameter), and converts it to degress and outputs this (the returned variable).



John Marsham 2009-12-07