Program Examples
To illustrate some of the differences between main programs, procedures, and functions, here are three corresponding versions of the code for determining the length of the first dimension of a given variable (scalar or array) using the built-in SIZE function. Each version is assumed to be stored in a file named get_dim1.pro in your IDL path.
- [Refer to the definition of the SIZE function in the IDL help files. Note that the form of its output -- the intermediate variable s in the examples -- depends on the number of dimensions in the input variable. Its first element contains the number of dimensions (zero for a scalar); its second element is the length of the first dimension. Note that any text after a semi-colon ( ;) is optional and is ignored by the compiler.]
- Main Program
; MAIN PROGRAM: get_dim1.pro s = size(a_in) if (s[0] eq 0) then d1 = 0 else d1 = s[1] end
- Procedure
PRO get_dim1,a_in,d1 s = size(a_in) if (s[0] eq 0) then d1 = 0 else d1 = s[1] return end
- Function
FUNCTION get_dim1,a_in s = size(a_in) if (s[0] eq 0) then d1 = 0 else d1 = s[1] return,d1 end
- Main Program
.run get_dim1 print,d1
- Procedure
get_dim1,a_in,d1 print,d1
- Function
print,get_dim1(a_in)