We have discussed What is a Function module and How to call a Function Module in an ABAP program as well as handle exception. In this article we will learn how to create a custom Function module using TCode SE37 Function Builder.
- Execute SE37
- Enter Function module name ZAMARMN_DIVIDE and choose Create.
- Enter Function group ZAMARMN_FG, short text and click Save.
- Click continue if prompted with dialog Function module name is reserved for SAP.
- Choose Import tab to enter importing parameters.
- Choose Export tab to enter exporting parameters
- Choose Exceptions tab and enter Exception and Short text
- Choose Source code and enter code for function module between FUNCTION and ENDFUNCTION statement. Note that the interface defined for the function module reflects in the commented section of source code.
FUNCTION ZAMARMN_DIVIDE .
*"----------------------------------------------------------------------
*"*"Local Interface:
*" IMPORTING
*" REFERENCE(I_NUMBER1) TYPE NUMERIC
*" REFERENCE(I_NUMBER2) TYPE NUMERIC
*" EXPORTING
*" REFERENCE(E_RESULT) TYPE NUMERIC
*" EXCEPTIONS
*" ZERO_DEVIDE
*" OVERFLOW
*"----------------------------------------------------------------------
DATA:
l_num1 TYPE p LENGTH 8 DECIMALS 2,
l_num2 TYPE p LENGTH 8 DECIMALS 2,
l_res TYPE p LENGTH 8 DECIMALS 2.
* move to local variables to ensure arithmetic and precision
IF i_number1 GT '9999999999999.99' OR
i_number1 LT '-9999999999999.99' OR
i_number2 GT '9999999999999.99' OR
i_number2 LT '-9999999999999.99'.
RAISE overflow.
ENDIF.
l_num1 = i_number1.
l_num2 = i_number2.
IF l_num2 = 0.
RAISE zero_devide.
ELSE.
l_res = l_num1 / l_num2.
ENDIF.
* move result to output
e_result = l_res.
ENDFUNCTION.
- Save and Activate the function module
Test the function module
- Function module can be tested as standalone object. Click Execute to test the function module.
- Enter the input parameters value.
- Click Execute to execute the function module and see the result in the Export Parameters value.