📄 ch19.htm
字号:
<P>
A <I>function module</I> is the last of the four main ABAP/4 modularization
units. It is very similar to an external subroutine in these ways:
<UL>
<LI>Both exist within an external program.
<LI>Both enable parameters to be passed and returned.
<LI>Parameters can be passed by value, by value and result, or
by reference.
</UL>
<P>
The major differences between function modules and external subroutines
are the following:
<UL>
<LI>Function modules have a special screen used for defining parameters-parameters
are not defined via ABAP/4 statements.
<LI><TT>tables</TT> work areas are <I>not</I> shared between the
function module and the calling program.
<LI>Different syntax is used to call a function module than to
call a subroutine.
<LI>Leaving a function module is accomplished via the <TT>raise</TT>
statement instead of <TT>check</TT>, <TT>exit</TT>, or <TT>stop</TT>.
</UL>
<P>
A function module name has a practical minimum length of three
characters and a maximum length of 30 characters. Customer function
modules must begin with <I>Y_</I> or <I>Z_</I>. The name of each
function module is unique within the entire R/3 system.
<H2><A NAME="UnderstandingFunctionGroups"><FONT SIZE=5 COLOR=#FF0000>
Understanding Function Groups</FONT></A></H2>
<P>
As stated before, a function group is a program that contains
function modules. With each R/3 system, SAP supplies more than
5,000 pre-existing function groups. In total, they contain more
than 30,000 function modules. If the functionality you require
is not already covered by these SAP-supplied function modules,
you can also create your own function groups and function modules.
<P>
<IMG SRC="../button/newterm.gif" tppabs="http://pbs.mcp.com/ebooks/0672312174/button/newterm.gif">
<P>
Each function group is known by a four-character identifier called
a <I>function group ID</I>. If you create your own function group,
you must choose a function group ID that begins with <I>Y</I>
or <I>Z</I>. The ID must be exactly four characters long and cannot
contain blanks or special characters.<P>
<CENTER>
<TABLE BORDER=1>
<TR VALIGN=TOP><TD WIDTH=600><B>CAUTION</B></TD></TR>
<TR VALIGN=TOP><TD WIDTH=600>
<BLOCKQUOTE>
On releases prior to 3.0F, the R/3 system enables you to specify a function group ID of fewer than four characters or to use spaces within it. However, you should not do this because the tools that manipulate function modules will behave erratically with such a function group.</BLOCKQUOTE>
</TD></TR>
</TABLE>
</CENTER>
<P>
<P>
To illustrate function groups, I will briefly describe the process
of creating a function group and a function module within it.
Let's assume that neither the function group nor the function
module yet exist.
<P>
You begin by creating a function module. When you create a function
module, the system will first ask you for a function group ID.
This ID tells the system where to store the function module.
<P>
When you supply the ID, and if it doesn't yet exist, the system
creates:
<UL>
<LI>A main program
<LI>A top <TT>include</TT>
<LI>A UXX <TT>include</TT>
<LI>A function module <TT>include</TT>
</UL>
<P>
Collectively, these components are known as the function group.
Their relationships are illustrated in Figure 19.2.
<P>
<A HREF="javascript:popUp('f19-2.gif')"><B>Figure 19.2 :</B> <I>This is the hierarchical relationship of
the components of a function group</I>.</A>
<P>
The name of the main program will be <TT>sapl<I>fgid</I></TT>,
where <TT><I>fgid</I></TT> is
your four-character function group ID. The system automatically
places two <TT>include</TT> statements into it:
<UL>
<LI><TT>include l<I>fgid</I>top.</TT>
<LI><TT>include l<I>fgid</I>uxx.</TT>
</UL>
<P>
<IMG SRC="../button/newterm.gif" tppabs="http://pbs.mcp.com/ebooks/0672312174/button/newterm.gif">
<P>
The first include program-<TT>l<I>fgid</I>top</TT>-is
known as the <I>top include</I>. Within it you can place global
data definitions. These are data definitions that are global to
all function modules within the group.
<P>
<IMG SRC="../button/newterm.gif" tppabs="http://pbs.mcp.com/ebooks/0672312174/button/newterm.gif">
<P>
The second include program-<TT>l<I>fgid</I>uxx</TT>-is
known as the <I>UXX</I>. You are not allowed to modify the UXX.
The system will automatically place an <TT>include</TT> statement
in the UXX for each function module you create in this function
group. For the first function module, the statement <TT>include
l<I>fgid</I>u01</TT> will be inserted
into the UXX. When you create a second function module in this
group, the system will add a second statement: <TT>include l<I>fgid</I>u02</TT>.
Each time you add a new function module to the group, the system
will add a new <TT>include</TT> statement to the UXX. The number
of the <TT>include</TT> will be <TT>01</TT> for the first function
module created within the group, <TT>02</TT> for the second, <TT>03</TT>
for the third, and so on.
<P>
Within each <TT>include</TT> mentioned in the UXX is the source
code for a function module.
<H3><A NAME="AccessingtheFunctionLibrary">
Accessing the Function Library</A></H3>
<P>
<IMG SRC="../button/newterm.gif" tppabs="http://pbs.mcp.com/ebooks/0672312174/button/newterm.gif">
<P>
Function groups are stored in a group of tables within the database
called the <I>function library</I>. By accessing the function
library, you can also access the tools by which you manipulate
function modules and groups. To access the function library from
the Development Workbench, press the Function Library button on
the Application toolbar. The transaction code is <TT>SE37</TT>.
<P>
Each of the components of a function group can be accessed from
the Function Library Initial Screen shown in Figure 19.3.
<P>
<A HREF="javascript:popUp('f19-3.gif')"><B>Figure 19.3 :</B> <I>The Function Library Initial Screen</I>.</A>
<P>
For example, to access the main program of the function group,
choose the Main Program radio button and then press the Display
pushbutton. The main program of function group <TT>ztxa</TT> is
shown in Figure 19.4.
<P>
<A HREF="javascript:popUp('f19-4.gif')"><B>Figure 19.4 :</B> <I>The main program of function group ztxa</I>.</A>
<P>
Figure 19.5 shows the UXX for function group <TT>ztxa</TT>.
<P>
<A HREF="javascript:popUp('f19-5.gif')"><B>Figure 19.5 :</B> <I>The UXX for function group ztxa</I>.</A>
<P>
In Figure 19.5, you can see that function group <TT>ztxa</TT>
contains two function modules: <TT>z_tx_1901</TT> and <TT>z_tx_1902</TT>.
The first one is active, the second is not (its name is commented
out).
<H3><A NAME="ActivatingaFunctionModule">
Activating a Function Module</A></H3>
<P>
A function module must be <I>activated</I> before it can be called.
There is an Activate button on the Function Library Initial Screen.
Pressing it activates the function module.
<P>
When the function module is first created, the <TT>include</TT>
statement for it within the UXX is commented out. Activating the
function module causes the system to remove the comment from the
<TT>include</TT> statement. The function module is then available
to be called from other programs. If you change the code within
the function module, it doesn't need to be activated again. On
the other hand, re-activating won't do any harm, either.
<P>
If you want, you can deactivate a function module. From the ABAP/4
Function Library: Initial Screen, choose the menu path Function
Module->Deactivate. Choosing this menu path comments out the
<TT>include</TT> statement for that function module within the
UXX.
<P>
All the function modules that belong to a single group exist within
the main program: <TT>sapl<I>fgid</I></TT>.
Because of that, a syntax error in any one of these function modules
will cause the rest to become inoperative; executing any one of
them will result in a syntax error. The Activate and Deactivate
functions enable you to work on any single function module of
a group without affecting the rest. You can deactivate a function
module before you work on it, change the code, and then perform
a syntax check on that single module before reactivating it. In
that way, the rest of the function modules can still be called,
even while you are working on one of them.
<H3><A NAME="DefiningDatawithinaFunctionModule">
Defining Data within a Function Module</A></H3>
<P>
Data definitions within function modules are similar to those
of subroutines.
<P>
Within a function module, use the <TT>data</TT> statement to define
local variables that are reinitialized each time the function
module is called. Use the <TT>statics</TT> statement to define
local variables that are allocated the first time the function
module is called. The value of a static variable is remembered
between calls.
<P>
Define parameters within the function module interface to create
local definitions of variables that are passed into the function
module and returned from it (see the next section).
<P>
You cannot use the <TT>local</TT> statement within a function
module. Instead, globalized interface parameters serve the same
purpose. See the following section on defining global data to
learn about local and global interface parameters.
<H3><A NAME="DefiningtheFunctionModuleInterface">
Defining the Function Module Interface</A></H3>
<P>
<IMG SRC="../button/newterm.gif" tppabs="http://pbs.mcp.com/ebooks/0672312174/button/newterm.gif">
<P>
To pass parameters to a function module, you must define a <I>function
module interface</I>. The function module interface is the description
of the parameters that are passed to and received from the function
module. It is also simply known as the <I>interface</I>. In the
remainder of this chapter, I will refer to the function module
interface simply as the interface.
<P>
To define parameters, you must go to one of two parameter definition
screens:
<UL>
<LI>Import/Export Parameter Interface
<LI>Table Parameters/Exceptions Interface
</UL>
<P>
The Import/Export Parameter Interface screen is shown in Figure
19.6. On this screen you can define the following:
<P>
<A HREF="javascript:popUp('f19-6.gif')"><B>Figure 19.6 :</B> <I>The Import/Export Parameter Interface screen</I>.</A>
<UL>
<LI>Import parameters
<LI>Export parameters
<LI>Changing parameters
</UL>
<P>
The Table Parameters/Exceptions Interface screen is shown in Figure
19.7. On this screen you can:
<P>
<A HREF="javascript:popUp('f19-7.gif')"><B>Figure 19.7 :</B> <I>The Table Parameters/Exceptions Interface
screen</I>.</A>
<UL>
<LI>Define internal table parameters
<LI>Document exceptions
</UL>
<P>
You enter the name of the parameter in the first column and the
attributes of the parameter in the remaining columns. Enter one
parameter per row.
<P>
<IMG SRC="../button/newterm.gif" tppabs="http://pbs.mcp.com/ebooks/0672312174/button/newterm.gif">
<P>
<I>Import parameters</I> are variables or field strings that contain
values passed into the function module from the calling program.
These values originate outside of the function module and they
are imported into it.
<P>
<IMG SRC="../button/newterm.gif" tppabs="http://pbs.mcp.com/ebooks/0672312174/button/newterm.gif">
<P>
<I>Export parameters</I> are variables or field strings that contain
values returned from the function module. These values originate
within the function module and they are exported out of it.
<P>
<IMG SRC="../button/newterm.gif" tppabs="http://pbs.mcp.com/ebooks/0672312174/button/newterm.gif">
<P>
<I>Changing parameters</I> are variables or field strings that
contain values that are passed into the function module, changed
by the code within the function module, and then returned. These
values originate outside the function module. They are passed
into it, changed, and passed back.
<P>
<IMG SRC="../button/newterm.gif" tppabs="http://pbs.mcp.com/ebooks/0672312174/button/newterm.gif">
<P>
<I>Table parameters</I> are internal tables that are passed to
the function module, changed within it, and returned. The internal
tables must be defined in the calling program.
<P>
<IMG SRC="../button/newterm.gif" tppabs="http://pbs.mcp.com/ebooks/0672312174/button/newterm.gif">
<P>
An <I>exception</I> is a name for an error that occurs within
a function module. Exceptions are described in detail in the following
section.
<H3><A NAME="PassingParameters">
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -