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

📄 ch18.htm

📁 这个是sap开发语言abap的教育文档
💻 HTM
📖 第 1 页 / 共 4 页
字号:
4  tables ztxlfa1.
5  data: it like ztxlfa1 occurs 5.         "doesn't have a header line
6
7  select * up to 5 rows from ztxlfa1 into table it order by lifnr.
8  perform: s1 using it,
9           s2 using it,
10          s3 using it,
11          writeitout tables it.
12
13 end-of-selection.
14   write: / 'In End-Of-Selection'.
15   perform writeitout tables it.
16
17 form s1 using value(pt) like it. "pass by value
18 *                        you can also use: like it[].
19     data wa like line of pt.
20 *    you can also use:
21 *      data wa like ztxlfa1.
22 *      data wa like line of pt[].
23 *      local ztxlfa1.             "if you use this, the work area name
24 *                                 "will be ztxlfa1, not wa
25     read table pt into wa index 1.
26     wa-lifnr = 'XXX'.
27     modify pt from wa index 1.
28     endform.
29
30 form s2 using pt like it. "pass by reference
31     data wa like line of pt.
32     read table pt into wa index 2.
33     wa-lifnr = 'YYY'.
34     modify pt from wa index 2.
35     endform.
36
37 form s3 changing value(pt) like it. "pass by value and result
38     data wa like line of pt.
39     read table pt into wa index 3.
40     wa-lifnr = 'ZZZ'.
41     modify pt from wa index 3.
42     stop.
43     endform.
44
45 form writeitout tables it structure ztxlfa1.
46     loop at it.
47         write / it-lifnr.
48         endloop.
49     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.10 produces the following output:
<BLOCKQUOTE>
<PRE>
  In End-Of-Selection
  1000
  YYY
  1020
  1030
  1040
