Parameters are passed to IDL system and user-written procedures and functions by value or by reference. It is important to recognize the distinction between these two methods.

  • Expressions, constants, system variables, and subscripted variable references are passed by value.
  • Variables are passed by reference.

Parameters passed by value can only be inputs to program units. Results cannot be passed back to the caller by these parameters. Parameters passed by reference can convey information in either or both directions. For example, consider the following trivial procedure:

PRO ADD, A, B
   A = A + B
   RETURN
END

This procedure adds its second parameter to the first, returning the result in the first. The call

ADD, A, 4

adds 4 to A and stores the result in variable A. The first parameter is passed by reference and the second parameter, a constant, is passed by value.

The following call does nothing because a value cannot be stored in the constant 4, which was passed by value.

ADD, 4, A

No error message is issued. Similarly, if ARR is an array, the call

ADD, ARR[5], 4

will not achieve the desired effect (adding 4 to element ARR[5]), because subscripted variables are passed by value. The correct, though somewhat awkward, method is as follows:

TEMP = ARR[5]
ADD, TEMP, 4
ARR[5] = TEMP

Parameter Passing with Structures


An entire structure is passed by reference by using the name of the variable containing the structure as a parameter. Changes to the parameter within the procedure are passed back to the calling procedure. Fields within a structure are passed by value. For example, the following statement prints the value of the structure field A.name:

PRINT, A.name

Any reference to a structure with a subscript or tag name is evaluated into an expression, hence A.name is an expression and is passed by value. This works as expected unless the called procedure returns information in the parameter. For example, the call

READ, A.name

does not read into A.name but interprets its parameter as a prompt string. The proper code to read into the field is as follows.

;Copy type and attributes to variable.
B = A.name
 
;Read into a simple variable.
READ, B
 
;Store result into field.
A.name = B