⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ch19.htm

📁 这个是sap开发语言abap的教育文档
💻 HTM
📖 第 1 页 / 共 4 页
字号:
Passing Parameters</A></H3>
<P>
The methods for passing parameters to function modules are very
similar to those for passing parameters to external subroutines.
<P>
By default:
<UL>
<LI>Import and export parameters are passed by value.
<LI>Changing parameters are passed by value and result.
<LI>Internal tables are passed by reference.
</UL>
<P>
You can cause import, export, and changing parameters to be passed
by reference by placing a tickmark in the Reference check box
on the Import/Export Parameters screen (refer to Figure 19.6).
<P>
<CENTER>
<TABLE BORDER=1>
<TR VALIGN=TOP><TD WIDTH=600><B>NOTE</B></TD></TR>
<TR VALIGN=TOP><TD WIDTH=600>
<BLOCKQUOTE>
The concept of an export parameter being passed by value is a little strange for most programmers. Think of it this way: When passed by value, the memory for an export parameter is defined within the function module. The <TT>endfunction </TT>statement copies the value of the export parameter from with-in the function module to the variable assigned to it in the calling program.
</BLOCKQUOTE>
<BLOCKQUOTE>
If the <TT>endfunction </TT>statement is not executed (for example, if you leave the function module abnormally via the <TT>raise </TT>statement), the value is not copied and the caller's variable remains unchanged.
</BLOCKQUOTE>

</TD></TR>
</TABLE>
</CENTER>
<P>
<H3><A NAME="UsingTypedandUntypedParameters">
Using Typed and Untyped Parameters</A></H3>
<P>
Like subroutines, function module parameters can be typed or untyped.
Typing works the same way for function modules as it does for
subroutines. Untyped parameters adopt all of their technical characteristics
from the passing parameter. For example, if the passed parameter
is type <TT>c</TT> length 12 and the function module parameter
is untyped, the function module parameter becomes type <TT>c</TT>
length 12.
<P>
Typed parameters for function modules follow the same rules as
typed parameters for subroutines. There are two ways of specifying
typed parameters in the Import/Export Parameters screen:
<UL>
<LI>By specifying an ABAP/4 data type in the Reference Type column
<LI>By specifying the name of a DDIC structure or component of
a DDIC structure in the Reference Field column
</UL>
<P>
You cannot use both of these methods at the same time for a single
parameter. You must use either one or the other.
<P>
Using the first method, simply specify an ABAP/4 data type in
the Reference Type column on the Import/Export Parameters screen.
For example, in Figure 19.8, <TT>p1</TT> and <TT>p2</TT> are typed
parameters that have an ABAP/4 data type of <TT>i</TT> (integer).
<TT>P3</TT> is also typed, having a data type of <TT>p</TT>. Type
<TT>i</TT> is a fixed length data type and so has a length of
<TT>4</TT>. Type <TT>p</TT> is a variable length data type, and
takes its length and number of decimal places from the parameter
that is passed to it.
<P>
Using the second method, enter the name of a Data Dictionary structure
in the Reference Field column. The parameters are defined as if
you had used the <TT>like</TT> addition on the <TT>form</TT> statement
in an external subroutine.
<P>
Parameters can also be made optional and given default values.
To make an import parameter optional, place a tickmark in the
Optional column (refer to Figure 19.7). If you make an import
parameter optional, you do not have to name it on the <TT>call
function</TT> statement when you call the function module. Export
parameters are always optional. By that, I mean that you are not
required to code any export parameters on the <TT>call function</TT>
statement. For example, if function module <TT>z_xyz</TT> returns
three export parameters, <TT>e1</TT>, <TT>e2</TT>, and <TT>e3</TT>,
and you only want <TT>e2</TT>, then only code <TT>e2</TT> on the
<TT>call function</TT> statement. Simply omit the rest.
<P>
To give an import parameter a default value, enter a value in
the Proposal column, making sure to enclose character values within
single quotes. <TT>sy</TT> variable names are also allowed. If
you specify a proposal, the system will automatically place a
tickmark in the Optional column.
<P>
All parameters that you define in the interface also appear at
the top of the source code within special comments. Each special
comment line begins with the characters <TT>*&quot;</TT>. Each
time you change the interface the system automatically updates
these special comments to reflect your changes.<P>
<CENTER>
<TABLE BORDER=1>
<TR VALIGN=TOP><TD WIDTH=600><B>CAUTION</B></TD></TR>
<TR VALIGN=TOP><TD WIDTH=600>
<BLOCKQUOTE>
Don't change or delete these comments; they are generated by the system. If you change them, the function module might not work.</BLOCKQUOTE>

