When you create a function group, the ABAP Workbench automatically generates a main program, global data, and source code. The system uses the formatSAPL<FGRP> to name the main program where<FRGP> is the name of the function group. For each successive function module in the function group, the system automatically creates an include file and assigns it a name using the following format:L<FGRP>U<NN>.
In the function group FGRP, the first function module is stored in the include file LFGRPU01. The subsequent function modules are stored in the include files LFGRPU02, LFGRPU03, LFGRPU04, and so on.
Once you have defined the interface of your function module, you can start writing the source code. In the Object Navigator (transaction SE80), open your function module and choose the Source Code tab page.
The parameters and exceptions of the function module appear as commented lines.
A few features to bear in mind when writing function modules:
Data Handling in Function Modules
Calling Subroutines from Function Modules
You can call various subroutines from function modules.
Triggering Exceptions
Within a function module, you can address all exceptions by using the names you have defined in the interface. Exceptions are handled either by the system or by the calling program. You define this when you call the function by assigning numeric values to the exceptions that you want to handle yourself. For more information, seeCalling Function Modules From Your Programs.
There are two ABAP statements you can use to trigger exceptions:
RAISE <exception>.
MESSAGE..... RAISING <exception>.
The effect of these statements depends on whether you handle the exception in the calling program or let the system process it:
For more information, see the keyword documentation for the MESSAGE statement.
Suppose we have the following function module:
function my_divide
*"-----------------------------------------------
*"*"Local interface:
*" IMPORTING
*" VALUE(Z1) TYPE F
*" VALUE(M1) TYPE F
*" EXPORTING
*" VALUE(RES) TYPE F
*" EXCEPTIONS
*" DIV_ZERO
*"------------------------------------------------
if n1 eq 0.
raise div_zero.
else.
res = z1 / n1.
endif.
endfunction.
IfN1 is not equal to zero, the module dividesZ1 byN1. Otherwise, it triggers the exception DIV_ZERO.
The MDTEST program calls the MY_DIVIDE function:
report mdtest.
data: result type f.
call function 'MY_DIVIDE'
exporting
z1 = 6
n1 = 4
importing
res = result
exceptions
div_zero = 1
others = 2.
if sy-sybrc eq 0.
write: / 'Result =' , result.
else.
write 'Division by zero'.
endif.
When you run the program, the output is:Result = 1,500000
If you replaceN1 = 4 withN1 = 0 in theEXPORTING list, the program MDTEST processes the exceptionDIV_ZERO by assigning the value 1 toSY-SUBRC. The output is: Division by zero.
You can also use exception classes to raise exceptions. For that purpose, you must have selected the Exception classes indicator.
function fb_exception_test.
*"---------------------------------------------------------
*"*"Local Interface:
*" IMPORTING
*" REFERENCE(P1) TYPE I
*" REFERENCE(P2) TYPE I
*" EXPORTING
*" REFERENCE(P3) TYPE I
*" VALUE(P4) TYPE I
*" RAISING
*" CX_SY_ZERODIVIDE
*"---------------------------------------------------------
if p2 = 0.
raise exception type cx_sy_zerodivide.
else.
p3 = p1 / p2.
endif.
endfunction.