</PRE>
</BLOCKQUOTE>
<P>
<IMG SRC="../button/analysis.gif" tppabs="http://pbs.mcp.com/ebooks/0672312174/button/analysis.gif">
<UL>
<LI>Line 4 defines a global work area <TT>ztxlfa1</TT>. This work
area is a field string having the same structure as the DDIC table
<TT>ztxlfa1</TT>.
<LI>Line 5 defines internal table <TT>it</TT> without a header
line.
<LI>Line 7 populates the internal table with 5 rows from table
<TT>ztxlfa1</TT>.
<LI>Line 8 transfers control to line 17.
<LI>Line 17 causes the body of the internal table to be passed
by value and does not create a header line for it. Pass by value
causes a separate copy of the internal table to be created, so
<TT>pt</TT> refers to a local copy of the internal table. <TT>like</TT>
is needed to define <TT>pt</TT> as an internal table. Without
<TT>like</TT>, <TT>pt</TT> would be a simple variable and a syntax
error would occur.
<LI>Line 18 shows the other choice available for defining <TT>pt</TT>
as an internal table.
<LI>Line 19 defines a local work area <TT>wa</TT> that has the
same structure as a row of <TT>pt</TT>. Lines 20 through 24 show
the other choices available for defining a local work area for
<TT>pt</TT>.
<LI>Line 25 reads row 1 from <TT>pt</TT> and places it into <TT>wa</TT>.
<LI>Line 26 changes the value of <TT>lifnr</TT> in the work area
<TT>wa</TT>.
<LI>Line 27 overwrites row 1 from <TT>wa</TT>. Since the internal
table was passed by value, this modifies the contents of the local
copy. The original is unchanged.
<LI>Line 28 returns control to line 8. The memory for <TT>pt</TT>
is freed and the change is lost.
<LI>Line 9 transfers control to line 30.
<LI>Line 30 causes the body of the internal table to be passed
by reference and does not create a header line for it. <TT>pt</TT>
now refers to the original. <TT>like</TT> is needed to define
<TT>pt</TT> as an internal table. Without <TT>like</TT>, <TT>pt</TT>
would be a simple variable and a syntax error would occur.
<LI>Line 31 defines a local work area <TT>wa</TT> that has the
same structure as a row in <TT>pt</TT>.
<LI>Line 32 reads row 2 from <TT>pt</TT> and places it into <TT>wa</TT>.
<LI>Line 33 changes the value of <TT>lifnr</TT> in the work area
<TT>wa</TT>.
<LI>Line 34 overwrites row 2 from <TT>wa</TT>. Since the internal
table was passed by reference, this modifies the contents of the
original. The original is now changed.
<LI>Line 35 returns control to line 9.
<LI>Line 10 transfers control to line 37.
<LI>Line 37 causes the body of the internal table to be passed
by value and result and does not create a header line for it.
<TT>pt</TT> now refers to a copy of the original. <TT>like</TT>
is needed to define <TT>pt</TT> as an internal table. Without
<TT>like</TT>, <TT>pt</TT> would be a simple variable, and a syntax
error would occur.
<LI>Line 38 defines a local work area <TT>wa</TT> that has the
same structure as a row in <TT>pt</TT>.
<LI>Line 39 reads row 3 from <TT>pt</TT> and places it into <TT>wa</TT>.
<LI>Line 40 changes the value of <TT>lifnr</TT> in the work area
<TT>wa</TT>.
<LI>Line 41 overwrites row 3 from <TT>wa</TT>. Since the internal
table was passed by value and result, this modifies the contents
of the local copy. The original hasn't yet been changed.
<LI>Line 42 bypasses the <TT>endform</TT> statement-it transfers
control directly to line 13. This causes the local copy to be
discarded. If the <TT>stop</TT> statement had not been executed,
the <TT>endform</TT> would have caused the local copy to overwrite
the original, and the change would have been kept.
<LI>Line 15 transfers control to line 45.
<LI>Line 45 causes the internal table to be passed by reference
and a header line automatically created.
<LI>Lines 46 through 48 write out the contents of the internal
table. The only change is in row 2, and was performed in <TT>s2</TT>.
</UL>
<H2><A NAME="DefiningandCallingExternalSubroutines"><FONT SIZE=5 COLOR=#FF0000>
Defining and Calling External Subroutines</FONT></A></H2>
<P>
An <I>external subroutine</I> is one that resides in a different
program than the <TT>perform</TT> statement that calls it. Figure
18.4 illustrates an external subroutine.
<P>
<A HREF="javascript:popUp('f18-4.gif')"><B>Figure 18.4 :</B> <I>An illustration of an external subroutine</I>.</A>
<P>
When a <TT>perform</TT> calls an external subroutine
<UL>
<LI>The external program containing the subroutine is loaded.
<LI>The entire external program is checked for syntax.
<LI>Control transfers to the <TT>form</TT> in the external program.
<LI>The statements within the external subroutine are executed.
<LI>The <TT>endform</TT> transfers control back to the statement
following the <TT>perform</TT>.
</UL>
<P>
The fact that the syntax check occurs at runtime is important,
for the following two reasons:
<UL>
<LI>If the formal parameter is typed, a mismatched actual parameter
causes a runtime error instead of a syntax error.
<LI>A syntax error anywhere in the external program causes a runtime
error, whether it is inside or outside the external subroutine.
</UL>
<P>
External subroutines are very similar to internal subroutines:
<UL>
<LI>Both allow parameters to be passed.
<LI>Both allow typed formal parameters.
<LI>Both allow parameters to be passed by value, by value and
result, and by reference.
</UL>
<P>
Both allow local variable definitions.
<P>
Figure 18.5 illustrates the differences between internal and external
subroutines.
<P>
<A HREF="javascript:popUp('f18-5.gif')"><B>Figure 18.5 :</B> <I>The differences between internal and external
subroutines</I>.</A>
<P>
The following are differences between external and internal subroutines:
<UL>
<LI>A global variable defined by using <TT>data</TT> is known
only within the program that defines it. For example, in Figure
18.5 the <TT>data f1</TT> statement appears in both programs.
This defines two memory areas named <TT>f1</TT>. The <TT>f1</TT>
defined within <TT>ztx1811</TT> is only accessible from within
<TT>ztx1811</TT>. The <TT>f1</TT> defined within <TT>ztx1812</TT>
is only accessible from within <TT>ztx1812</TT>. All references
to <TT>f1</TT> within <TT>ztx1812</TT> are references to the <TT>f1</TT>
defined within <TT>ztx1812</TT>.
<LI>A global variable that has the same name in both programs
and is defined using the <TT>tables</TT> statement in both programs
is common to both programs. A change to this variable in one program
affects the other. In Figure 18.5, the memory area named <TT>ztxlfa1</TT>
is shared between both programs. Any change to that work area
is seen immediately by both programs.
</UL>
<P>
Listings 18.11 and 18.12 illustrate a call to an external subroutine.
<P>
<IMG SRC="../button/input.gif" tppabs="http://pbs.mcp.com/ebooks/0672312174/button/input.gif">
<HR>
<P>
<B>Listing 18.11&nbsp;&nbsp;Calling the Subroutine Shown in Listing
18.12<BR>
</B>
<BLOCKQUOTE>
<PRE>
1  report ztx1811.
2  tables ztxlfa1.
3  data   f1(3) value 'AAA'.
4
5  ztxlfa1-lifnr = '1000'.
6  perform s1(ztx1812).
7  write: / 'f1 =', f1,
8         / 'lifnr =', ztxlfa1-lifnr.
</PRE>
</BLOCKQUOTE>
<HR>
<P>
<IMG SRC="../button/input.gif" tppabs="http://pbs.mcp.com/ebooks/0672312174/button/input.gif">
<HR>
<P>
<B>Listing 18.12&nbsp;&nbsp;Subroutine Called from the Program
in Listing 18.11<BR>
</B>
<BLOCKQUOTE>
<PRE>
1  report ztx1812.
2  tables ztxlfa1.
3  data   f1(3).
4
5  form s1.
6      f1 = 'ZZZ'.
7      ztxlfa1-lifnr = '9999'.
8      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.11 produces the following output:
<BLOCKQUOTE>
<PRE>
    f1 = AAA      
 lifnr = 9999
</PRE>
</BLOCKQUOTE>
<P>
<IMG SRC="../button/analysis.gif" tppabs="http://pbs.mcp.com/ebooks/0672312174/button/analysis.gif">
<UL>
<LI>In both programs, line 2 defines a global work area <TT>ztxlfa1</TT>.
This work area is shared between both programs.
<LI>In both programs, line 3 defines a global work variable <TT>f1</TT>.
Two independent memory areas are defined, one for each program.
<LI>In <TT>ztx1811</TT>, line 5 assigns a value of 1000 to <TT>ztxlfa1-lifnr</TT>.
<LI>In <TT>ztx1811</TT>, line 6 passes control to line 5 in <TT>ztx1812</TT>.
<LI>In <TT>ztx1812</TT>, line 6 assigns <TT>ZZZ</TT> to the global
variable <TT>f1</TT> for <TT>ztx1812</TT>. <TT>f1</TT> in <TT>ztx1811</TT>
is unaffected.
<LI>In <TT>ztx1812</TT>, line 7 assigns <TT>9999</TT> to <TT>ztxlfa1</TT>.
This affects both programs.
<LI>In <TT>ztx1812</TT>, line 8 returns control to line 6 in <TT>ztx1811</TT>.
<LI>In <TT>ztx1811</TT>, lines 7 and 8 write out the values of
<TT>f1</TT> and <TT>ztxlfa1-lifnr</TT>. <TT>f1</TT> is unchanged,
and <TT>lifnr</TT> has been changed.
</UL>
<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 <TT>local </TT>statement within external subroutines to avoid sharing tables work areas.
</BLOCKQUOTE>

