Function Definition

A function is a program unit containing one or more IDL statements that returns a value. This unit executes independently of its caller. It has its own local variables and execution environment. Once a function has been defined, references to the function cause the program unit to be executed. All functions return a function value which is given as a parameter in the RETURN statement used to exit the function. Function names can be up to 128 characters long.

The general format of a function definition is as follows:

FUNCTION Name, Parameter1, ..., Parametern

Statement1

Statement2

...

...

RETURN, Expression

END

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

FUNCTION AVERAGE, arr
RETURN, TOTAL(arr)/N_ELEMENTS(arr)
END

Once the function AVERAGE has been defined, it is executed by entering the function name followed by its arguments enclosed in parentheses. Assuming the variable X contains an array, the statement,

PRINT, AVERAGE(X^2)

squares the array X, passes this result to the AVERAGE function, and prints the result. Parameters passed to functions are identified by their position or by a keyword.