Function Definition Statement
FUNCTION function_name, p1, p2, ..., pn
A function may be defined as a program unit containing one or more statements and which returns a value. 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.
Briefly the format of a function definition is, where name can contain up to 31 characters:
FUNCTION name, parameter1,..., parametern
Statement1
Statement2
...
...
RETURN, expression
END
For example, to define a function called AVERAGE that returns the average value of an array:
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.
Functions can take positional and keyword parameters. For more information on parameters and parameter passing, see Positional Parameters and Keyword Parameters and More On Parameters.
For more information on writing functions, see Writing Procedures and Functions.
Automatic Compilation of Functions and Procedures
PV-WAVE will automatically compile and execute a user-written function or procedure when it is first referenced if both of the following conditions are met:
The source code of the function is in the current working directory or in a directory in the search path defined by the system variable !Path. For more information setting the search path, see Modifying Your Environment. For more information on system variables, see System Variables.
The name of the file containing the function is the same as the function name suffixed by .pro
. The file name should be in lowercase letters.
Note: |
User-written functions must be defined before they are referenced, unless they meet the above conditions for automatic compilation. This restriction is necessary in order to distinguish between function calls and subscripted variable references. For more information on compiling functions and procedures, see Executive Commands in the (Undefined variable: pvwave.waveur). |