Creating Function Modules 

You can only create function modules and function groups using the Function Builder in the ABAP Workbench. For further information, refer to Creating New Function Modules. This section uses an example to illustrate how a function module is created from the point of view of ABAP programming.

We are going to create a function module READ_SPFLI_INTO_TABLE to read data for a specified airline from table SPFLI into an internal table, which it then passes back to the calling program.

Function Groups and Function Modules

Firstly, we create a new function group DEMO_SPFLI to hold the function module (see Creating a Function Group). Then, we can create the new function module (see Creating a Function Module).

Parameter Interface

You can specify the types of interface parameters in function modules in the same way as the parameter interfaces of subroutines. Since function modules can be used anywhere in the system, their interfaces can only contain references to data types that are declared systemwide. These are the elementary ABAP data types, the systemwide generic types, such as ANY TABLE, and types defined in the ABAP Dictionary. You cannot use LIKE to refer to data types declared in the main program.

The function module READ_SPFLI_INTO_TABLE requires an import parameter to restrict the selection to a single airline. To specify the type, we can refer to the key field CARRID of the database SPFLI:

Under Ref. field/structure, you can enter a column of a database table, a component of a ABAP Dictionary structure, or a whole ABAP Dictionary structure. The import parameter ID is optional, and has a default value.

The following type specification would have the same effect:

Ref. type can contain any generic or full data type that is recognized systemwide. Here, the parameter is defined with reference to the elementary ABAP Dictionary type (or data element) S_CARR_ID. This is the type used to define the field SPFLI-CARRID.

To pass data back to the calling program, the function module needs an export parameter with the type of an internal table. For this, we define a systemwide table type SPFLI_TAB with the line type SPFLI in the ABAP Dictionary.

We can now use this data type to specify the type of the export parameter ITAB:

The internal table is passed by value. Only internal tables that are passed using tables parameters can be passed exclusively by reference.

Exceptions

Our function module needs an exception that it can trigger if there are no entries in table SPFLI that meet the selection criterion. The exception NOT_FOUND serves this function:

Source Code

Having defined the parameter interface and exceptions, we can now write the source code of our function module. To do this, choose Source code in the Function Builder. This opens the ABAP Editor for the include program L<fgrp>U<xx> (see Function Groups ). This is the include that will hold the program code for the function module;

The source code of the function module occurs between the FUNCTION and ENDFUNCTION statements. The definitions of the parameter interface and the exceptions is displayed here in comment lines. Its real coding is generated invisibly by the Function Builder.

Data in Function Modules

You can use the TYPES and DATA statements to create local data types and objects. The interface parameters also behave like local data objects. In addition, you can access all of the global data of the main program. This data is defined in the include program L<fgrp>TOP. To open this include, choose Goto ® Global data. The global data behaves like the instance attributes of a class. The first time you call a function module in a particular function group, the data is loaded into memory. It can then be accessed and changed by all of the function modules in the group. The system retains the values until the next time a function module is called.

Calling Subroutines

You use subroutines for local modularization. Function modules can also use this technique. The function module that they call are defined in the corresponding main program.

If you only want to call a subroutine from a single function module, it is best to define them in the same include program as the function module itself, directly after the ENDFUNCTION statement. These subroutines can be called from all function modules in the function group, but for clarity, they should only be called from the function module that precedes them.

If you want to define a subroutine that will be called from several different function modules, you can define a special include program for it with the name L<fgrp>F<xx>.

Raising Exceptions

There are two ABAP statements for raising exceptions. They can only be used in function modules:

RAISE <except>.

and

MESSAGE..... RAISING <except>.

The effect of these statements depends on whether the calling program handles the exception or not. If the name <except> of the exception or OTHERS occurs in the EXCEPTIONS addition of the CALL FUNCTION statement, the exception is handled by the calling program.

If the calling program does not handle the exception

If the calling program handles the exception, both statements return control to the program. No values are transferred. The MESSAGE ..... RAISING statement does not display a message. Instead, it fills the system fields SY-MSGID, SY-MSGTY, SY-MSGNO, and SY-MSGV1 to SY-MSGV4.

Source Code of READ_SPFLI_INTO_TABLE

The entire source code of READ_SPFLI_INTO_TABLE looks like this:

FUNCTION READ_SPFLI_INTO_TABLE.

*"------------------------------------------------------------
*"*"Local interface:
*"       IMPORTING
*"             VALUE(ID) LIKE  SPFLI-CARRID DEFAULT 'LH '
*"       EXPORTING
*"             VALUE(ITAB) TYPE  SPFLI_TAB
*"       EXCEPTIONS
*"              NOT_FOUND
*"------------------------------------------------------------

  SELECT * FROM SPFLI INTO TABLE ITAB WHERE CARRID = ID.

  IF SY-SUBRC NE 0.
    MESSAGE E007(AT) RAISING NOT_FOUND.
  ENDIF.

ENDFUNCTION.

The function module reads all of the data from the database table SPFLI where the key field CARRID is equal to the import parameter ID and places the entries that it finds into the internal table SPFLI_TAB. If it cannot find any entries, the exception NOT_FOUND is triggered using MESSAGE...RAISING. Otherwise, the table is passed to the caller as an exporting parameter.

Calling READ_SPFLI_INTO_TABLE

The following program calls the function module READ_SPFLI_INTO_TABLE:

REPORT DEMO_FUNCTION_MODULE.

PARAMETERS CARRIER TYPE S_CARR_ID.

DATA: JTAB TYPE SPFLI_TAB,
      WA   LIKE LINE OF JTAB.

CALL FUNCTION 'READ_SPFLI_INTO_TABLE'
     EXPORTING
          ID        = CARRIER
     IMPORTING
          ITAB      = JTAB
     EXCEPTIONS
          NOT_FOUND = 1
          OTHERS    = 2.

CASE SY-SUBRC.
  WHEN 1.
    MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO.
  WHEN 2.
    MESSAGE E702(AT).
ENDCASE.

LOOP AT JTAB INTO WA.
  WRITE: /  WA-CARRID, WA-CONNID, WA-CITYFROM, WA-CITYTO.
ENDLOOP.

The actual parameters CARRIER and JTAB have the same data types as their corresponding interface parameters in the function module. The exception NOT_FOUND is handled in the program. It displays the same message that the function module would have displayed had it handled the error.