Main program, Procedures and Functions

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.]

  1. Main Program

             ; MAIN PROGRAM: get_dim1.pro 
             s = size(a_in)
             if (s[0] eq 0) then d1 = 0 else d1 = s[1]
             end
    
  2. 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
                
    
  3. 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
    
To execute these three routines during an interactive session and to print to the terminal command window the resulting value for the first dimension, the commands would be as follows. In all cases, the input variable  a_in  must have already been defined during the session. Note that you do not have to give a  .run  command to compile a procedure or function as long as its file name matches its logical name (except for the  .pro  extension).

  1. Main Program

              .run get_dim1
              print,d1
    
  2. Procedure

              get_dim1,a_in,d1
              print,d1
    
  3. Function

              print,get_dim1(a_in)
    
Tips

你可能感兴趣的:(Main program, Procedures and Functions)