首页 > 代码库 > 转 ABAP中USING与CHANGING的用法

转 ABAP中USING与CHANGING的用法

 

 

  ABAP中FORM参数的传递有以下几种方式。
  1.引用传递(CALL BY REFERENCE)
  传递参数时将参数的地址(ADDRESS)传至子程序中,也就是子程序中的参数变量与外部程序的参数变量共享地址内的值。又叫CALL BY ADDRESS,若子程序中的参数变量的值发生了改变,那么,外部程序的实际变量的值也发生改变。
  [Syntax]
  FORM <subform> [USING <f1> <f2>...]  [CHANGING <fr1> <fr2>...]  
  PERFORM <subform> [USING <f1> <f2>...]  [CHANGING <fr1> <fr2>...]
  USING 之后跟随着不会发生变化的子程序参数,CHANGING后面的参数按引用传递,值会随着子程序内的变化而变化。
  [FOR EXAMPLE]

  DATA: i_num1 TYPE VALUE 10,
        i_num2 TYPE VALUE 20,
        i_num3 TYPE I.

  WRITE: /   ‘I_NUM1=‘,i_num1,
             ‘I_NUM2=‘,i_num2,
             ‘I_NUM3=‘,i_num3.
  

  PERFORM CALCULATOR USING i_num1 i_num2 CHANGING i_num3.
   WRITE: / ‘I_NUM1=‘,i_num1,
            ‘I_NUM2=‘,i_num2,
            ‘I_NUM3=‘,i_num3.

  FORM CALCULATOR USING NUM1 NUM2 CHANGING NUM3.
    NUM3 = NUM1 + NUM2.
    WRITE: / ‘NUM1=‘,NUM1,
              ‘NUM2=‘,NUM2,
              ‘NUM3=‘,NUM3.

    "NUM1 = NUM1 + 10.  "不允许修改
     "NUM2 = NUM2 + 12.  "不允许修改

  ENDFORM.
  程序运行结果如下,i_num3变量在调用子程序后值变为了30.
  

  2.值传递(CALL BY VALUE)
  
值传递就是将参数的值复制一份到另一个至内存地址中,所以在子程序中参数变量的值的改变不影响外部程序实际变量的值。
  [Syntax]
  FORM <subform> [USING VALUE(f1) VALUE(f2)...]...
  PERFORM <subform> [USING VALUE(f1) VALUE(f2)...]...
  VALUE(f1) VALUE(f2)是按值传递的CALL BY VALUE
  [For Example]
  DATA: i_num1 TYPE VALUE 10,
        i_num2 TYPE VALUE 20,
        i_num3 TYPE I.

  WRITE: /   ‘I_NUM1=‘,i_num1,
             ‘I_NUM2=‘,i_num2,
             ‘I_NUM3=‘,i_num3.

  

  PERFORM CALCULATOR_TWO USING i_num1 i_num2 CHANGING i_num3.
   WRITE: / ‘I_NUM1=‘,i_num1,
            ‘I_NUM2=‘,i_num2,
            ‘I_NUM3=‘,i_num3.
  

  *值传递 USING VALUE(f1)
  FORM CALCULATOR_TWO USING VALUE(NUM1) VALUE(NUM2) CHANGING SUM.
    SUM = NUM1 + NUM2.
    NUM1 = NUM1 * NUM2.
    NUM2 = NUM1 * NUM2.
    WRITE: / ‘NUM1=‘,NUM1,
             ‘NUM2=‘,NUM2,
             ‘SUM=‘,SUM.
  ENDFORM.
  程序运行如下图,子程序内部参数变量的值发生变量,不影响外部程序实际变量的值。
  

   当只按USING的方式传递参数时其也是按引用的方式传递,共享内存地址。如下:

  DATA: i_num1 TYPE VALUE 10,
        i_num2 TYPE VALUE 20.

  WRITE: / ‘I_NUM1=‘,i_num1 LEFT-JUSTIFIED,
           ‘I_NUM2=‘,i_num2 LEFT-JUSTIFIED.

  PERFORM CALCULATOR USING i_num1 i_num2.
   WRITE: / ‘I_NUM1=‘,i_num1 LEFT-JUSTIFIED,
            ‘I_NUM2=‘,i_num2 LEFT-JUSTIFIED.

     FORM CALCULATOR USING NUM1 NUM2.
      WRITE: / ‘  NUM1=‘,NUM1 LEFT-JUSTIFIED,
            ‘  NUM2=‘,NUM2 LEFT-JUSTIFIED.

     NUM1 = NUM1 + 10.  
     NUM2 = NUM2 + 12.  
  ENDFORM.
   运行结果很明显,如下图:
  

  3)值传递参数并返回最终值(CALL BY VALUE AND RETURN RESULT)
  
传递参数的方式同值传递相同,但在子程序结束执行是会将传入参数变量的最终值回传一份给外部程序变量。
  [Syntax]
  FORM <subform> [.....] [CHANGING VALUE(f1)...]
  PERFORM <subform> [.....] [CHANGING VALUE(f1)...]
  CHANGING VALUE(f1)首先按CALL BY VALUE而后RETURN RESULT给外部变量.
  [For Example]
  DATA: i_num1 TYPE VALUE 10,
        i_num2 TYPE VALUE 20,
        i_num3 TYPE I.

  WRITE: /   ‘I_NUM1=‘,i_num1,
             ‘I_NUM2=‘,i_num2,
             ‘I_NUM3=‘,i_num3.
  

  PERFORM CALCULATOR_THREE USING i_num1 i_num2 CHANGING i_num3.
  WRITE: / ‘I_NUM1=‘,i_num1,
           ‘I_NUM2=‘,i_num2,
           ‘I_NUM3=‘,i_num3.
  

  FORM CALCULATOR_THREE USING NUM1 NUM2 CHANGING VALUE(TOTAL).
    TOTAL = NUM1 + NUM2.
    WRITE: / ‘NUM1=‘,NUM1,
             ‘NUM2=‘,NUM2,
             ‘TOTAL=‘,TOTAL,
             ‘I_NUM3=‘,i_num3.
"原值没有发生改变
  ENDFORM.
  程序运行结果如下,i_num3的值在子程序中并没有发生变量,当子程序调用结束后将把相应的值赋予。