Entering content frame

Conceptual documentation Understanding Function Module Code Locate the document in its SAP Library structure

The system predefines the organization of objects for a function group and its function modules. When you create a function group, the Workbench automatically generates a main program, global data, and source code. The system uses the format SAPL<fgrp> to name the Main program. The <fgrp> variable is the function group's name.

For each successive function module in a function group, the Workbench automatically creates an include file. You can view this include file by selecting Source Code on the Function Builder initial screen. The system gives the include file a name using the form L<functgrp>U<nn> . For example, in the function group FGRP, the first function module resides in include file LFGRPU01. The subsequent function modules are in include files LFGRPU02, LFGRPU03, LFGRPU04, and so on.

The Main Function Program

The Function Builder generates the main function program includes for you. The system uses the L<functgrp>UXX form to name a main function program. So, for the function group FGRP, the main functions include would be LFGRPUXX .

Writing Function Modules

Once you have defined the interface of your function module, you can start writing the code itself.

On the initial screen of the Function Builder, select Source code. The ABAP Editor appears, in which you can write the code of the function module between the FUNCTION and ENDFUNCTION statements.
The parameters and exceptions of the function module appear in the Editor as commented lines.

This graphic is explained in the accompanying text

 

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 using the names you defined in the interface. Exceptions can be handled either by the system or by the calling program. You decide this when you call the function, by assigning a numeric value to the exceptions that you want to handle yourself. For further information, see Calling Function Modules From Your Programs.

 

Exceptions must be explicitly triggered.
There are two ABAP statements that may only be used in function modules that you can use to trigger exceptions:

Syntax

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.

If the calling program fails to handle the exception, the system triggers a runtime error.

For further information, see the keyword documentation for the MESSAGE statement.

Here is an example for raising exceptions:

Example

Suppose we have the following function module:

This graphic is explained in the accompanying text

If N1 is unequal to zero, it divides Z1 by N1. Otherwise, it triggers the exception DIV_ZERO.

Example: Program MDTEST calls the function MY_DIVIDE:

This graphic is explained in the accompanying text

 

When you run the program, the output looks like this:

Result = 1,500000

If you replace N1 = 4 with N1 = 0 in the EXPORTING list, the program MDTEST processes the exception DIV_ZERO by assigning the value 1 to SY-SUBRC. The output now looks like this:

Division by zero

 

 

 

Leaving content frame