📄 ch19.htm
字号:
<P>
<B>Listing 19.6 The Source Code for the z_tx_div Function
Module<BR>
</B>
<BLOCKQUOTE>
<PRE>
1 function z_tx_div.
2 *"------------------------------------------------------------
3 *"*"Local interface:
4 *" IMPORTING
5 *" VALUE(P1) TYPE I DEFAULT 1
6 *" VALUE(P2) TYPE I
7 *" EXPORTING
8 *" VALUE(P3) TYPE P
9 *"------------------------------------------------------------
10 p3 = p1 / p2.
11 endfunction.
</PRE>
</BLOCKQUOTE>
<HR>
<P>
<IMG SRC="../button/output.gif" tppabs="http://pbs.mcp.com/ebooks/0672312174/button/output.gif">
<P>
The code in Listing 19.6 is called from line 6 of Listing 19.5.
<P>
The code in Listing 19.5 produces this output:
<BLOCKQUOTE>
<PRE>
2 / 3 = 0.67
</PRE>
</BLOCKQUOTE>
<P>
<IMG SRC="../button/analysis.gif" tppabs="http://pbs.mcp.com/ebooks/0672312174/button/analysis.gif">
<UL>
<LI>In Listing 19.5, lines 2 and 3 define two variables, <TT>op1</TT>
and <TT>op2</TT>. Because these are defined using the <TT>parameters</TT>
statement, the user is prompted for values for these variables
when the program executes.
<LI>Line 4 defines a variable named <TT>rslt</TT> type <TT>p</TT>
with two decimal places.
<LI>Line 6 transfers control to function module <TT>Z_TX_DIV</TT>.
The value of variable <TT>op1</TT> is assigned to import parameter
<TT>p1</TT> and the value of <TT>op2</TT> is assigned to import
parameter <TT>p2</TT>. In the Export/Import Parameters screen,
a Reference Type is specified for these parameters, which makes
them typed parameters. Because these parameters are typed, they
do not obtain their technical attributes from <TT>p1</TT> and
<TT>p2</TT>. Instead, the data types of <TT>op1</TT> and <TT>op2</TT>
must match the data types of <TT>p1</TT> and <TT>p2</TT>. <TT>op1</TT>
and <TT>op2</TT> are defined as integers, therefore the data types
match. <TT>P3</TT> is type <TT>p</TT>; it is a partially typed
parameter. It obtains its length and number of decimals from <TT>rslt</TT>.
The values of <TT>op1</TT> and <TT>op2</TT> are transferred to
the function module and control transfers to line 1 of Listing
19.6.
<LI>In Listing 19.6, line 10 performs a simple division.
<LI>The function module returns control to line 6 of Listing 19.5.
The value of <TT>p3</TT> is assigned to variable <TT>rslt</TT>.
<LI>Line 13 writes out the result of the division.
</UL>
<P>
<CENTER>
<TABLE BORDER=1>
<TR VALIGN=TOP><TD WIDTH=600><B>TIP</B></TD></TR>
<TR VALIGN=TOP><TD WIDTH=600>
<BLOCKQUOTE>
Press the Pattern button on the ABAP/4 Editor: Edit Program screen to automatically insert the <TT>call function</TT> statement into your code. You will be asked for the function module name and it automatically codes the left-hand side of all interface parameter assignments for you.
</BLOCKQUOTE>
</TD></TR>
</TABLE>
</CENTER>
<P>
<H2><A NAME="CreatingaFunctionModule"><FONT SIZE=5 COLOR=#FF0000>
Creating a Function Module</FONT></A></H2>
<P>
Use the following procedure to create a function module. For an
exercise, follow this procedure and create a function module exactly
like <TT>z_tx_div</TT>. Call yours <TT>z_div</TT>, and put it
in function group <TT>za</TT> (remember to replace the with the
two characters that you are using to identify your programs).
<P>
<img src="../button/screencam.gif" tppabs="http://pbs.mcp.com/ebooks/0672312174/button/screencam.gif">
<P>
Start the ScreenCam "How to Create a Function Module"
now.
<OL>
<LI>From the ABAP/4 Development Workbench screen, press the Function
Library button on the Application toolbar. The Function Library
Initial Screen is shown (refer to Figure 19.3).
<LI>Type the name of your function module in the Function Module
field. The name must begin with<I> Y_</I> or <I>Z_</I>.
<LI>Press the Create button. The Function Module Create: Administration
screen is shown (see Figure 19.10).<BR>
<A HREF="javascript:popUp('f19-10.gif')"><B>Figure 19.10:</B> <I>This is the Function Module Create: Administration
screen</I>.</A>
<LI>Type the name of a function group in the Function Group field.
The function group name must be four characters long and must
begin with <I>Y</I> or <I>Z</I>.
<LI>Type an <B>S</B> in the Application field. This field is used
to indicate which function area uses the function module. Our
functionality is not used by any functional area, it is simply
an example, so any choice will do. (S indicates that the function
module contains functionality needed by Basis.)
<LI>Type a description of the function module in the Short Text
field. The contents of this field are seen when displaying a list
of function modules.
<LI>Press the Save button on the Application toolbar.
<LI>If the function group does not already exist, a pop-up informs
you of that fact and asks you if you want to create it. Press
the Yes button to create the function group. The Create Function
Group dialog box appears. Type a description in the Short Text
field and press the Save button. The Create Object Catalog Entry
screen appears. Press the Local Object button. You are returned
to the Function Module Change: Administration screen.
<LI>Press the Source Code button on the Application toolbar. The
Function Module Edit screen is displayed (refer to Figure 19.8).
<LI>Type the source code for your function module. Do not change
the system-generated comment lines under any circumstances! Your
function module might fail to operate if you do.
<LI>Press the Save button on the Application toolbar. The message
<TT>Program <I>xxxxx</I> saved</TT>
appears in the status bar at the bottom of the window.
<LI>Press the Back button on the Application toolbar. You are
returned to the Function Library Initial screen.
<LI>If you want to define import or export parameters, select
the Import/Export Parameter Interface radio button. Press the
Change pushbutton. You are shown the Import/Export Parameters
screen (refer to Figure 19.6). Type the names of your parameters
in the first column and enter any other desired characteristics
for each parameter. When you are finished, press the Save button
and then the Back button.
<LI>Finally, to activate your function module, press the Activate
button on the Application toolbar of the Function Library Initial
Screen.
</OL>
<P>
Now would be a good time to create an ABAP/4 program to call your
function module and try it out. Create a program like the one
shown in Listing 19.5. Within your program, be sure to code the
function module name in uppercase. If you don't, you will get
a short dump because the system won't be able to find the function
module.<P>
<CENTER>
<TABLE BORDER=1>
<TR VALIGN=TOP><TD WIDTH=600><B>TIP</B></TD></TR>
<TR VALIGN=TOP><TD WIDTH=600>
<BLOCKQUOTE>
Use the Object Browser to list the function modules you have created. From the Object browser, choose Local Priv. Objects and press the Display push-button.</BLOCKQUOTE>
</TD></TR>
</TABLE>
</CENTER>
<P>
<H2><A NAME="Summary"><FONT SIZE=5 COLOR=#FF0000>
Summary</FONT></A></H2>
<UL>
<LI>You can use the <TT>include</TT> statement to segregate your
code into smaller, more manageable chunks. Include programs have
a program type of <TT>i</TT>. Their code is copied into the including
program at the point of the <TT>include</TT> statement and completely
replaces it.
<LI>Function modules are modularization units with unique names
that can be called from any program in the entire R/3 system.
They hold code that is used by more than one program.
<LI>A function group is a collection of programs and has a predefined
structure. The names of all programs within the group all contain
the four-character function group ID.
<LI>The interface of a function module contains the definitions
for importing, exporting, and changing parameters, as well as
the documentation for exceptions raised within the function module.
<LI>Typed and untyped parameters can be specified and they can
be passed by reference, by value, or by value and result.
</UL>
<P>
<CENTER>
<TABLE BORDER=1>
<TR VALIGN=TOP><TD WIDTH=288><CENTER><B>DO</B></CENTER></TD><TD WIDTH=288><CENTER><B>DON'T</B></CENTER>
</TD></TR>
<TR VALIGN=TOP><TD WIDTH=288><B>DO</B> use pass-by-reference for import parameters. It is more efficient. Be careful when using it with export parameters. The values of by-reference parameters are changed immediately in the caller. If you change export parameters before you issue the raise statement, you will return but the values will already be changed in the calling program.
</TD><TD WIDTH=288><B>DON'T</B> change the system-generated comments at the top of the function module source code.
</TD></TR>
<TR VALIGN=TOP><TD WIDTH=288><B>DO</B> document all exceptions in the function module interface and document all parameters in the Documentation component of the function module.
</TD><TD WIDTH=288><B>DON'T</B> import values that you don't need from a function module. Simply omit unneeded imports from the call function statement.
</TD></TR>
<TR VALIGN=TOP><TD WIDTH=288><B>DO</B> save your test data so that you can retest your function module after modifications using the same data.
</TD><TD WIDTH=288><B>DON'T</B> use stop, exit, or check within a function module. (check and exit are okay if they are within a loop inside of the function module.)
</TD></TR>
</TABLE>
</CENTER>
<P>
<H2><A NAME="QampABR"><FONT SIZE=5 COLOR=#FF0000>
Q&A<BR>
</FONT></A></H2>
<TABLE>
<TR VALIGN=TOP><TD WIDTH=48><CENTER><B>Q</B></CENTER></TD><TD><B>How do I decide whether to use an <TT><B>include</B></TT>, an internal subroutine, an external subroutine, or a function module to implement my code?</B>
</TD></TR>
<TR VALIGN=TOP><TD WIDTH=48><CENTER><B>A</B></CENTER></TD><TD>If the code will not be used by any other programs, then use an internal subroutine. If the code might be useful to other programs, use a function module. You should not create external subroutines. They were only covered in the previous chapter so that you would know how to use them because there are still many in use in the R/3 system. It's also easier to understand function modules if you know how external subroutines work. Instead of external subroutines, use function modules. <TT>include</TT>s should be used to simplify your program structure and group similar components together. They should not be used as containers for reusable code that is included into multiple programs. For example, you might put all of your data declarations into one <TT>include</TT>, your events into another, your subroutines into a third, and your calls to function modules into a fourth.
</TD></TR>
<TR VALIGN=TOP><TD WIDTH=48><CENTER><B>Q</B></CENTER></TD><TD><B>How do I decide which function group to put a new function module into? When should I create a new one, and when should I reuse an existing one?</B>
</TD></TR>
<TR VALIGN=TOP><TD WIDTH=48><CENTER><B>A</B></CENTER></TD><TD>Put function modules that have similar functionality into the same function group. For example, you might put all of the function modules that calculate depreciation into one function group, all that perform calendar functions into another function group, and those that provide external communication interfaces into a third.
</TD></TR>
</TABLE>
<P>
<H2><A NAME="Workshop"><FONT SIZE=5 COLOR=#FF0000>
Workshop</FONT></A></H2>
<P>
The Workshop provides two ways for you to affirm what you've learned
in this chapter. The Quiz section poses questions to help you
solidify your understanding of the material covered and the Exercise
section provides you with experience in using what you have learned.
You can find answers to the quiz questions and exercises in Appendix
B, "Answers to Quiz Questions and Exercises."
<H3><A NAME="Quiz">
Quiz</A></H3>
<OL>
<LI>True or False: An import parameter that has a proposal is
always optional.
<LI>True or False: All export parameters are optional.
</OL>
<H3><A NAME="Exercise">
Exercise 1</A></H3>
<P>
Use the Test function within the Function Library to test the
function module <TT>POPUP_WITH_TABLE_DISPLAY</TT>. After testing
this function module, look at the following program and predict
its behavior.
<BLOCKQUOTE>
<PRE>
report ztx1910.
data: begin of it occurs 5,
data(10),
end of it,
result(10).
perform: fill_table tables it,
call_fm tables it changing result.
write: / 'Result=', result.
form fill_table tables it like it.
move 'Select' to it-data. append it.
move 'One' to it-data. append it.
move 'Option' to it-data. append it.
endform.
form call_fm tables it changing result.
call function 'POPUP_WITH_TABLE_DISPLAY'
importing
endpos_col = 30
endpos_row = 30
startpos_col = 1
startpos_row = 1
titletext = 'Look Ma, a Window'
exporting
choise = result
tables
valuetab = it.
endform.
</PRE>
</BLOCKQUOTE>
<P>
<CENTER>
<HR SIZE=4>
<A HREF="../ch18/ch18.htm" tppabs="http://pbs.mcp.com/ebooks/0672312174/ch18/ch18.htm"><IMG SRC="../button/previous.gif" tppabs="http://pbs.mcp.com/ebooks/0672312174/button/previous.gif" BORDER="0"></A>
<A HREF="../index.htm" tppabs="http://pbs.mcp.com/ebooks/0672312174/index.htm"><IMG SRC="../button/contents.gif" tppabs="http://pbs.mcp.com/ebooks/0672312174/button/contents.gif" BORDER="0"></A>
<A HREF="../ch20/ch20.htm" tppabs="http://pbs.mcp.com/ebooks/0672312174/ch20/ch20.htm"><IMG SRC="../button/next.gif" tppabs="http://pbs.mcp.com/ebooks/0672312174/button/next.gif" BORDER="0"></A>
</P>© <A HREF="../copy.htm" tppabs="http://pbs.mcp.com/ebooks/0672312174/copy.htm">Copyright</A>, Macmillan Computer Publishing. All rights reserved.
</CENTER>
</BODY>
</HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -