ABAP自定义功能函数

1、实现计算器中阶乘函数

FUNCTION zfun_mm_001.
*"----------------------------------------------------------------------
*"*"本地接口:
*"  IMPORTING
*"     REFERENCE(I_I) TYPE  I
*"  EXPORTING
*"     REFERENCE(O_I) TYPE  I
*"  EXCEPTIONS
*"      E_LARGE
*"----------------------------------------------------------------------
DATA t_o TYPE i.
DATA t_i TYPE i.
t_i = i_i.
IF i_i GT 1.
        t_i = t_i - 1.
        CALL FUNCTION 'ZFUN_MM_001'
          EXPORTING
           i_i           = t_i
         IMPORTING
           o_i           = t_o
           .
        TRY.
          o_i = t_o * i_i.
        CATCH cx_sy_arithmetic_overflow.
           o_i = 0.
        ENDTRY.
ELSEIF i_i = 1.
  o_i = 1.
ELSE.
  o_i = 0.
ENDIF.
ENDFUNCTION.

2、实现计算器中累加函数1+2+3+...+n

FUNCTION ZFUN_MM_002.
*"----------------------------------------------------------------------
*"*"本地接口:
*"  IMPORTING
*"     REFERENCE(I_I) TYPE  I
*"  EXPORTING
*"     REFERENCE(O_I) TYPE  I
*"----------------------------------------------------------------------
IF i_i GT 1.
        CALL FUNCTION 'ZFUN_MM_002'
          EXPORTING
           i_i           = i_i - 1
         IMPORTING
           o_i           = o_i
           .
        TRY.
          o_i = o_i + i_i.
        CATCH cx_sy_arithmetic_overflow.
          o_i = 0.
        ENDTRY.
ELSE.
  o_i = 1.
ENDIF.
ENDFUNCTION.

 

posted @ 2019-02-14 11:36  苗士军  阅读(1165)  评论(0编辑  收藏  举报