The main difference between a function module and a normal ABAP/4 subroutine is as follows:
Function is stored in central library and has global presence while subroutines are confined to a particular program. Subroutine cannot return values while functions can return values. Unlike functions, subroutine cannot handle exceptions. And last but not least, the difference in the way the parameters are passed to functions.
Declaring data as common parts is not possible for function modules. The calling program and the called function module have separate work in ABAP/4 Dictionary tables.
You use the ABAP/4 Workbench to call, create, and maintain function modules.
You can combine several function modules to form a function group in the function library. Since you can define global data that can be accessed from all function modules of one function group, it is reasonable to include function modules that operate with the same data, for example internal table for sales module can be grouped, in one function group.
Via the ABAP/4 Development Workbench screen, choose Development Function Library or select Function Library in the application toolbar or use se37 transaction code.
The Function Library:
The function library maintains Function Modules, the screen displays following components:
All function names should start with Z_ or Y_ followed by any name.
• Source code
• Documentation
• Administrative info
• Import-export parameters
• Table parameters and exception interface
• Global data
• Main program
(Not necessarily in the same order)
Documentation
The documentation describes the purpose of the function module, lists the parameters for passing data to and from the module, and the exceptions. The parameters of the parameter type I are import parameters, which are used to pass data to the function module. Parameters of the parameter type E are export parameters, which are used to pass data from, the function module to the calling program. Exceptions describe error scenarios, which can occur in function modules.
Import-export parameters
Import parameters correspond to the formal input parameter of subroutines. They pass data from the calling program to the function module.
Export parameters correspond to the formal output parameters of subroutines. They pass data from the function module back to the calling program (which his not possible in subroutines)
Table parameters
Table parameters are internal tables. Internal tables are treated like changing parameters and are always passed by reference.
Exceptions
Exceptions are used to handle error scenarios, which can occur in function modules. The function module checks for any type of error and raise exception and returns SY-SUBRC to the calling program. Main program checks this SY-SUBRC for any errors that have occurred and then takes action accordingly.
Source Code
The ABAP/4 Edit screen displays the ABAP/4 source code of the function module. You can work with the source code in the same way as you would work with normal ABAP/4 programs.
Import parameters, changing parameters, and table parameters can be Optional. This means that you can omit the corresponding actual parameter when you call the function in the calling program. If the parameter is optional and the actual parameter is not specified, you can specify a default value for use in the function module. Export parameters are always optional.
As with subroutines, you can specify the data types of the formal parameters in the field Reference type. In the field Ref. structure, you can specify ABAP/4 Dictionary reference structures or fields. Then, the system checks the current parameter against the structure or field at runtime.
Testing of function module
You can test a function module without calling it from an ABAP/4 program via the Function Library: Maintain Function Modules screen by choosing Single test. You can assign values to the import parameters on the Test Function Modules screen.
Calling Function Modules
To call a function module from an ABAP/4 program, use the CALL statement as follows:
Syntax:
CALL FUNCTION
[EXPORTING
f1 = s1
f2 = s2
fn = sn (parameters which you pass from program to function are
s1, s2, sn)]
[IMPORTING
f1 = r1
f2 = r2
fn = rn (parameters which program receives you pass from function in
r1, r2, rn)]
[TABLES f1 = a1 … fn = an]
EXCEPTIONS notvalid = 1
not correct = 2
OTHERS = 5].
You can specify the name of the function module
If in your function if you have raised exception not valid then this exception can be handled in main program. Functions return different sy-subrc for different exceptions.
No comments:
Post a Comment