</TD></TR>
</TABLE>
</CENTER>
<P>
<P>
In Figure 19.8, you can see that <TT>p1</TT>, <TT>p2</TT>, and
<TT>p3</TT> are passed by value.
<P>
<A HREF="javascript:popUp('f19-8.gif')"><B>Figure 19.8 :</B> <I>The system-generated comments at the top
of the source code</I>.</A>
<H2><A NAME="CallingFunctionModules"><FONT SIZE=5 COLOR=#FF0000>
Calling Function Modules</FONT></A></H2>
<P>
To call a function module, you code the <TT>call function</TT>
statement. The flow of control is within Listing 19.4.
<P>
<IMG SRC="../button/input.gif" tppabs="http://pbs.mcp.com/ebooks/0672312174/button/input.gif">
<HR>
<P>
<B>Listing 19.4&nbsp;&nbsp;This Program Performs a Simple Call
to a Function Module<BR>
</B>
<BLOCKQUOTE>
<PRE>
1  report ztx1904.
2 
3  write: / 'Before Call'.
4  call function 'Z_TX_1901'.
5  write: / 'After Call'.
</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.4 produces this output:
<BLOCKQUOTE>
<PRE>
Before Call     
Hi from Z_TX_1901
After Call      
</PRE>
</BLOCKQUOTE>
<P>
<IMG SRC="../button/analysis.gif" tppabs="http://pbs.mcp.com/ebooks/0672312174/button/analysis.gif">
<UL>
<LI>Line 4 transfers control to the beginning of function module
<TT>z_tx_1901</TT>. In this example, the function module exists
within the function group <TT>ztxa</TT>. <TT>saplztxa</TT> is
the main program of this function group and acts as the con-tainer
for <TT>z_tx_1901</TT>.
<LI>The code within the function module executes.
<LI>The last line of the function module returns control to the
<TT>call function</TT> statement. Processing continues with the
next statement after <TT>call function</TT>.
</UL>
<H3><A NAME="SyntaxforthecallfunctionStatement">
Syntax for the call function Statement</A></H3>
<P>
The following is the syntax for the <TT>call function</TT> statement.
<BLOCKQUOTE>
<PRE>
call function 'F'
    [exporting   p1 = v1 ... ]
    [importing   p2 = v2 ... ]
    [changing    p3 = v3 ... ]
    [tables      p4 = it ... ]
    [exceptions  x1 = n [others = n]].
</PRE>
</BLOCKQUOTE>
<P>
where:
<UL>
<LI><TT>F</TT> is the function module name.
<LI><TT><I>p1</I></TT> through
<TT><I>p4</I></TT> are parameter
names defined in the function module interface.
<LI><TT><I>v1</I></TT> through
<TT><I>v3</I></TT> are variable
or field string names defined within the calling program.
<LI><TT><I>it</I></TT><I> </I>is
an internal table defined within the calling program.
<LI><TT><I>n</I></TT><I> </I>is
any integer literal; <TT><I>n</I></TT>
cannot be a variable.
<LI><TT><I>x1</I></TT><I> </I>is
an exception name raised within the function module.
</UL>
<P>
The following points apply:
<UL>
<LI>All additions are optional.
<LI><TT>call function</TT> is a single statement. Do not place
periods or commas after parameters or exception names.
<LI>The function module name must be coded in uppercase. If it
is coded in lowercase, the function will not be found and a short
dump will result.
</UL>
<P>
Use the <TT>call function</TT> statement to transfer control to
a function module and specify parameters. Figure 19.9 illustrates
how parameters are passed to and received from the function module.
<P>
<A HREF="javascript:popUp('f19-9.gif')"><B>Figure 19.9 :</B> <I>This figure illustrates how parameters
are passed to and received from the function mod-ule</I>.</A>
<P>
On the <TT>call function</TT> statement, exports and imports are
from the point of view of the program. Values that are exported
by <TT>call function</TT> are imported by the function module.
Values exported from the function module are imported by the <TT>call
function</TT> statement.
<P>
On the left of the assignment operator (<TT><B>=</B></TT>)
is a list of the parameter names defined in the function module's
interface. On the right are variables defined within the calling
program. Assignments after <TT>exporting</TT> proceed from right-to-left.
Assignments after <TT>importing</TT> proceed from left-to-right.
Assignments after <TT>changing</TT> and <TT>tables</TT> are bi-directional.
Before the call, the assignment is from right-to-left. On return,
the return value is assigned from left-to-right.
<P>
Let's use Figure 19.9 as an example. When you call function module
<TT>Z_XXX</TT>, the value of variable <TT>v1</TT> is assigned
to parameter <TT>p1</TT> and the value of variable <TT>v2</TT>
is assigned to <TT>p2</TT>. Control then transfers to the function
module. On return, the value of <TT>p3</TT> is assigned to <TT>v3</TT>.
<P>
<CENTER>
<TABLE BORDER=1>
<TR VALIGN=TOP><TD WIDTH=600><B>NOTE</B></TD></TR>
<TR VALIGN=TOP><TD WIDTH=600>
<BLOCKQUOTE>
In this book, the term &quot;import parameters&quot; by itself (without reference to whether it is from the point of view of the program or function module) refers to parameters imported into the function module. The term &quot;export parameters&quot; without a reference refers to parameters exported from the function module.</BLOCKQUOTE>

</TD></TR>
</TABLE>
</CENTER>
<P>
<H3><A NAME="RunningaSampleFunctionModule">
Running a Sample Function Module</A></H3>
<P>
In this section I will illustrate a call to a sample function
module <TT>z_tx_div</TT>, which simply divides two numbers and
returns the result of the division.
<P>
Listing 19.5 calls <TT>z_tx_div</TT> and passes parameters. Figure
19.7 shows the Import/Export Parameters screen for this function
module, and Listing 19.6 shows the source code for it.
<P>
<IMG SRC="../button/input.gif" tppabs="http://pbs.mcp.com/ebooks/0672312174/button/input.gif">
<HR>
<P>
<B>Listing 19.5&nbsp;&nbsp;Using the call function Statement with
Parameters<BR>
</B>
<BLOCKQUOTE>
<PRE>
1  report ztx1905.
2  parameters: op1 type i default 2,   &quot;operand 1
3              op2 type i default 3.   &quot;operand 2
4  data rslt type p decimals 2.        &quot;result
5 
6  call function 'Z_TX_DIV'
7       exporting
8            p1      = op1
9            p2      = op2
10      importing
11           p3      = rslt.
12
13 write: / op1, '/', op2, '=', rslt.
</PRE>
</BLOCKQUOTE>
<HR>
<P>
<IMG SRC="../button/input.gif" tppabs="http://pbs.mcp.com/ebooks/0672312174/button/input.gif">
<HR>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -