Вы находитесь на странице: 1из 33

Function Modules

VFunction Modules are procedures that are defined in


function groups and can be called from any ABAP program.
VFunction groups and Function Modules are created in the
ABAP workbench using the Function Builder.
VFunction Modules are stored in a central library.
Unlike subroutines, you do not define function modules in
the source code . Instead we use the Function Builder.
Function Groups

VFunction groups are containers for function modules.


When we call a function module, the system loads the
whole of its function group into the internal session of
the calling program.
VThe name of a function group can be up to 26 characters
long. This name is used to create the components of the
group. When you create a function group or function
module , the main program and corresponding include
programs are generated automatically.
VThe following diagram shows the structure of a function
group.
 
 
VAPL<fgrp> contains nothing but the INCLUDE
statements for the following include programs:
VL<fgrp>TOP. This contains the FUNCTION-POOL
statement (equivalent for a function group of the
REPORT or PROGRAM statement) and global data
declarations for the entire function group.
VL<fgrp>UXX. This contains further INCLUDE
statements for the include programs L<fgrp>U01,
L<fgrp>U02, ... These includes contain the actual
function modules.
VThe include programs L<fgrp>F01, L<fgrp>F02, ...
can contain the coding of subroutines that can be
called with internal subroutine calls from all
function modules of the group.
 
ºAll of the function modules in a function group can access
the global data of the group. For example, if you have a set
of function modules that all use the same internal table, you
could place them in a function group containing the table
definition in its global data.
Function Modules

   


ºBefore programming a new function or creating a new function


module, you should look in the Function Builder to see whether there
is an existing function module that already performs the same task.
Function Modules
For example, you need a function module which is used for TRING
manipulation , use *TRING* as a search condition.
Ex:TRING_PLIT_AT_POITION

ºO  
   
 
  
The documentation describes the purpose of the function module, lists
the parameters for passing data to and from the module, and the
exceptions. It tells you how you can pass data to and from the function
module, and which errors it handles.
Function Modules
M   M
  M
Import parameters.
These must be supplied with data when you call the function module,
unless they are flagged as optional. You cannot change them in the
function module.
Export parameters.
These pass data from the function module back to the calling
program. Export parameters are always optional. You do not have to
receive them in your program.
Changing parameters.
These must be supplied with data when you call the function module,
unless they are flagged as optional. They can be changed in the function
module. The changed values are then returned to the calling program.
Function Modules
‡Tables parameters.
You use these to pass internal tables. They are treated like
CHANGING parameters. However, you can also pass internal
tables with other parameters if you specify the parameter type
appropriately.
‡Exceptions
Exceptions are used to handle errors that occur in function
modules. The calling program checks whether any errors have
occurred and then takes action accordingly.
First, create a Function Group ZXXX .
In E37(Function Builder)
ave
To Activate the Function Group, do the following steps
To Activate
elect main Program
Activate
Error
teps to Rectify Activation Error

To Rectify the error, do the following steps.

1. Double Click on Include LXXXXTOP.


2. Copy the first line
µ 
  
 
3. Paste the line as the first line in the Main Program.
4. Press ave button
5. Press back Button
6. Press Cancel Button
7. Now press the Activate button, the Function Group will get
Activated.
Create Function Module:
Assign the Function Group
Import Parameter
Export Parameter
Changing Parameter
Tables Parameter
Exceptions
ource Code
ave, Activate and Execute the Function Module.
Pass the Input parameters as shown in the screen shot.
    
 

To call a function module, use the CALL FUNCTION statement:


CALL FUNCTION <module>
[EXPORTING f1 = a 1.... f n = a n]
[IMPORTING f1 = a 1.... f n = a n]
[CHANGING f1 = a 1.... f n = a n]
[TABLES f1 = a 1.... f n = a n]
[EXCEPTIONS e1 = r 1.... e n = r n [ERROR_MESSAGE = r E]
[OTHERS = ro]].
    


º You can specify the name of the function module <module> either as a
literal or a variable. Each interface parameter <fi> is explicitly assigned
to an actual parameter <a i>. You can assign a return value <r i> to
each exception <e i>. The assignment always takes the form <interface
parameter> = <actual parameter>. The equals sign is not an
assignment operator in this context.

º After EXPORTING, you must supply all non-optional import


parameters with values appropriate to their type. You can supply
values to optional import parameters if you wish.
º After IMPORTING, you can receive the export parameters from the
function module by assigning them to variables of the appropriate
type.
º After CHANGING or TABLE, you must supply values to all of the non-
optional changing or tables parameters. When the function module
has finished running, the changed values are passed back to the
actual parameters. You can supply values to optional changing or
tables parameters if you wish.
Calling Function Modules

º You can use the Exceptions option to handle the exceptions of the
function module. If an exception <ei> is raised while the function
module is running, the system terminates the function module and
does not pass any values from the function module to the program. If
the <ei> is specified in the exception option, the calling program
handles the exception by assigning <ri> to Y-UBRC. <ri> must be a
numeric field.
Calling Function Modules

Example:
Calling Function Modules

Example for Calling a tandard Function Module

Вам также может понравиться