Procedure Definition

A procedure is a program unit containing one or more IDL. This unit executes independently of its caller. It has its own local variables and execution environment. Once a procedure has been defined, references to it cause the program unit to be executed.

The general format of a procedure definition is as follows:

PRO Name, Parameter1, ..., Parametern

Statement1

Statement2

...
END

Example
To define a procedure called AVERAGE, which returns the average value of an array, use the following statements:

PRO AVERAGE, arr,avg
avg = TOTAL(arr)/N_ELEMENTS(arr)
END

The procedure should be stored in a file named average.pro and the file should be in the IDL path. Once the procedure AVERAGE has been created and stored as a file, it is executed by entering the name followed by its arguments separated by commas. Assuming the variable X contains an array, the statement,

AVERAGE, X^2, value
PRINT, value

squares the array X, passes this result to the AVERAGE procedure. The second parameter will contain the value. This is determined and set within the procedure. The result can then be printed. Note that the names of the parameters in the call and in the definition do not have to be the same. Parameters passed to procedures and functions are identified by their position or by a keyword.