Direct links to:  procedures  functions

Parameters

Writing IDL Programs

Programs are files that contain IDL statements in a format that enables them to be used in a modular form to build other programs. Programs provide a way to organize the task of developing software and to make the work that is done available for other tasks. The statements within a program must, of course, conform to the syntax of the IDL language. A small number of additional statements are available that enable programs to be referred to by other programs.

A program is only useful if it can communicate with external components. The external components may include other programs and the IDL environment. Through these gateways it may communicate with the user, the operating system and other systems on a network. To understand programs it is important that the basic interaction between them and the IDL environment be understood.

The most basic structure for a program is shown in the diagram below. The user interacts with the IDL environment and the IDL environment interacts with a program called job_one. This program has been written in such a way that all the user has to do is to type the program name at the IDL command line. The IDL environment then finds the program and causes it to be compiled and run. When the program is run it may provide results that are displayed by the IDL environment as text or graphics.

IDL contains many programs that can be referred to directly by the user or by programs that the user writes. Examples that we have used already are HELP and PRINT. We have used PRINT,a to cause the value of variable a to be printed on the screen. PRINT is an example of a program that can be used to cause IDL to interact with the operating system - in this case to cause the text to be displayed on the screen.

We often want a program to receive information so that it modify its actions or cause it to act on that information. This is done with parameters that are defined for each program. The instruction PRINT,a sends the parameter a to the PRINT program so the value can be printed. User programs can be written so they can receive information when they are called.

The two primary types of programs in IDL are called procedure and function. Each is a self-contained set of IDL commands that perform a well-defined task. The only important difference is that a function always returns a value. A procedure is used by giving its name followed by any parameters that it needs. The call to a function uses an assignment to receive its returned value. Its parameters are contained within parentheses.

Procedure Calls
PRINT,a
PLOT,x,y

Function Calls
y=COS(x)
phi=ATAN(y,x)

Parameters

Parameters are the means by which information is exchanged with a program. Parameters may be distinguished by position or by keyword. The method used to refer to each parameter is determined by the definition in the procedure or function. Many programs have both position and keyword parameters. Both kinds of parameters can be used to send information to a program and to receive information from a program. A function also returns a value through the return statement.

An example of a procedure that uses both position and keyword parameters is PLOT. The statement

PLOT, x, y, XRANGE=[-5,5], YRANGE=[0,4]
transfers the horizontal and vertical datapoints to be plotted with the vectors x and y. The range for the horizontal and vertical axes is set by the keywords XRANGE and YRANGE, which are assigned vectors with the values that denote the ranges that are desired on the plot axes. In this case, all of the parameters are used to send information to the plot procedure so it can create the plot.