Advantages : Because the external subroutines and function modules are self-contained, they can be debugged independently of each other. Changes only have to be made in the one program - all other programs that call that subroutine or function module will be affected by the change. The overall complexity of the program is reduced.
When the perform command is executed, all statements between the form and endform are processed. Data defined in the ‘main’ program is global and can be used and changed in the subroutine. Double-Click on the subroutine name in the perform statement to create the subroutine at the bottom of the program code with documentation .
The output from the above program would be 1 2 1. Where possible, local data should be used in a subroutine. The contents of a local data object can be retained from one call of the subroutine to the next by using static variables. Example : perform count. perform count. ….. form count. statics calls type i. calls = calls + 1. write calls. endform. … will produce the output 1 2.
This is the preferred method for passing data to a subroutine. Internal tables are always global.
The result from the first call : answer = 6. The result from the second call : answer = 3. The parameter names in the PERFORM statement are called ACTUAL PARAMETERS. The parameter names in the FORM statement are called FORMAL PARAMETERS. The above example passes parameters by reference. The next slide explains this.
Parameters can be called either by reference or by value. Calling by reference: The address of the actual parameter is called. Within the subroutine, the variable is addressed using the formal parameter name and changes have an immediate. If only the formal parameter name is specified in the subroutine interface, then the parameter is called by reference. Calling by value: When the subroutine is called the actual parameter value is copied to the formal parameter. There are two types of call by value: Calling by value: the formal parameter is listed in the interface after the USING clause with the addition VALUE( <parameter name> ) . When the subroutine is called, the actual parameter is copied to the formal parameter. Changes made to the formal parameter only affect the local copy, not the actual parameter. Calling by value and result: the formal parameter is listed in the interface after the CHANGING clause with the addition VALUE( <parameter name> ) . When the subroutine is called, the actual parameter is copied to the formal parameter. Changes made to the formal parameter initially only affect the local copy. When the ENDFORM statement is reached, the formal parameter value is copied back to the actual parameter.
All rules re passing of data as for internal subroutines, except that using parameters is the only way of passing data. (Global data in the calling program may not be referenced in the called subroutine.) SAP do not recommend using External Subroutines.
Examples of Function Modules : Calculates whether a date falls on a Sunday. Rounding a number with decimals to 2 places. Function modules possess a clearly defined fixed interface for data exchange. This makes it easier for you to pass input and output parameters to and from the function module. The function module interface supports exception handling. This allows you to catch errors and pass them back to the calling program for handling. Function modules use their own memory area. The calling program and the function module cannot exchange data using a shared memory area - they must use the function module interface. This avoids unpleasant side effects such as accidentally overwriting data. How many function modules are supplied with SAP? In release 3.1, more than 43,000 function modules are available. In rel 4.5 more than 80,000 function modules are available. (4.6B > 98,000; Enterprise > 170,000) The are many thousands of function groups which contain function modules.
Hint: Add Function Builder to Favorites Information system allows you to search for function modules using search criteria of function name, short description and/or function group. SAP Applications allows you to list function modules associated with application components such as FI, SD, MM.
NOTES: Changing parameters appears to be new in ver 4.0 or greater Documentation can be found in the Goto pull-down menu.
{Pattern} button will display Insert Statement dialog. Enter or search for required function and click Continue to insert function into ABAP Editor. Ctrl-F6 shortcut to produce Insert Statement dialog.
Once the function module template is inserted, it is up to the programmer to fill it in. Note the use of ‘X’ for export SIGN variable. This comes from the function’s documentation where the code “X” indicates commercial rounding. The use of the MESSAGE Statement with system message codes allows the function to handle error processing and produce appropriate error messages. IF SY-SUBRC <> 0. MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4. ENDIF. You can choose to handle errors yourself and define your own error messages by replacing the above code with something like: CASE SY-SUBRC. WHEN 0. write: inputnum, ' commercially rounded = ', rounded. WHEN 1. write 'Input Invalid'. WHEN 2. write 'Overflow Error'. WHEN 3. write 'Output Field type Invalid'. WHEN 4. write 'Unknown Error'. ENDCASE.