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.

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

  2. Main Program

    
             ; MAIN PROGRAM: get_dim1.pro 
             s = size(a_in)
             if (s[0] eq 0) then d1 = 0 else d1 = s[1]
             end
    
  3. 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
                
    
  4. 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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值