</TD></TR>
</TABLE>
</CENTER>
<P>
<H2><A NAME="Summary"><FONT SIZE=5 COLOR=#FF0000>
Summary</FONT></A></H2>
<UL>
<LI>Parameters can be passed in three ways: by value, by value
and results, and by reference. The syntax on the <TT>form</TT>
statement determines which method is used.
<LI>The body of an internal table can be passed via <TT>using</TT>
or <TT>changing</TT>. The body (and header line, if present) can
be passed via <TT>tables</TT>. If the internal table doesn't have
a header line, <TT>tables</TT> creates one inside the subroutine.
<LI>To access the components of a field string or subroutine known
within the subroutine, the structure must be declared on the <TT>form</TT>
statement.
<LI>External subroutines are very similar to internal subroutines.
With external subroutines, variables defined by using <TT>data</TT>
are known only within the program that defines them. Variables
having the same name that are defined by using <TT>tables</TT>
are common to both programs.
</UL>
<H2><A NAME="QampABR"><FONT SIZE=5 COLOR=#FF0000>
Q&amp;A<BR>
</FONT></A></H2>

<TABLE>
<TR VALIGN=TOP><TD WIDTH=48><CENTER><B>Q</B></CENTER></TD><TD><B>Why do I need to know all of the variations of passing an internal table with header line and without? Don't just a few of these variations cover all circumstances?</B>
</TD></TR>
<TR VALIGN=TOP><TD WIDTH=48><CENTER><B>A</B></CENTER></TD><TD>If you were merely creating programs in ABAP/4, you wouldn't need to know all of these variations. You could code most of your programs with just a few simple variations. However, the majority of an ABAP/4 programmer's time is not usually spent creating ABAP/4 code-it is spent reading it. Passing internal tables to subroutines is a sticky subject, and 80 percent of your time will be spent reading code that does these sticky things-less than 20 percent of your time will be spent creating your own code. This sad imbalance dictates that you must learn the variations used by SAP, so you can spend less time pulling your hair out while trying to understand the SAP code. This will leave you with more time for writing your own code, and more hair.
</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, &quot;Answers to Quiz Questions and Exercises.&quot;
<H3><A NAME="Quiz">
Quiz</A></H3>
<OL>
<LI>What are parameters that appear on the <TT>form</TT> statement
called?
<LI>What does passing a variable of the wrong data type or length
to a typed parameter cause?
<LI>When you pass a parameter by reference, is new memory allocated
for the value?
<LI>What effect do the additions <TT>using f1</TT> and <TT>changing
f1</TT> have on the way parameters are passed?
</OL>
<H3><A NAME="Exercise">
Exercise 1</A></H3>
<P>
Run the program shown in Listing 18.13. Comment out the parameters
statement and run it again. Describe the sequence of events that
occur in each case.
<P>
<IMG SRC="../button/input.gif" tppabs="http://pbs.mcp.com/ebooks/0672312174/button/input.gif">
<HR>
<P>
<B>Listing 18.13&nbsp;&nbsp;Explain the Sequence of Events That
Occurs in this Program<BR>
</B>
<BLOCKQUOTE>
<PRE>
report ztx1813 no standard page heading.
ztx1813 data: flag,
      ctr type i.

parameters p1.

initialization.
  flag = 'I'.
  write: / 'in Initialization'.

start-of-selection.
  flag = 'S'.
  write: / 'in Start-Of-Selection',
         / 'p1 =', p1.

top-of-page.
   add 1 to ctr.
   write: / 'Top of page, flag =', flag, 'ctr =', ctr.
   uline.
</PRE>
</BLOCKQUOTE>
<P>
<CENTER>
<HR SIZE=4>

<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> 

</P>&#169; <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 + -