📄 ch18.htm
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<HTML>
<HEAD>
<SCRIPT LANGUAGE="JavaScript">
<!--
function popUp(pPage) {
popUpWin = window.open('','popWin','resizable=yes,scrollbars=no,width=525,height=394');
figDoc= popUpWin.document;
zhtm= '<HTML><HEAD><TITLE>' + pPage + '</TITLE>';
zhtm += '</head>';
zhtm += '<BODY bgcolor="#FFFFFF">';
zhtm += '<IMG SRC="' + pPage + '">';
zhtm += '<P><B>' + pPage + '</B>';
zhtm += '</BODY></HTML>';
figDoc.write(zhtm);
figDoc.close();
popUpWin.focus();
}
//-->
</SCRIPT>
<META NAME="GENERATOR" Content="Symantec Visual Page 1.0.1">
<META HTTP-EQUIV="Content-Type" CONTENT="text/html;CHARSET=iso-8859-1">
<TITLE>Sams Teach Yourself ABAP/4® in 21 Days -- Day 18- Modularization: Passing Parameters to Subroutines</TITLE>
</HEAD>
<BODY TEXT="#000000" BGCOLOR="#FFFFFF">
<CENTER>
<H1><IMG SRC="../button/sams.gif" tppabs="http://pbs.mcp.com/ebooks/0672312174/button/sams.gif" BORDER="0"></H1>
</CENTER>
<CENTER>
<P><A HREF="../ch17/ch17.htm" tppabs="http://pbs.mcp.com/ebooks/0672312174/ch17/ch17.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="../ch19/ch19.htm" tppabs="http://pbs.mcp.com/ebooks/0672312174/ch19/ch19.htm"><IMG SRC="../button/next.gif" tppabs="http://pbs.mcp.com/ebooks/0672312174/button/next.gif" BORDER="0"></A>
<H1>Sams Teach Yourself ABAP/4<sup>®</sup> in 21 Days</H1></CENTER>
<HR SIZE=4>
<H1>Day 18</H1>
<H1>Modularization: Passing Parameters to Subroutines</H1>
<UL>
<LI><A HREF="#PassingParameters">
Passing Parameters</A>
<UL>
<LI><A HREF="#CreatingTypedParameters">
Creating Typed Parameters</A>
<LI><A HREF="#ControllinghowParametersarePassed">
Controlling how Parameters are Passed</A>
<LI><A HREF="#UsingtheMethodsbyWhichyouPassParameters">
Using the Methods by Which you Pass Parameters</A>
<LI><A HREF="#PassingInternalTablesasParameters">
Passing Internal Tables as Parameters</A>
</UL>
<LI><A HREF="#DefiningandCallingExternalSubroutines">
Defining and Calling External Subroutines</A>
<LI><A HREF="#Summary">
Summary</A>
<LI><A HREF="#QampABR">
Q&A<BR>
</A>
<LI><A HREF="#Workshop">
Workshop</A>
<UL>
<LI><A HREF="#Quiz">
Quiz</A>
<LI><A HREF="#Exercise">
Exercise 1</A>
</UL></UL>
<HR>
<P>
After you have completed this chapter, you will be able to
<UL>
<LI>Pass typed and untyped parameters to subroutines
<LI>Pass parameters three ways: by reference, by value, and by
value and result
<LI>Pass field strings and internal tables to a subroutine
<LI>Understand how variables are shared between internal and external
subroutines
</UL>
<H2><A NAME="PassingParameters"><FONT SIZE=5 COLOR=#FF0000>
Passing Parameters</FONT></A></H2>
<P>
In addition to defining variables by using <TT>tables</TT>, <TT>local</TT>,
<TT>data</TT>, and <TT>statics</TT>, variables can also be defined
on the <TT>form</TT> statement itself. These are known as parameters.
Parameters can be either local or references to global variables.
The memory for local parameters is allocated when the subroutine
is called and freed when it ends.
<P>
If you define variables on the <TT>form</TT> statement, the <TT>perform</TT>
statement must pass a value to each of these variables.
<P>
<IMG SRC="../button/newterm.gif" tppabs="http://pbs.mcp.com/ebooks/0672312174/button/newterm.gif">
<P>
Parameter names that appear on the <TT>form</TT> statement are
called <I>formal</I> <I>parameters</I>. This term is easy to remember
because "formal" starts with "form." For example,
in the statement <TT>form s1 using p1 changing p2 p3</TT>, the
parameters <TT>p1</TT>, <TT>p2</TT>, and <TT>p3</TT> are called
formal parameters.
<P>
<IMG SRC="../button/newterm.gif" tppabs="http://pbs.mcp.com/ebooks/0672312174/button/newterm.gif">
<P>
Parameter names that appear on the <TT>perform</TT> statement
are called <I>actual parameters</I>. For example, in the statement
<TT>perform s1 using f1 changing f2 f3</TT>, the parameters <TT>f1</TT>,
<TT>f2</TT>, and <TT>f3</TT> are called actual parameters.
<P>
Defining variables as parameters is illustrated in Listing 18.1.
<P>
<IMG SRC="../button/input.gif" tppabs="http://pbs.mcp.com/ebooks/0672312174/button/input.gif">
<HR>
<P>
<B>Listing 18.1 Passing Parameters to a Subroutine
<BR>
</B>
<BLOCKQUOTE>
<PRE>
1 report ztx1801.
2 data: f1 value 'A',
3 f2 value 'B',
4 f3 value 'C'.
5
6 perform: s1 using f1 f2 f3,
7 s2 using f1 f2 f3.
8
9 form s1 using p1 p2 p3.
10 write: / f1, f2, f3,
11 / p1, p2, p3.
12 endform.
13
14 form s2 using f1 f2 f3.
15 skip.
16 write: / f1, f2, f3.
17 endform.
</PRE>
</BLOCKQUOTE>
<HR>
<P>
<IMG SRC="../button/output.gif" tppabs="http://pbs.mcp.com/ebooks/0672312174/button/output.gif">
<P>
The code in Listing 18.1 produces the following output:
<BLOCKQUOTE>
<PRE>
A B C
A B C
A B C
</PRE>
</BLOCKQUOTE>
<P>
<IMG SRC="../button/analysis.gif" tppabs="http://pbs.mcp.com/ebooks/0672312174/button/analysis.gif">
<UL>
<LI>Line 6 transfers control to line 9.
<LI>Line 9 defines three variables: <TT>p1</TT>, <TT>p2,</TT>
and <TT>p3</TT>. It assigns the value of <TT>f1</TT> to <TT>p1</TT>,
the value of <TT>f2</TT> to <TT>p2</TT>, and the value of <TT>f3</TT>
to <TT>p3</TT>.
<LI>Line 10 writes the values of the variables <TT>p1</TT>, <TT>p2</TT>,
and <TT>p3</TT> and the global variables <TT>f1</TT>, <TT>f2</TT>,
and <TT>f3</TT>.
<LI>Line 12 returns control to line 6.
<LI>Line 7 transfers control to line 14.
<LI>Line 14 defines three variables: <TT>f1</TT>, <TT>f2</TT>,
and <TT>f3</TT>. It assigns the value of <TT>f1</TT> to <TT>f1</TT>,
the value of <TT>f2</TT> to <TT>f2</TT>, and the value of <TT>f3</TT>
to <TT>f3</TT>.
<LI>Line 16 writes the values of the variables defined on line
14 on the <TT>form</TT> statement: <TT>f1</TT>, <TT>f2</TT>, and
<TT>f3</TT>.
<LI>Line 17 returns control to line 7.
</UL>
<P>
Please review the syntax for the <TT>form</TT> and <TT>perform</TT>
statements before continuing. See the section "Defining and
Calling Internal Subroutines" in <A HREF="../ch19/ch19.htm" tppabs="http://pbs.mcp.com/ebooks/0672312174/ch19/ch19.htm" >Chapter 19</A>.<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> used typed formal parameters-they are more efficient than untyped parameters.
</TD><TD WIDTH=288><B>DON'T</B> pass internal tables by value or by value and result unless you really need a new copy of the internal table. Pass para-meters by reference whenever possible.
</TD></TR>
</TABLE>
</CENTER>
<P>
<H3><A NAME="CreatingTypedParameters">
Creating Typed Parameters</A></H3>
<P>
<IMG SRC="../button/newterm.gif" tppabs="http://pbs.mcp.com/ebooks/0672312174/button/newterm.gif">
<P>
Formal parameters can be either typed or untyped. A <I>typed parameter</I>
is a formal parameter that has a data type following its name
on the <TT>form</TT> statement. An <I>untyped parameter</I> is
a formal parameter that doesn't have a data type following its
definition on the <TT>form</TT> statement. In Listing 18.1, all
parameters were untyped.
<P>
Untyped formal parameters allow you to pass a variable of any
data type or length to it. The formal parameter uses the attributes
of the actual parameter. For example, if you pass a four-byte
integer to an untyped formal parameter <TT>p1</TT>, <TT>p1</TT>
becomes a four-byte integer. If you pass a character string length
3 to the same parameter, it will become character 3.
<H4>Syntax for Typed Parameters</H4>
<BLOCKQUOTE>
<PRE>
form <I>s1</I> using <I>u1</I> type <I>t</I> value(<I>u2</I>) type <I>t
</I> changing <I>c1</I> type <I>t</I> value(<I>c2</I>) type <I>t</I>.
</PRE>
</BLOCKQUOTE>
<P>
where:
<UL>
<LI><TT>s1</TT> is a subroutine name.
<LI><TT>u1</TT>,<I> </I><TT>u2</TT>,<I> </I><TT>c1</TT>,<I> </I>and<I>
</I><TT>c2</TT> are formal parameters.
<LI><TT><I>t</I></TT> is either
an ABAP/4 data type or a user-defined data type.
</UL>
<P>
The following points apply:
<UL>
<LI>Only a data type can be specified on the <TT>form</TT> statement.
A length cannot be specified.
<LI>If you define a formal parameter using a fixed-length data
type (those are types <TT>d</TT>, <TT>t</TT>, <TT>i</TT>, and
<TT>f</TT>), the length of the actual parameter must match the
data type of the formal parameter. This is usually the case, since
you will usually design your program such that the data types
of the actual and formal parameters will match. You usually do
not pass one data type to a different data type.
</UL>
<P>
Passing a variable of the wrong data type or length to a typed
parameter causes a syntax error. Listing 18.2 shows how to use
typed parameters.
<P>
<IMG SRC="../button/input.gif" tppabs="http://pbs.mcp.com/ebooks/0672312174/button/input.gif">
<HR>
<P>
<B>Listing 18.2 Using Typed Parameters<BR>
</B>
<BLOCKQUOTE>
<PRE>
1 report ztx1802.
2 data: f1 value 'A',
3 f2 type i value 4,
4 f3 like sy-datum,
5 f4 like sy-uzeit.
6
7 f3 = sy-datum.
8 f4 = sy-uzeit.
9
10 perform s1 using f1 f2 f3 f4.
11
12 form s1 using p1 type c
13 p2 type i
14 p3 type d
15 p4 type t.
16 write: / p1,
17 / p2,
18 / p3,
19 / p4.
20 endform.
</PRE>
</BLOCKQUOTE>
<HR>
<P>
<IMG SRC="../button/output.gif" tppabs="http://pbs.mcp.com/ebooks/0672312174/button/output.gif">
<P>
The code in Listing 18.2 produces the following output:
<BLOCKQUOTE>
<PRE>
AAA
4
19980510
164836
</PRE>
</BLOCKQUOTE>
<P>
<IMG SRC="../button/analysis.gif" tppabs="http://pbs.mcp.com/ebooks/0672312174/button/analysis.gif">
<UL>
<LI>Lines 2 through 5 define four variables having various data
types. <TT>F3</TT> is type <TT>c</TT> length 8, with an output
length of 10 (defined in the domain for <TT>syst-datum</TT>).
<TT>F4</TT> is type <TT>c</TT> length 6 with an output length
of 8 (defined in the domain for <TT>sy-uzeit</TT>).
<LI>Line 10 transfers control to line 12.
<LI>On line 12, <TT>p1</TT> accepts only actual parameters of
type <TT>c</TT>. <TT>f1</TT> is type <TT>c</TT> length 3, so a
length of 3 is assigned to <TT>p1</TT>. If <TT>f1</TT> had not
been type <TT>c</TT> a syntax error would have occurred. <TT>p2</TT>
is a fixed-length data type; type <TT>i</TT> is always length
4; <TT>f2</TT> is also, so the parameters match fully. <TT>p3</TT>
and <TT>p4</TT> are also fixed-length data types, as are their
actual parameters.
<LI>Lines 16 through 19 write out the values of <TT>p1</TT> through
<TT>p4</TT>. Notice the output length is not passed from the actual
to the formal parameter. The output length of the formal parameters
is set to the default for each data type. This causes the date
and time to be output without separators.
</UL>
<P>
Typed parameters have three advantages:
<UL>
<LI>They are more efficient. Less CPU is needed to allocate memory
for a typed parameter than an untyped one.
<LI>They help prevent coding errors. Because you cannot pass a
parameter of an incompatible type, the syntax checker will point
out the error to you if you try to pass an incompatible parameter.
<LI>They help prevent runtime errors. For example, if your program
accepts an untyped variable and performs an arithmetic operation
on it, it is possible to pass character data to that subroutine.
If this happens at runtime, a short dump will result.
</UL>
<H4>Passing Field Strings</H4>
<P>
You can pass a field string the same way as any other parameter.
However, if you want to access the components of the field string
within the subroutine, you must make the structure of the field
string known to the subroutine via one of two additions to the
<TT>form</TT> statement:
<UL>
<LI><TT>like <I>x</I></TT>
<LI><TT>structure <I>x</I></TT>
</UL>
<P>
Here, <TT><I>x</I></TT> can be
a field string or a DDIC structure or table. For example, <TT>form
s1 using fs1 structure ztxlfa1</TT> defines <TT>fs1</TT> as having
the structure of DDIC table <TT>ztxlfa1</TT>.
<H3><A NAME="ControllinghowParametersarePassed">
Controlling how Parameters are Passed</A></H3>
<P>
There are three ways of passing parameters to a subroutine:
<UL>
<LI>Pass by reference
<LI>Pass by value
<LI>Pass by value and result
</UL>
<P>
The syntax on the <TT>form</TT> statement determines how variables
are passed. The syntax on <TT>perform</TT> does not. What this
means will be explained in the next section. The first thing needed
is to learn how to code each method.
<P>
Table 18.2 shows the relationship between syntax and passing methods.
<BR>
<P>
<CENTER><B>Table 18.2 form Statement Additions and
the Resulting Parameter Passing Method</B></CENTER><CENTER>
<TABLE BORDER=1>
<TR VALIGN=TOP><TD WIDTH=192><CENTER><B>Addition</B></CENTER></TD><TD WIDTH=192><CENTER><B>Method</B></CENTER>
</TD></TR>
<TR VALIGN=TOP><TD WIDTH=192><TT>using <I>v1</I></TT>
</TD><TD WIDTH=192>Pass by reference</TD></TR>
<TR VALIGN=TOP><TD WIDTH=192><TT>changing <I>v1</I></TT>
</TD><TD WIDTH=192>Pass by reference</TD></TR>
<TR VALIGN=TOP><TD WIDTH=192><TT>using value(<I>v1)</I></TT>
</TD><TD WIDTH=192>Pass by value</TD></TR>
<TR VALIGN=TOP><TD WIDTH=192><TT>changing value(<I>v1</I>)</TT>
</TD><TD WIDTH=192>Pass by value and result</TD></TR>
</TABLE>
</CENTER>
<P>
<P>
Although the syntax on <TT>form</TT> and <TT>perform</TT> can
differ, for the sake of program clarity they should be the same.
<P>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -