ABAP offers several ways to break processing into reusable units, reflecting its long history. They suit different purposes.
Three mechanisms
| Mechanism | Defined in | Reuse scope | Recommended? |
|---|---|---|---|
| Subroutine (FORM) | A program | Within the program | No โ legacy |
| Function module | A function group | System-wide | Depends on purpose |
| Method | A class | System-wide | Yes |
Function modules
| Kind | Direction | Note |
|---|---|---|
| IMPORT | In | Values the function receives |
| EXPORT | Out | Values the function returns |
| CHANGING | In and out | The caller variable is modified |
| TABLES | In and out | Internal tables, an older style |
| EXCEPTIONS | Error | Surfaced to the caller as sy-subrc |
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
| BAPI | Purpose |
|---|---|
| BAPI_MATERIAL_SAVEDATA | Create or change a material |
| BAPI_SALESORDER_CREATEFROMDAT2 | Create a sales order |
| BAPI_PO_CREATE1 | Create a purchase order |
| BAPI_ACC_DOCUMENT_POST | Post an accounting document |
| BAPI_GOODSMVT_CREATE | Post a goods movement |
| BAPI_TRANSACTION_COMMIT | Commit; required after any BAPI |
Methods
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.