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

📄 ch18.htm

📁 这个是sap开发语言abap的教育文档
💻 HTM
📖 第 1 页 / 共 4 页
字号:
Listing 18.3 illustrates how to code these additions.
<P>
<IMG SRC="../button/input.gif" tppabs="http://pbs.mcp.com/ebooks/0672312174/button/input.gif">
<HR>
<P>
<B>Listing 18.3&nbsp;&nbsp;How to Code Parameter Additions<BR>
</B>
<BLOCKQUOTE>
<PRE>
1  report ztx1803.
2  data: f1 value 'A',
3        f2 value 'B',
4        f3 value 'C',
5        f4 value 'D',
6        f5 value 'E',
7        f6 value 'F'.
8
9  perform s1 using  f1 f2
10            changing  f3 f4.
11
12 perform s2 using  f1 f2 f3 f4
13            changing  f5 f6.
14
15 perform s3 using  f1 f2 f3.
16
17 form s1 using  p1 value(p2)
18         changing  p3 value(p4).
19     write: / p1, p2, p3, p4.
20     endform.
21
22 form s2 using  p1 value(p2) value(p3) p4
23         changing  value(p5) p6.
24     write: / p1, p2, p3, p4, p5, p6.
25     endform.
26
27 form s3 using  value(p1)
28         changing  p2 value(p3).
29     write: / p1, p2, p3.
30     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.3 produces the following output:
<BLOCKQUOTE>
<PRE>
A B C D
A B C D E F
A B C
</PRE>
</BLOCKQUOTE>
<P>
<IMG SRC="../button/analysis.gif" tppabs="http://pbs.mcp.com/ebooks/0672312174/button/analysis.gif">
<UL>
<LI>Line 9 passes four parameters to subroutine <TT>s1</TT>. The
syntax on line 17 determines how they are passed. <TT>f1</TT>
and <TT>f3</TT> are passed by reference; <TT>f2</TT> is passed
by value; <TT>f4</TT> is passed by value and result.
<LI>Line 9 passes six parameters to subroutine <TT>s2</TT>. <TT>f1</TT>,
<TT>f4</TT>, and <TT>f6</TT> are passed by reference; <TT>f2</TT>
and <TT>f3</TT> are passed by value. <TT>f5</TT> is passed by
value and result.
<LI>Line 12 passes three parameters to subroutine <TT>s2</TT>.
<TT>f3</TT> is passed by value, <TT>f3</TT> is passed by reference,
and <TT>f5</TT> is passed by value and result. Although the <TT>perform</TT>
statement only specifies <TT>using</TT>, the <TT>form</TT> statement
is allowed to differ. It specifies both <TT>using</TT> and <TT>changing</TT>.
The syntax on the <TT>form</TT> statement takes precedence and
determines the method by which the parameters are passed.
</UL>
<P>
Remember to keep the following things in mind:
<UL>
<LI><TT>perform</TT> and <TT>form</TT> must contain the same number
of parameters.
<LI>The syntax on the <TT>perform</TT> and <TT>form</TT> statements
can differ.
<LI>The syntax on the <TT>form</TT> statement alone determines
the method by which a param-eter is passed.
<LI>The <TT>value()</TT> addition cannot be used on the <TT>perform</TT>
statement.
<LI><TT>using</TT> must come before <TT>changing</TT>.
<LI>The addition <TT>using</TT> can only occur once in a statement.
The same rule applies to <TT>changing</TT>.
</UL>
<H3><A NAME="UsingtheMethodsbyWhichyouPassParameters">
Using the Methods by Which you Pass Parameters</A></H3>
<P>
Table 18.3 briefly describes the three methods of passing parameters.
<BR>
<P>
<CENTER><B>Table 18.3&nbsp;&nbsp;Methods of Passing Parameters</B></CENTER><CENTER>
<TABLE BORDER=1>
<TR VALIGN=TOP><TD WIDTH=192><CENTER><B>Method</B></CENTER></TD><TD WIDTH=192><CENTER><B>Description</B></CENTER>
</TD><TD WIDTH=192><CENTER><B>Advantages</B></CENTER></TD></TR>
<TR VALIGN=TOP><TD WIDTH=192>By reference</TD><TD WIDTH=192>Passes a pointer to the original memory location.
</TD><TD WIDTH=192>Very efficient</TD></TR>
<TR VALIGN=TOP><TD WIDTH=192>By value</TD><TD WIDTH=192>Allocates a new memory location for use within the subroutine. The memory is freed when the subroutine ends.
</TD><TD WIDTH=192>Prevents changes to passed variable</TD></TR>
<TR VALIGN=TOP><TD WIDTH=192>By value and result</TD><TD WIDTH=192>Similar to pass by value, but the contents of the new memory is copied back into the original memory before returning.
</TD><TD WIDTH=192>Allows changes and allows a rollback</TD></TR>
</TABLE>
</CENTER>
<P>
<H4>Passing Parameters by Reference</H4>
<P>
When you pass a parameter by reference, new memory is not allocated
for the value. Instead, a pointer to the original memory location
is passed. All references to the parameter are references to the
original memory location. Changes to the variable within the subroutine
update the original memory location immediately. Figure 18.1 and
Listing 18.4 illustrate how this works.
<P>
<A HREF="javascript:popUp('f18-1.gif')"><B>Figure 18.1 :</B> <I>How a parameter passed by reference affects
the original memory location</I>.</A>
<P>
<IMG SRC="../button/input.gif" tppabs="http://pbs.mcp.com/ebooks/0672312174/button/input.gif">
<HR>
<P>
<B>Listing 18.4&nbsp;&nbsp;Effect of Pass by Reference<BR>
</B>
<BLOCKQUOTE>
<PRE>
1  report ztx1804.
2  data f1 value 'A'.
3
4  perform s1 using f1.
5  write / f1.
6
7  form s1 using p1.
8      p1 = 'X'.
9      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.4 produces the following output:
<BLOCKQUOTE>
<PRE>
X
</PRE>
</BLOCKQUOTE>
<P>
<IMG SRC="../button/analysis.gif" tppabs="http://pbs.mcp.com/ebooks/0672312174/button/analysis.gif">
<UL>
<LI>Line 2 allocates memory for variable <TT>f1</TT>. For the
sake of this example, let's assume the memory location is 1000.
<LI>Line 4 transfers control to line 7.
<LI>Line 7 causes <TT>f1</TT> to be passed by reference. Therefore,
<TT>p1</TT> is a pointer to memory location 1000.
<LI>Line 8 modifies memory location 1000, causing the memory for
<TT>f1</TT> to change to <TT>X</TT>.
<LI>Line 9 returns control to line 5.
<LI>Line 5 writes out the value <TT>X</TT>.
</UL>
<P>
With internal subroutines, there is little difference between
passing parameters by reference and accessing global variables
from within the subroutine. Both allow you to change the value
of a global variable directly. In external subroutines and function
modules (see <A HREF="../ch19/ch19.htm" tppabs="http://pbs.mcp.com/ebooks/0672312174/ch19/ch19.htm" >Chapter 19</A>, &quot;Modularization: Function Modules,
Part 1&quot;) the pass by reference is more useful. Even so, passing
parameters to a subroutine-be it internal or external-is good
programming style. It makes maintenance easier and improves the
readability of your program.
<P>
The additions <TT>using f1</TT> and <TT>changing f1</TT> both
pass <TT>f1</TT> by reference-they are identical in function.
The reason they both exist is that-used properly-they can document
whether the subroutine will change a parameter or not.
<P>
Code <TT>changing</TT> with parameters, the subroutine changes.
You should code <TT>using</TT> with parameters that are not changed
by the subroutine. Listing 18.5 illustrates this point.
<P>
<IMG SRC="../button/input.gif" tppabs="http://pbs.mcp.com/ebooks/0672312174/button/input.gif">
<HR>
<P>
<B>Listing 18.5&nbsp;&nbsp;using and changing Are Identical in
Function<BR>
</B>
<BLOCKQUOTE>
<PRE>
1  report ztx1805.
2  data: f1 value 'A',
3        f2 value 'B'.
4
5  write: / f1, f2.
6  perform s1 using f1
7             changing f2.
8  write: / f1, f2.
9
10 form s1 using p1
11         changing p2.
12     p1 = p2 = 'X'.
13     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.5 produces the following output:
<BLOCKQUOTE>
<PRE>
A B
X X
</PRE>
</BLOCKQUOTE>
<P>
<IMG SRC="../button/analysis.gif" tppabs="http://pbs.mcp.com/ebooks/0672312174/button/analysis.gif">
<P>
<TT>f1</TT> and <TT>f2</TT> are both passed by reference to <TT>s1</TT>.
Therefore, <TT>p1</TT> and <TT>p2</TT> are pointers to <TT>f1</TT>
and <TT>f2</TT>. Changes to <TT>p1</TT> and <TT>p2</TT> are immediately
reflected in <TT>f1</TT> and <TT>f2</TT>. This example only illustrates
that it is possible to change any parameter that is passed by
reference. You should code your parameters so that <TT>using</TT>
and <TT>changing</TT> properly document your usage of those parameters.
<H4>Passing Parameters by Value</H4>
<P>
When you pass a parameter by value, new memory is allocated for
the value. This memory is allocated when the subroutine is called
and is freed when the subroutine returns. Therefore, references
to the parameter are thus <TT>references</TT> to a unique memory
area that is known only within the subroutine; the original memory
location is separate. The original is unchanged if you change
the value of the parameter. Figure 18.2 and Listing 18.6 illustrate
how this works.
<P>
<A HREF="javascript:popUp('f18-2.gif')"><B>Figure 18.2 :</B> <I>How a parameter passed by value allocates
a new storage location independent of the original</I>.</A>
<P>
<IMG SRC="../button/input.gif" tppabs="http://pbs.mcp.com/ebooks/0672312174/button/input.gif">
<HR>
<P>
<B>Listing 18.6&nbsp;&nbsp;Effect of Pass by Value<BR>
</B>
<BLOCKQUOTE>
<PRE>
1  report ztx1806.
2  data: f1 value 'A'.
3
4  perform s1 using f1.
5  write / f1.
6
7  form s1 using value(p1).
8      p1 = 'X'.
9      write / p1.
10     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.6 produces the following output:
<BLOCKQUOTE>
<PRE>
X
A
</PRE>
</BLOCKQUOTE>
<P>
<IMG SRC="../button/analysis.gif" tppabs="http://pbs.mcp.com/ebooks/0672312174/button/analysis.gif">
<UL>
<LI>Line 2 allocates memory for variable <TT>f1</TT>.
<LI>Line 4 transfers control to line 7.
<LI>Line 7 causes <TT>f1</TT> to be passed by value. Therefore,
<TT>p1</TT> refers to a new memory location that is independent
of <TT>f1</TT>. The value of <TT>f1</TT> is automatically copied
into the memory for <TT>p1</TT>.
<LI>Line 8 modifies the memory for <TT>p1</TT>. <TT>f1</TT> is
unchanged.
<LI>Line 9 writes out the value <TT>X</TT>.
<LI>Line 10 returns control to line 5.
<LI>Line 5 writes out the value <TT>A</TT>.
</UL>
<P>
Use pass by value when you need a local copy of a variable that
you can change without affecting the original. Pass by reference
is more efficient than pass by value. Use pass by reference unless
you need an independent local copy of the variable.
<H4>Passing Parameters by Value and Result</H4>
<P>
Pass by value and result is very similar to pass by value. Like
pass by value, a new memory area is allocated and it holds an
independent copy of the variable. It is freed when the subroutine
ends, and that is also when the difference occurs.
<P>
When the <TT>endform</TT> statement executes, it copies the value
of the local memory area back into the original memory area. Changes
to the parameter within the subroutine are reflected in the original,
but not until the subroutine returns.
<P>
This may seem like a small difference, but the difference become
greater. You can change whether the copy takes place or not.
<P>
The copy always takes place unless you leave the subroutine by
using one of two statements:
<UL>
<LI><TT>stop</TT>
<LI><TT>message e<I>nnn</I></TT>
</UL>
<P>
The <TT>stop</TT> statement terminates the subroutine and goes
directly to the <TT>end-of-selection</TT> event. If <TT>p1</TT>
was passed by value and result, changes to <TT>p1</TT> are discarded
before <TT>end-of-selection</TT> is triggered. In a sense, <TT>stop</TT>
behaves like a mini-rollback for value and result parameters.
When it is used inside of a subroutine, the <TT>stop</TT> statement
is usually preceded by a test for an abnormal condition within
the program. If the abnormal condition arises, <TT>stop</TT> is
executed. It discards the changes to value and result variables,
and triggers <TT>end-of-selection</TT>, where cleanup procedures
are then executed.
<P>
Use pass by value and result for parameters you want to change,
but there may be a possibility that you will want to discard the
changes if an abnormal condition should arise in your subroutine.
<P>
<CENTER>
<TABLE BORDER=1>
<TR VALIGN=TOP><TD WIDTH=600><B>NOTE</B></TD></TR>
<TR VALIGN=TOP><TD WIDTH=600>
<BLOCKQUOTE>
The <TT>message </TT>statement and its affect on subroutines is covered in <A HREF="../ch21/ch21.htm" tppabs="http://pbs.mcp.com/ebooks/0672312174/ch21/ch21.htm" >Chapter 21</A>, &quot;Selection Screens.&quot;
</BLOCKQUOTE>

</TD></TR>
</TABLE>
</CENTER>
<P>
<P>
Figure 18.3 and Listing 18.7 illustrate parameters passed by value
and result.
<P>
<A HREF="javascript:popUp('f18-3.gif')"><B>Figure 18.3 :</B> <I>How a parameter passed by value and result
allocates a new storage location independent of the original and
copies the value back in again</I>.</A>
<P>
<IMG SRC="../button/input.gif" tppabs="http://pbs.mcp.com/ebooks/0672312174/button/input.gif">
<HR>
<P>
<B>Listing 18.7&nbsp;&nbsp;Effect of Pass by Value and Result
<BR>
</B>
<BLOCKQUOTE>
<PRE>
1  report ztx1807.
2  data: f1 value 'A'.
3
4  perform: s1 changing f1,
5           s2 changing f1.
6
7  end-of-selection.
8      write: / 'Stopped. f1 =', f1.
9
10 form s1 changing value(p1).
11     p1 = 'B'.
12     endform.
13
14 form s2 changing value(p1).
15     p1 = 'X'.
16     stop.
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.7 produces the following output:
<BLOCKQUOTE>
<PRE>
Stopped. f1 = B
</PRE>
</BLOCKQUOTE>
<P>

⌨️ 快捷键说明

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