Section 7 of 11

Modularization Techniques

Subroutines, function modules and methods, how parameters are passed, and where BAPIs fit.

Open contents

ABAP offers several ways to break processing into reusable units, reflecting its long history. They suit different purposes.

Three mechanisms

MechanismDefined inReuse scopeRecommended?
Subroutine (FORM)A programWithin the programNo โ€” legacy
Function moduleA function groupSystem-wideDepends on purpose
MethodA classSystem-wideYes

Function modules

Parameter kinds
KindDirectionNote
IMPORTInValues the function receives
EXPORTOutValues the function returns
CHANGINGIn and outThe caller variable is modified
TABLESIn and outInternal tables, an older style
EXCEPTIONSErrorSurfaced to the caller as sy-subrc
Calling a function module
CALL FUNCTION 'Z_CALCULATE_DISCOUNT'
  EXPORTING
    iv_amount     = lv_amount
  IMPORTING
    ev_discount   = lv_discount
  EXCEPTIONS
    invalid_input = 1
    OTHERS        = 2.

IF sy-subrc <> 0.
ENDIF.

RFC-enabled functions and BAPIs

Common BAPIs
BAPIPurpose
BAPI_MATERIAL_SAVEDATACreate or change a material
BAPI_SALESORDER_CREATEFROMDAT2Create a sales order
BAPI_PO_CREATE1Create a purchase order
BAPI_ACC_DOCUMENT_POSTPost an accounting document
BAPI_GOODSMVT_CREATEPost a goods movement
BAPI_TRANSACTION_COMMITCommit; required after any BAPI

Methods

A method with RETURNING
CLASS lcl_calculator DEFINITION.
  PUBLIC SECTION.
    METHODS calculate_total
      IMPORTING it_items        TYPE ty_items
      RETURNING VALUE(rv_total) TYPE i.
ENDCLASS.

DATA(lo_calc) = NEW lcl_calculator( ).
DATA(lv_total) = lo_calc->calculate_total( lt_items ).

A method with a RETURNING parameter can be used inside an expression, which removes temporary variables. Only one is allowed, and that constraint encourages methods that do one thing.

๐Ÿ“– Unfamiliar term? Look it up in the SAP glossary.

Check your understanding

Test what you just read.

Quiz 1

Which modularization technique is deprecated for new ABAP development?

Quiz 2

BAPIs are implemented as function modules.

Quiz 3

Which function module parameter receives data from the caller?

Quiz 4

What mechanism allows external systems to call SAP function modules?

Quiz 5

Function modules can exist independently without belonging to a function group.

Quiz 6

Arrange the steps for calling a function module in order.

Click items in the correct order