Parameter Passing with Structures

An entire structure is passed by reference by simply 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 

Storing Into Array Fields

As mentioned previously, the semantics of storing into structure array fields is slightly different than storing into simple arrays. The main difference is that with structures, a subscript range must be used when storing an array into part of an array field. With normal arrays, when storing an array inside part of another array, use the subscript of the lower-left corner, not a range specification. Other differences occur because the size and type of a field are fixed by the original structure definition, and the normal IDL semantics of dynamic binding are not applicable. The rules for storing into array fields are as follows:

VAR.ARRAY_TAG = Scalar_Expression

All elements of VAR.tag are set to Scalar_Expression. For example:

;Set all 12 elements of A.inten to 100. 
A.inten = 100 

VAR.TAG = Array_Expression

Each element of Array_Expression is copied into the array VAR.tag. If Array_Expression contains more elements than the destination array does, an error results. If it contains fewer elements than VAR.TAG, the unmatched elements remain unchanged. For example:

;Set A.inten to the 12 numbers 0, 1, 2,..., 11. 
A.inten = FINDGEN(12) 
 
;Set A.inten[0] to 1 and A.inten[1] to 2. The other elements  
;remain unchanged. 
A.inten = [1, 2] 

VAR.TAG[Subscript] = Scalar_Expression

The value of the scalar expression is simply copied into the designated element of the destination. If Subscript is an array of subscripts, the scalar expression is copied into the designated elements. For example:

;Set the sixth element of A.inten to 100.  
A.inten[5] = 100 
 
;Set elements 2, 4, and 6 to 100. 
A.inten[[2, 4, 6]] = 100 

VAR.TAG[Subscript] = Array_Expression

Unless VAR.tag is an array of structures, the subscript must be an array. Each element of Array_Expression is copied into the element given by the corresponding element subscript. For example:

;Set elements 2, 4, and 6 to the values 5, 7, and 9 respectively. 
A.inten[[2, 4, 6]] = [5, 7, 9] 

VAR.TAG[Subscript_Range] = Scalar_Expression

The value of the scalar expression is stored into each element specified by the subscript range. For example:

;Sets elements 8, 9, 10, and 11 to the value 5. 
A.inten[8:*] = 5 

VAR.TAG[Subscript_Range] = Array_Expression

Each element of the array expression is stored into the element designated by the subscript range. The number of elements in the array expression must agree with the size of the subscript range. For example:

;Sets elements 3, 4, 5, and 6 to the numbers 0, 1, 2, and 3, 
;respectively. 
A.inten[3:6] = FINDGEN(4)