本文是一个ABAP简单函数编写以及调用的demo,废话不多说直接看demo场景
Demo场景
本次Demo是设计一个函数来进行简单的四则运算,先去SE37创建函数,这里创建的函数叫做ZWYZ_FC_01
函数构建器——导入
函数构建器——导出
函数构建器——例外
函数构建器——源代码
FUNCTION ZWYZ_FC_01. *"---------------------------------------------------------------------- *"*"本地接口: *" IMPORTING *" REFERENCE(I_DATA1) TYPE P *" REFERENCE(I_DATA2) TYPE P *" REFERENCE(I_DATA3) TYPE C *" EXPORTING *" REFERENCE(E_DATA) TYPE P *" EXCEPTIONS *" NO_ZERO *" OPERATION_SYMBOL_ERROR *"---------------------------------------------------------------------- CASE I_DATA3. WHEN '+'. E_DATA = I_DATA1 + I_DATA2. WHEN '-'. E_DATA = I_DATA1 - I_DATA2. WHEN '*'. E_DATA = I_DATA1 * I_DATA2. WHEN '/'. IF I_DATA2 = 0. RAISE NO_ZERO. ELSEIF I_DATA2 <> 0. E_DATA = I_DATA1 / I_DATA2. ENDIF. WHEN OTHERS. RAISE OPERATION_SYMBOL_ERROR. ENDCASE. ENDFUNCTION.
SE38调用函数代码
*&---------------------------------------------------------------------* *& Report ZWYZTEST_03 *&---------------------------------------------------------------------* *& *&---------------------------------------------------------------------* REPORT ZWYZTEST_03 MESSAGE-ID FILAM. PARAMETERS P_1 TYPE P DECIMALS 5. PARAMETERS P_3 TYPE C LENGTH 1 . PARAMETERS P_2 TYPE P DECIMALS 5. DATA:GV_OUTCOME TYPE P DECIMALS 5. CALL FUNCTION 'ZWYZ_FC_01' EXPORTING I_DATA1 = P_1 I_DATA2 = P_2 I_DATA3 = P_3 IMPORTING E_DATA = GV_OUTCOME EXCEPTIONS NO_ZERO = 1 OPERATION_SYMBOL_ERROR = 2 OTHERS = 3 . CASE SY-SUBRC . WHEN'0'. WRITE:/ P_1, P_3, P_2 ,'=' , GV_OUTCOME. MESSAGE S001 WITH '运算成功' . WHEN '1'. MESSAGE S001 WITH '被除数不能为0' DISPLAY LIKE 'E'. WHEN '2'. MESSAGE S001 WITH '运算符号错误' DISPLAY LIKE 'E'. WHEN OTHERS. MESSAGE S001 WITH '运算错误' DISPLAY LIKE 'E'. ENDCASE.