Function Modules and Function Groups
|
This content was generated with the assistance of AI and should be verified against the official documentation before being relied on in production. |
A function module is a named, reusable piece of ABAP code that any program in the system can call by its name. Function modules
are kept in containers called function groups, and SAP has shipped thousands of them since the first ABAP releases: you meet
them when you read old code, when you call an SAP function such as a number-range or a lock function, and whenever one system
has to call another, because a function module is the unit that a remote function call reaches. After reading this page you
can read the interface of a function module, call it with CALL FUNCTION, handle both of its kinds of exception, tell an
ordinary, a remote-enabled and an update function module apart, test a module in transaction SE37, and decide when a method
is the better choice. The page is written against Standard ABAP release 8.16 and ABAP for Cloud Development release 9.20
(2608). Function modules belong to classic Standard ABAP, on premise and Private Edition. In ABAP Cloud and in the
Public Edition you can only call a function module that SAP has released for it (see
ABAP Language Versions and Released APIs), and
the new code you write there is made of classes.
Before you read
-
Programming Concepts for Newcomers: what a procedure, a parameter and a call are.
-
Classes and Objects: methods, their parameters and visibility, because the page compares them with function modules.
-
Class-Based Exceptions:
TRY,CATCHand exception classes, which a function module can raise as well. -
Classic Tools: SE80, SE38, SE11, SE37 and SE16: the Object Navigator and the Function Builder.
-
Program Types and Structure: what a function group is among the program types.
Key terms
-
Function module: a procedure stored in a function group that any program can call with
CALL FUNCTION. -
Function group: the container program, also called function pool, that holds function modules and the data they share.
-
Parameter interface: the list of parameters and exceptions through which a function module talks to its caller.
-
Classic exception: a name in the interface of a function module that the code raises with
RAISEand the caller tests throughsy-subrc; the opposite of a class-based exception. -
Remote Function Call (RFC): a call of a function module that runs in another system, or in another session of the same system.
-
Update function module: a function module that is registered during a program and run later, in a special work process, when the program says
COMMIT WORK. -
sy-subrc: the system field that holds the return code of the statement that ran last; after
CALL FUNCTIONit holds the number you gave to an exception.
What a function module is
A function module is a procedure: a block of statements with a name, input values, output values and a way to report errors. A
method is a procedure too, but it lives inside a class. A function module lives outside any class, in a function group, and is
identified by one name that is unique in the whole system, for example Z_EXP_READ_CLAIM. A program calls it with
CALL FUNCTION 'Z_EXP_READ_CLAIM'; the call does not mention the function group, because the system finds the module by its name.
The Keyword Documentation describes function modules as the predecessor technology of public methods in global classes. Both are cross-program procedures, but a method has more: classes, objects, inheritance, inline declarations when you call it, and a complete syntax check of every call. The ABAP programming guidelines therefore say to use function modules only where a technical reason demands them: remote function calls, update tasks and the access to classic dynpros or selection screens. Even then, the function module should only wrap a call of a method, so that the real logic is written in a class.
The function group
A function module cannot exist alone. It is part of a function group, which is an ABAP program of the type function pool (see Program Types and Structure). The function group is the unit that is loaded into memory: when a program calls one of its function modules for the first time, the whole group is loaded, and it stays loaded for the rest of the session. A function group holds at most 99 function modules.
When you create a function group in the Object Navigator (SE80), the system generates its pieces by a fixed naming scheme.
For a group named ZEXP_CLAIMS they are:
| Part | Content |
|---|---|
|
The main program. Its name is the prefix |
|
The TOP include. It starts with the statement |
|
The include of all function module includes. The system maintains it; you never edit it. |
|
One include per function module, numbered in the order of creation. The include holds the statements between |
The names of function groups are limited to 26 characters, and customers use names that start with Y or Z. Further includes for declarations, class implementations, event blocks or subroutines follow the same scheme, with the endings D, P, E and F followed by a two-digit number.
The generated main program is only these lines. It requires an SAP system, where the tools generate it; not run here.
*******************************************************************
* System-defined Include-files. *
*******************************************************************
INCLUDE lzexp_claimstop. " Global Declarations
INCLUDE lzexp_claimsuxx. " Function Modules
Source: ABAP Keyword Documentation: FUNCTION-POOL and ABAP Workbench Tools: Creating a Function Group
Because the TOP include is shared, a global variable declared there keeps its value from one call of a function module of the group to the next, as long as the internal session lasts (see Memory Organization and Sessions). Old function groups use this to remember a state between calls. It makes a module behave differently depending on what was called before, so new code passes everything it needs through the parameters.
The parameter interface
The interface of a function module lists what the caller hands in, what it gets back, and what can go wrong. It is defined in the Function Builder (SE37 or SE80) or in the ABAP development tools for Eclipse (ADT), not in the source code. The tools write it into the source as a comment block at the top, which you read but never edit by hand.
| Kind | Meaning |
|---|---|
|
Input values from the caller. The module must not overwrite an input parameter that is passed by reference. A parameter can be optional or have a default value. |
|
Output values for the caller. They are always optional for the caller. |
|
A value that the caller hands in and the module may change and hand back. |
|
Internal tables that go in and out. This kind is obsolete: use an |
|
The class-based exceptions that the module may propagate to its caller. |
|
The names of classic exceptions that the module raises with |
"Importing" and "exporting" are named from the point of view of the function module: what the caller passes with EXPORTING in
the CALL FUNCTION statement arrives in an IMPORTING parameter of the module, and the module’s EXPORTING parameters are what
the caller receives in its own IMPORTING list. This mirror image confuses every newcomer once; the example below shows it.
Each parameter is passed either by reference or by value. By reference, the formal parameter is another name for the caller’s
variable, so a change is visible at once. By value, the module works on a copy, and the result of an EXPORTING or CHANGING
parameter is copied back only if the module ends without an error. Parameters should be typed with TYPE and a type of the ABAP
Dictionary or of a global class, because the interface is public in the whole system; without a type the parameter is generic.
Inside the module the predicate expression IS SUPPLIED tells whether the caller passed an optional parameter.
The comment block that the tools generate for a module with every kind of parameter looks like this. It is a sketch with invented names and requires an SAP system; not run here.
FUNCTION z_exp_summarise_claims.
*"----------------------------------------------------------------------
*"*"Local Interface:
*" IMPORTING
*" REFERENCE(MIN_AMOUNT) TYPE I
*" VALUE(MAX_ITEMS) TYPE I DEFAULT 100
*" REFERENCE(INCLUDE_REJECTED) TYPE ABAP_BOOL OPTIONAL
*" EXPORTING
*" REFERENCE(TOTAL) TYPE I
*" CHANGING
*" REFERENCE(CLAIMS) TYPE ZEXP_CLAIM_TAB
*" RAISING
*" ZCX_EXP_CLAIM_NOT_FOUND
*"----------------------------------------------------------------------
IF include_rejected IS SUPPLIED.
" the caller decided; otherwise the parameter is initial
ENDIF.
ENDFUNCTION.
Two ways to report an error
A function module can tell its caller about an error in two ways, and both are in use.
A classic exception is a name in the EXCEPTIONS part of the interface. The module raises it with RAISE name, or with
MESSAGE ... RAISING name, which also stores a message text in the system fields. The caller lists the names in the
EXCEPTIONS part of CALL FUNCTION and gives each a number; after the call, sy-subrc holds the number of the exception that
happened, and 0 if none did. A name the caller did not list ends the program with a runtime error, unless the caller wrote
OTHERS, which collects all the names that it did not list. Nothing but the number travels with a classic exception.
A class-based exception is declared in the RAISING part and raised with RAISE EXCEPTION. The caller catches it with
TRY and CATCH, and the exception object carries a text and any attributes the class defines (see
Class-Based Exceptions). If the module raises one that is not declared in its
interface, the call ends with the exception CX_SY_NO_HANDLER instead. A module uses one style or the other: once the interface
has classic exceptions, RAISE EXCEPTION cannot be used in it, and a caller cannot give numbers to the exceptions of a module
that declares RAISING.
Example: reading an expense claim in both styles
The Expense claims application keeps claims in the table ZEXP_CLAIM. The function group ZEXP_CLAIMS below has two modules
that do the same work, read one claim by its number, and differ in how they report a missing claim. The first uses the
classic exception NOT_FOUND; the second raises the exception class zcx_exp_claim_not_found of this example. The TOP include
holds only the statement that makes the group a function pool.
FUNCTION-POOL zexp_claims.
The exception class stores the claim number and builds its text in get_text, the same technique as in
Class-Based Exceptions.
CLASS zcx_exp_claim_not_found DEFINITION
PUBLIC
INHERITING FROM cx_static_check
FINAL
CREATE PUBLIC.
PUBLIC SECTION.
DATA claim_id TYPE zexp_claim-claim_id READ-ONLY.
METHODS constructor
IMPORTING claim_id TYPE zexp_claim-claim_id
previous LIKE previous OPTIONAL.
METHODS if_message~get_text REDEFINITION.
ENDCLASS.
CLASS zcx_exp_claim_not_found IMPLEMENTATION.
METHOD constructor ##ADT_SUPPRESS_GENERATION.
super->constructor( previous = previous ).
me->claim_id = claim_id.
ENDMETHOD.
METHOD if_message~get_text.
result = |Claim { claim_id } does not exist|.
ENDMETHOD.
ENDCLASS.
The first function module declares NOT_FOUND under EXCEPTIONS. It reads the claim into its export parameter and raises the
classic exception when no row is found.
FUNCTION z_exp_read_claim.
*"----------------------------------------------------------------------
*"*"Local Interface:
*" IMPORTING
*" REFERENCE(CLAIM_ID) TYPE ZEXP_CLAIM-CLAIM_ID
*" EXPORTING
*" REFERENCE(CLAIM) TYPE ZEXP_CLAIM
*" EXCEPTIONS
*" NOT_FOUND
*"----------------------------------------------------------------------
SELECT SINGLE * FROM zexp_claim WHERE claim_id = @claim_id INTO @claim.
IF sy-subrc <> 0.
RAISE not_found.
ENDIF.
ENDFUNCTION.
The second function module declares the exception class under RAISING and raises it with RAISE EXCEPTION.
FUNCTION z_exp_read_claim_new.
*"----------------------------------------------------------------------
*"*"Local Interface:
*" IMPORTING
*" REFERENCE(CLAIM_ID) TYPE ZEXP_CLAIM-CLAIM_ID
*" EXPORTING
*" REFERENCE(CLAIM) TYPE ZEXP_CLAIM
*" RAISING
*" ZCX_EXP_CLAIM_NOT_FOUND
*"----------------------------------------------------------------------
SELECT SINGLE * FROM zexp_claim WHERE claim_id = @claim_id INTO @claim.
IF sy-subrc <> 0.
RAISE EXCEPTION NEW zcx_exp_claim_not_found( claim_id = claim_id ).
ENDIF.
ENDFUNCTION.
The report stores one claim, calls the classic module for a claim that exists and one that does not, and then calls the
class-based module in the same way inside a TRY block. In the call, note how the caller’s EXPORTING list feeds the module’s
IMPORTING parameter and the caller’s IMPORTING list receives the module’s EXPORTING parameter. The actual parameters are
declared before the call, because a function module call accepts no inline declaration after IMPORTING.
REPORT zp_fm.
START-OF-SELECTION.
TYPES ty_claims TYPE STANDARD TABLE OF zexp_claim WITH EMPTY KEY.
INSERT zexp_claim FROM TABLE @( VALUE ty_claims( ( claim_id = 'C-1001' status = 'A' amount = 120 ) ) ).
DATA claim TYPE zexp_claim.
" classic exception: the caller assigns a number and tests sy-subrc
CALL FUNCTION 'Z_EXP_READ_CLAIM'
EXPORTING
claim_id = 'C-1001'
IMPORTING
claim = claim
EXCEPTIONS
not_found = 4
OTHERS = 8.
WRITE / |classic, existing claim: sy-subrc { sy-subrc }, amount { claim-amount }|.
CLEAR claim.
CALL FUNCTION 'Z_EXP_READ_CLAIM'
EXPORTING
claim_id = 'C-9999'
IMPORTING
claim = claim
EXCEPTIONS
not_found = 4
OTHERS = 8.
WRITE / |classic, missing claim: sy-subrc { sy-subrc }|.
" class-based exception: TRY and CATCH
TRY.
CALL FUNCTION 'Z_EXP_READ_CLAIM_NEW'
EXPORTING
claim_id = 'C-1001'
IMPORTING
claim = claim.
WRITE / |class-based, existing claim: amount { claim-amount }|.
CALL FUNCTION 'Z_EXP_READ_CLAIM_NEW'
EXPORTING
claim_id = 'C-9999'
IMPORTING
claim = claim.
CATCH zcx_exp_claim_not_found INTO DATA(not_found).
WRITE / |class-based, missing claim: { not_found->get_text( ) }|.
ENDTRY.
This example runs in the local playground, which is a community tool and not SAP software. The playground reads the function group from files, one per include, instead of from the Function Builder, and the report and the exception class run as written. It prints the following.
classic, existing claim: sy-subrc 0, amount 120 classic, missing claim: sy-subrc 4 class-based, existing claim: amount 120 class-based, missing claim: Claim C-9999 does not exist
The first two lines come from the classic module. The call for the missing claim ends with sy-subrc 4, the number that the
caller gave to not_found, and the caller learns nothing else. The last two lines come from the class-based module: the
exception object travels to the CATCH block, which reads its text. If the first call had no EXCEPTIONS list, the missing
claim would have ended the program with a runtime error.
The CALL FUNCTION statement
CALL FUNCTION takes the name of the module as a character literal or as a variable that holds the name in capital letters. The
parameters are written in a fixed order: EXPORTING, IMPORTING, TABLES, CHANGING, then EXCEPTIONS. There is no
different syntax for a static and a dynamic call; the difference is only whether the name is a literal or a variable.
The syntax check of a method call compares every parameter with the method definition. For a function module it cannot: the name
is resolved when the statement runs. A misspelled parameter name or a wrong type therefore appears as a runtime error or a
catchable exception such as CX_SY_DYN_CALL_PARAM_MISSING, CX_SY_DYN_CALL_PARAM_NOT_FOUND or CX_SY_DYN_CALL_ILLEGAL_TYPE,
and a module that does not exist raises CX_SY_DYN_CALL_ILLEGAL_FUNC. Test every call, not just the code around it.
TABLES accepts only standard tables, and the actual parameters of IMPORTING and CHANGING must be existing variables:
inline declarations such as DATA(x) and writable expressions are not allowed, unlike in a method call. The name of the module
must never come unchecked from outside the program, for example from user input, because that lets an attacker run any function
module; check it first, for example with the system class CL_ABAP_DYN_PRG.
Source: ABAP Keyword Documentation: CALL FUNCTION, parameter_list and ABAP Keyword Documentation: CALL FUNCTION func
A classic exception carries no text, but a module that raised it with MESSAGE ... RAISING has filled the system fields
sy-msgid, sy-msgty, sy-msgno and sy-msgv1 to sy-msgv4, which describe the message. The predefined exception
error_message also gives a return code to the messages that a module sends without RAISING. This is the usual way to read the
text of a classic exception in old code. The following sketch requires an SAP
system; not run here.
CALL FUNCTION 'Z_EXP_READ_CLAIM'
EXPORTING
claim_id = claim_id
IMPORTING
claim = claim
EXCEPTIONS
not_found = 4
error_message = 6
OTHERS = 8.
IF sy-subrc <> 0.
MESSAGE ID sy-msgid TYPE 'I' NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ENDIF.
Processing types: normal, remote-enabled and update
A function module has a processing type, chosen in its attributes. Normal modules are ordinary procedures. The other two types exist for the technical reasons that the programming guidelines allow.
| Processing type | What it is for |
|---|---|
Normal |
An ordinary procedure called inside the same system. |
Remote-enabled (RFC) |
A module that other systems, or another session of the same system, can call. It is the entry point of a remote function call. |
Update module, start immediately (V1) |
High-priority database changes that run in the same SAP LUW and may be restarted by the update task after an error. A variant does not allow a restart. |
Update module, start delayed (V2) |
Low-priority changes that run in their own update transaction after the V1 modules. |
Remote-enabled modules
A remote function call, or RFC, is a call of a function module in another system. The statement is CALL FUNCTION with the
addition DESTINATION, which names a configured connection. In the plain form the caller waits for the result, which is a
synchronous RFC (sRFC). In classic Standard ABAP, STARTING NEW TASK continues at once (an asynchronous RFC, aRFC) and
IN BACKGROUND UNIT queues the call for execution after the next COMMIT WORK (a background RFC, bgRFC); the older
transactional and queued RFC are obsolete and replaced by bgRFC. The Keyword Documentation for ABAP Cloud (CALL FUNCTION, RFC) lists only the
synchronous forms (DESTINATION and IN REMOTE SESSION); RFC, BAPIs and IDocs compares all the
variants. External programs written in Java, .NET or C reach the same module through the SAP connector
libraries. Because the call crosses a system boundary, the interface of a remote-enabled module is restricted:
-
Every
IMPORTING,EXPORTINGandCHANGINGparameter is passed by value. -
A parameter is typed with an ABAP Dictionary type or a built-in type, never with a reference type or a type from a type pool.
-
Only classic exceptions can be declared. A class-based exception that the module raises cannot cross the boundary: the caller gets the predefined classic exception
SYSTEM_FAILUREinstead, andCOMMUNICATION_FAILUREandRESOURCE_FAILUREreport errors of the connection. Handle all three in the caller. -
A synchronous or asynchronous RFC triggers a database commit in the caller.
The following sketch calls the claim module of a remote system and requires an SAP system with an RFC destination named
EXP_REMOTE; not run here.
DATA message TYPE c LENGTH 200.
CALL FUNCTION 'Z_EXP_READ_CLAIM'
DESTINATION 'EXP_REMOTE'
EXPORTING
claim_id = claim_id
IMPORTING
claim = claim
EXCEPTIONS
not_found = 4
system_failure = 8 MESSAGE message
communication_failure = 12 MESSAGE message.
CASE sy-subrc.
WHEN 0.
" claim read
WHEN 4.
" the claim does not exist
WHEN OTHERS.
WRITE / message.
ENDCASE.
Source: Keyword Documentation for Standard ABAP: RFC - Introduction, RFC - Restrictions and RFC - Exceptions
Whether other systems may call a remote-enabled module is a security matter. ADT lets you set the call scope of the module (only the same client and user, the same system, or any system); the safe choice is the narrowest scope that works, and the module has to check authorizations for a wide scope.
Update modules
An update function module holds changes to the database that must be written together, in one database LUW, at the end of a
transaction. A program does not call it directly: it registers it with CALL FUNCTION ... IN UPDATE TASK, which stores the name
and the parameters. Nothing runs yet. The statement COMMIT WORK then starts the update work process, which runs all
registered modules; ROLLBACK WORK, or ending the program without a commit, discards them. Inside an update module you cannot
commit, roll back or check authorizations, and a failing module is rolled back and can be restarted after the cause is fixed
(see ABAP SQL: Writing Data for the SAP LUW). The sketch requires an
SAP system; not run here.
" the update module Z_EXP_POST_CLAIM has the processing type "Update module, start immediately"
CALL FUNCTION 'Z_EXP_POST_CLAIM' IN UPDATE TASK
EXPORTING
claim = claim.
COMMIT WORK. " the registered module runs now, in the update work process
The parameters of an update module are stored when it is registered, so no reference variables can be passed to it, and
IMPORTING, CHANGING and EXCEPTIONS in the registering call are ignored.
Testing a function module in SE37
The Function Builder has a test environment that runs a module without writing a program around it. Use it before you call a module you have not used before, or after you change one of your own. You need an SAP system; this part cannot be run here.
-
Start SE37, enter the name of the function module and choose Test (F8). The Test Function Module screen lists the import, changing and tables parameters.
-
Fill in the values: type a value for a single-field parameter, and double-click the name of a structure or table parameter to fill it in a table editor.
-
Choose Execute (F8). The screen shows the export parameters, any exception that occurred, and the elapsed time.
-
To step through the code instead, select Debugging before you execute. A test can also be run with a runtime analysis.
A test with an input that does not exist, for example the claim C-9999, shows the exception name in the result, which is the
fastest way to see what a classic exception looks like. In ADT you can edit a function module, and ADT can open the classic tool in an embedded SAP GUI (see Classic Tools: SE80, SE38, SE11, SE37 and SE16).
Function module or method
A method is the better choice for new code. It has a full syntax check, can be tested with ABAP Unit, lives in a class that you can reuse and replace, and works in ABAP Cloud. Choose a function module only in these cases:
-
The caller is another system or an external program, so the target must be a remote-enabled module.
-
The code must be registered as an update module.
-
You maintain an existing function group and a rewrite is not worth the risk.
In the first two cases, keep the function module thin: it takes the parameters, calls a method of a class that holds the real logic, and hands the result back. The sketch below requires an SAP system; not run here.
FUNCTION z_exp_approve_claim.
*"----------------------------------------------------------------------
*"*"Local Interface:
*" IMPORTING
*" VALUE(CLAIM_ID) TYPE ZEXP_CLAIM-CLAIM_ID
*" EXPORTING
*" VALUE(APPROVED) TYPE ABAP_BOOL
*"----------------------------------------------------------------------
approved = zcl_exp_claim_service=>approve( claim_id ).
ENDFUNCTION.
Older material you may meet
Books and code from before ABAP Objects teach function modules as the normal way to share code between programs, and many of them describe parameter kinds and exceptions that the Keyword Documentation now marks as obsolete. Read them as a description of what exists in old systems, not as a model for new code.
-
TABLESparameters,LIKEandSTRUCTUREtyping and untyped parameters are obsolete. Replace them with typedIMPORTING,EXPORTINGorCHANGINGparameters. -
Classic exceptions,
RAISEandMESSAGE ... RAISINGare marked as deprecated syntax that is supported only temporarily. A new module that is not remote-enabled declaresRAISINGwith an exception class. A remote-enabled module can still declare only classic exceptions. -
Global data of the function group as a hidden memory between calls makes behaviour depend on call order; pass the data through parameters.
-
Function modules that contain the whole business logic are replaced by a class with the logic and a thin function module as wrapper where one is needed.
Common mistakes
-
Forgetting
OTHERSor the name of a classic exception in the call: the exception ends the program with a runtime error. -
Not testing
sy-subrcafter a call that has anEXCEPTIONSlist: the program carries on with an initial result. -
Putting
EXPORTINGin the call for what the module declares asEXPORTING: the call’sEXPORTINGfeeds the module’sIMPORTING, and the call’sIMPORTINGreceives the module’sEXPORTING. -
Writing
IMPORTING claim = DATA(claim): inline declarations are not allowed in a function module call. -
Relying on a global variable of the function group to keep a value between calls from different internal sessions: the group is loaded again in each session.
-
Declaring a class-based exception in a remote-enabled module: that is not possible, and an exception raised there reaches the remote caller as
SYSTEM_FAILURE.
Summary
-
A function module is a reusable procedure with a system-wide name, stored in a function group, and called with
CALL FUNCTION. -
A function group is a function pool program with a main program
SAPL<name>, a TOP include for global data and one include per module; at most 99 modules fit in it, and the whole group is loaded when one module is called. -
The interface has
IMPORTING,EXPORTING,CHANGING, the obsoleteTABLES, and eitherRAISINGfor class-based exceptions orEXCEPTIONSfor classic ones. -
A classic exception reaches the caller as a number in
sy-subrc; a class-based exception is caught withTRYandCATCH. -
Remote-enabled modules (RFC) take their parameters by value and only classic exceptions; update modules are registered with
IN UPDATE TASKand run atCOMMIT WORK. -
SE37 tests a module with typed input and the debugger; new code uses methods, and a function module only wraps a method where RFC or the update task requires it.
References
-
ABAP Keyword Documentation: Properties of Interface Parameters
-
ABAP Keyword Documentation: Class-Based Exceptions in Procedures
-
Keyword Documentation for Standard ABAP: CALL FUNCTION, IN UPDATE TASK
-
Keyword Documentation for Standard ABAP: Function Modules and Subroutines (programming guideline)
-
ABAP Workbench Tools: Interface Parameters of a Function Module
-
ABAP Development Tools: Specific Properties of Function Modules