Function Modules & Function Groups

A function group (SE80/SE37, FUNCTION-POOL) is a program container that holds related function modules and shares global data among them.

13.1 Concepts

A function group (SE80/SE37, FUNCTION-POOL) is a program container that holds related function modules and shares global data among them. Function modules are global procedures with a fixed interface, and they are the only procedures that can be:

called remotely (RFC-enabled), registered as update modules (IN UPDATE TASK), exposed as BAPIs.

13.2 Interface

Input (optional/default possible; pass by value for RFC) Input and output

Internal tables (obsolete except for RFC compatibility) Classic exceptions (or class-based RAISING)

13.3 Example: writing a function module

(In SE37 the interface is maintained in tabs; the source shows it as a comment block. ADT shows the source-based syntax above.)

FUNCTION z_get_customer_orders
  IMPORTING
    VALUE(iv_customer) TYPE kunnr
    VALUE(iv_max_rows) TYPE i DEFAULT 100
  EXPORTING
    VALUE(et_orders)   TYPE ztt_orders
    VALUE(ev_total)    TYPE zorders-amount
  EXCEPTIONS
    not_found.

  SELECT order_id, customer, order_date, amount, currency, status
    FROM zorders
    WHERE customer = @iv_customer
    ORDER BY order_date DESCENDING
    INTO CORRESPONDING FIELDS OF TABLE @et_orders
    UP TO @iv_max_rows ROWS.

  IF sy-subrc <> 0.
    RAISE not_found.
  ENDIF.

  ev_total = REDUCE #( INIT t = CONV zorders-amount( 0 ) FOR o IN et_orders NEXT t = t + o-amount ).
ENDFUNCTION.

13.4 Calling a function module

Note the reversed perspective: the caller *EXPORTS* to the module's *IMPORTING* parameters. Use the Pattern button (SE38) or code completion in ADT to generate the call. The name is a string, so typos are found only at runtime (dump CALL_FUNCTION_NOT_FOUND).

DATA lt_orders TYPE ztt_orders.
DATA lv_total  TYPE zorders-amount.

CALL FUNCTION 'Z_GET_CUSTOMER_ORDERS'
  EXPORTING
    iv_customer = '0000001000'
  IMPORTING
    et_orders   = lt_orders
    ev_total    = lv_total
  EXCEPTIONS
    not_found   = 1
    OTHERS      = 2.

CASE sy-subrc.
  WHEN 0. " ok
  WHEN 1. MESSAGE 'No orders found' TYPE 'I'.
  WHEN OTHERS. MESSAGE ID sy-msgid TYPE 'E' NUMBER sy-msgno WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ENDCASE.

13.5 Commonly used standard function modules

Add/remove leading zeros Next number from a number range GUI_UPLOAD / GUI_DOWNLOAD

Front-end file transfer (prefer CL_GUI_FRONTEND_SERVICES) Classic ALV grid BAPI_TRANSACTION_COMMIT / _ROLLBACK Commit after BAPIs JOB_OPEN / JOB_SUBMIT / JOB_CLOSE Schedule background jobs Legacy e-mail (prefer CL_BCS) Date arithmetic with months/years Validate a date

Many are not released for ABAP Cloud; there you use released classes instead (§36).