
Subroutines
Subroutines were originally designed for the internal modularization of a program. They are implemented between the statements FORM and ENDFORM. The additions USING and CHANGING of the statement FORM define the parameter interface. The addition RAISING declares exceptions that can be propagated from the subroutine to the caller. Subroutines are called using the statement PERFORM. Here, the assignment between actual parameters and formal parameters is made using the parameter position. The addition IN PROGRAM is used to call subroutines defined in another program.
Most subroutines are now obsolete and should be replaced by methods in local classes in new programs.
Example
Defines and calls a subroutine.
DATA: num1 TYPE i,
num2 TYPE i,
mean TYPE p DECIMALS 2.
START-OF-SELECTION.
num1 = 4.
num2 = 5.
PERFORM mean_value USING num1 num2
CHANGING mean.
FORM mean_value USING p1 TYPE i p2 TYPE i
CHANGING result TYPE numeric.
result = ( p1 + p2 ) / 2.
ENDFORM.
Function Modules
Function modules are designed to provide reusable functions. A function module is always part of a precisely one function group and has a unique name within an SAP system. As well as its local data area, function modules can also access global data from the function group. A function group can be viewed as a type of abstract final class whose static methods are the function modules.
Function modules and their parameter interfaces are defined using Function Builder and implemented between the statements FUNCTION and ENDFUNCTION. A function module is called using the statement CALL FUNCTION. Here, the assignment between actual parameters and formal parameters is made using the parameter names.
Most regular function modules are now obsolete and should be replaced by methods in global classes in new applications.
Specifying the addition DESTINATION of the statement CALL FUNCTION makes the function module call to an RFC call. Remote Function Call (RFC) makes it possible to call function modules on other application servers of the same system or in a different SAP system. The function module in question runs on the target server in the context of a separate user logon. A function module must first be flagged as remote-capable in Function Builder before it can be called using RFC. RFC calls also work as external interfaces to external systems.
Example
Calls a function module for reading a database table.
DATA flight_tab TYPE STANDARD TABLE OF spfli.
CALL FUNCTION 'READ_SPFLI_INTO_TABLE'
EXPORTING id = 'LH'
IMPORTING itab = flight_tab.