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

📄 ch17.htm

📁 这个是sap开发语言abap的教育文档
💻 HTM
📖 第 1 页 / 共 4 页
字号:
</PRE>
</BLOCKQUOTE>
<P>
<IMG SRC="../button/analysis.gif" tppabs="http://pbs.mcp.com/ebooks/0672312174/button/analysis.gif">
<UL>
<LI>Line 5 transfers control to line 17.
<LI>Line 18 begins a loop. Line 19 exits the loop, not the subroutine.
<LI>Line 22 exits the subroutine. Control returns to line 6.
<LI>Line 6 transfers control to line 26.
<LI>Line 28 transfers control to line 27 4 times.
<LI>Line 32 leaves the subroutine. Control return to line 7.
<LI>Line 7 transfers control to line 36.
<LI>Line 39 transfers control directly to line 10.
<LI>The output from line 11 begins on a new line because a new
event has begun.
<LI>Line 13 exits the event and the list is shown.
</UL>
<H3><A NAME="DefiningGlobalandLocalVariables">
Defining Global and Local Variables</A></H3>
<P>
<IMG SRC="../button/newterm.gif" tppabs="http://pbs.mcp.com/ebooks/0672312174/button/newterm.gif">
<P>
A <I>global variable</I> is one that is defined outside of a subroutine
by using the <TT>tables</TT> or <TT>data</TT> statement. It can
be accessed from any point in the program, be it inside an event
or inside a subroutine. It is good programming practice to place
global variable definitions at the top of the program, somewhere
before the first line of executable code.
<P>
<IMG SRC="../button/newterm.gif" tppabs="http://pbs.mcp.com/ebooks/0672312174/button/newterm.gif">
<P>
A <I>local variable</I> is a variable that is defined inside a
subroutine using the <TT>local</TT>, <TT>data</TT>, or <TT>statics</TT>
statement. It is said to be local to the subroutine. Variables
defined by using <TT>local</TT> are accessible from outside the
subroutine; variables defined by using <TT>data</TT> or <TT>statics</TT>
are not. Thus if the subroutine calls another subroutine, variables
defined by using <TT>local</TT> are visible from within the called
subroutine-variables defined by using <TT>data</TT> or <TT>statics</TT>
are not.
<P>
For local variables defined by using <TT>local</TT> or <TT>data</TT>,
memory is allocated each time the subroutine is called. That memory
is freed when the subroutine ends, and so the values within it
are lost. For <TT>statics</TT>, the memory is retained. These
characteristics will be described in depth in this chapter.
<H4>Defining a <TT>tables</TT> Work Area</H4>
<P>
Variables defined by using the <TT>tables</TT> statement are always
global. Placing the <TT>tables</TT> statement at the top of a
program defines a global field string. Placing the same statement
inside a subroutine also defines a <TT>global</TT> field string
of that name. Therefore, you should not use the <TT>tables</TT>
statement inside a subroutine since the definition is always global;
global definitions should be placed at the top of your program.
<P>
To define a local table work area inside a subroutine, use <TT>local</TT>
instead of the <TT>tables</TT> statement. The syntax is the same
as <TT>tables</TT>, but it defines a local field string instead
of a global one. The variables defined by using <TT>local</TT>
are visible from within the subroutine and all subroutines it
calls.<P>
<CENTER>
<TABLE BORDER=1>
<TR VALIGN=TOP><TD WIDTH=600><B>NOTE</B></TD></TR>
<TR VALIGN=TOP><TD WIDTH=600>
<BLOCKQUOTE>
To be precise, a variable is only known within a program after the point at which it is defined. For example, if you define a variable on line 10, you would be able to access it on lines 11 and later, but not on lines before line 10. In the case of a <TT>local </TT>definition, you can access the global version of the variable at any point before the <TT>local </TT>definition.
</BLOCKQUOTE>

</TD></TR>
</TABLE>
</CENTER>
<P>
<P>
Listing 17.9 illustrates the <TT>local</TT> statement.
<P>
<IMG SRC="../button/input.gif" tppabs="http://pbs.mcp.com/ebooks/0672312174/button/input.gif">
<HR>
<P>
<B>Listing 17.9&nbsp;&nbsp;The local Statement<BR>
</B>
<BLOCKQUOTE>
<PRE>
1  report ztx1709.
2  tables ztxlfa1.
3
4  select single * from ztxlfa1 where lifnr = 'V9'.
5  write: / '*-----', ztxlfa1-lifnr.
6  perform s1.
7  write: / '*S1---', ztxlfa1-lifnr.
8  perform s2.
9  write: / '*S2---', ztxlfa1-lifnr.
10
11 form s1.
12     write: / ' S1-A', ztxlfa1-lifnr.
13     local ztxlfa1.
14     select single * from ztxlfa1 where lifnr = 'V1'.
15     write: / ' S1-B', ztxlfa1-lifnr.
16     perform s2.
17     write: / ' S1-C', ztxlfa1-lifnr.
18     endform.
19
20 form s2.
21     write: / '  S2-A', ztxlfa1-lifnr.
22     select single * from ztxlfa1 where lifnr = 'V2'.
23     write: / '  S2-B', ztxlfa1-lifnr.
24     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 17.9 produces the following output:
<BLOCKQUOTE>
<PRE>
* - - - - -V9
 S1-A V9
 S1-B V1
  S2-A V1
  S2-B V2
 S1-C V2
*S1 - - -V9
  S2-A V9
  S2-B V2
*S2 - - -V2
</PRE>
</BLOCKQUOTE>
<P>
<IMG SRC="../button/analysis.gif" tppabs="http://pbs.mcp.com/ebooks/0672312174/button/analysis.gif">
<UL>
<LI>Line 2 defines a global work area named <TT>ztxlfa1</TT>.
<LI>Line 4 selects a single record from table <TT>ztxlfa1</TT>
and places it into the global work area <TT>ztxlfa1</TT>.
<LI>Line 5 writes the value <TT>V9</TT> from the global work area
<TT>ztxlfa1</TT>.
<LI>Line 6 passes control to line 11.
<LI>Line 12 writes the value of <TT>lifnr</TT> from the global
work area <TT>ztxlfa1</TT>.
<LI>Line 13 defines a local field string <TT>ztxlfa1</TT>. (It
is good programming practice to place this definition at the top
of the subroutine. For the purposes of this example, it is placed
after the <TT>write</TT> to completely demonstrate the effect
of <TT>local</TT>.) A new memory location is allocated having
the name <TT>ztxlfa1</TT>. References to <TT>ztxlfa1</TT> after
this point in the subroutine and in all called subroutines will
refer to this local definition of <TT>ztxlfa1</TT>, not the global
one. The global one is now inaccessible.
<LI>Line 14 selects a single record and places it into the local
work area <TT>ztxlfa1</TT>.
<LI>Line 15 writes a value from the local work area. It is <TT>V1</TT>.
<LI>Line 16 transfers control to line 20. The local memory remains
allocated and will still be visible within <TT>s2</TT>.
<LI>Line 21 writes from the local memory area defined in <TT>s1</TT>.
<LI>Line 22 selects a new value into the local <TT>ztxlfa1</TT>.
<LI>Line 23 writes from the local <TT>ztxlfa1</TT>.
<LI>Line 24 returns control to line17. The local memory remains
allocated and unchanged.
<LI>Line 17 writes the value put into the local <TT>ztxlfa1</TT>
by <TT>s2</TT>.
<LI>Line 18 returns control to line 6. The local memory is freed,
and the value <TT>V2</TT> is discarded.
<LI>Line 7 writes the value of <TT>lifnr</TT>. It is now <TT>V9</TT>-the
same value it had when <TT>s1</TT> was called.
<LI>Line 8 transfers control to line 20.
<LI>Prior to executing line 21, a local definition of <TT>ztxlfa1</TT>
has not been created. Therefore, the global <TT>ztxlfa1-lifnr</TT>
is written out.
<LI>Line 22 selects a record into the global work area <TT>ztxlfa1</TT>.
<LI>Line 23 writes <TT>V2</TT> from the global work area.
<LI>Line 24 returns control to line 8.
<LI>Line 9 writes <TT>V2</TT> from the global work area.
</UL>
<P>
If you define a local variable having the same name as a global
variable, the local variable will take precedence inside the subroutine.
Consequently, you will not be able to access the global variable
of the same name within that subroutine. All references will refer
to the local variable.
<H4>Defining Data</H4>
<P>
Variables defined by the <TT>data</TT> statement at the top of
the program are global. <TT>data</TT> definitions within a subroutine
are local to the subroutine. Memory is allocated for these variables
when the subroutine is called, and freed when it returns. Like
variables defined by using <TT>local</TT>, the values in <TT>data</TT>
variables will be lost when the subroutine returns.
<P>
Use the <TT>statics</TT> statement<B> </B>to create local variables
that are not freed when the subroutine ends. The syntax for <TT>statics</TT>
is the same as the syntax for the <TT>data</TT> statement. The
memory for a static variable is allocated the first time the subroutine
is called, and retained when the subroutine ends. However, a static
variable is only visible from within the subroutine itself, not
within subroutines that it calls. The next time the subroutine
is called, the memory for that variable becomes visible again.
The value within it will be what it was when you last returned
from that subroutine.
<P>
The memory of a static variable belongs to the subroutine that
allocated it; that variable is not visible from other subroutines.
If you allocate a static variable of the same name in multiple
subroutines, these are separate variables with their own memory
and values.
<P>
Listing 17.10 illustrates variables defined by using <TT>data</TT>
and <TT>statics</TT>.
<P>
<IMG SRC="../button/input.gif" tppabs="http://pbs.mcp.com/ebooks/0672312174/button/input.gif">
<HR>
<P>
<B>Listing 17.10&nbsp;&nbsp;data and statics<BR>
</B>
<BLOCKQUOTE>
<PRE>
1  report ztx1710.
2  data: f1 type i value 8,
3        f2 type i value 9.
4
5  write: / 'before s1:', f1, f2.
6  do 3 times.
7      perform s1.
8      enddo.
9  write: / 'after  s1:', f1, f2.
10
11 form s1.
12     data    f1 type i value 1.
13     statics f2 type i value 1.
14     write: / 'inside s1:', f1, f2.
15     perform s2.
16     f1 = f2 = f2 * 2.
17     endform.
18
19 form s2.
20     write: ' S2-', f1, f2.
21     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 17.10 produces the following output:
<BLOCKQUOTE>
<PRE>
before s1:          8           9
inside s1:          1           1   S2-          8           9
inside s1:          1           2   S2-          8           9
inside s1:          1           4   S2-          8           9
after  s1:          8           9
</PRE>
</BLOCKQUOTE>
<P>
<IMG SRC="../button/analysis.gif" tppabs="http://pbs.mcp.com/ebooks/0672312174/button/analysis.gif">
<UL>
<LI>Lines 2 and 3 define global variables <TT>f1</TT> and <TT>f2</TT>.
<LI>Line 7 transfers control to line 11.
<LI>Line 12 allocates new memory for local variable <TT>f1</TT>
and assigns it a value of <TT>1</TT>.
<LI>Line 13 allocates new memory for static variable <TT>f2</TT>
and assigns it a value of <TT>1</TT>.
<LI>Line 15 transfers control to line 19.
<LI>Line 20 writes from the global variables <TT>f1</TT> and <TT>f2</TT>.
<LI>Line 21 returns control to line 15.
<LI>Line 16 multiplies <TT>f2</TT> by 2 and assigns the value
to both <TT>f1</TT> and <TT>f2</TT>.
<LI>Line 17 returns control to line 6. The memory for <TT>f1</TT>
is freed, but the memory for <TT>f2</TT> is retained. However,
it will not be visible again until the next time the subroutine
is called.
<LI>The loop loops and line 7 again transfers control to line
11.
<LI>Line 12 allocates new memory for <TT>f1</TT> and assigns it
a value of <TT>1</TT>.
<LI>Line 13 causes the previously allocated memory for <TT>f2</TT>
to again become visible with this subroutine. The value in <TT>f2</TT>
is <TT>2</TT>, as it was when the subroutine ended last.
</UL>
<H2><A NAME="Summary"><FONT SIZE=5 COLOR=#FF0000>
Summary</FONT></A></H2>
<UL>
<LI>ABAP/4 has three types of modularization units: events, subroutines,
and function modules.
<LI>Events exist in all programs. If none are coded, the default
event <TT>start-of-selection</TT> is automatically inserted. <TT>initialization</TT>
is often used to assign values to parameters that appear on a
selection screen. <TT>end-of-selection</TT> is for program cleanup,
summaries, or abnormal termination routines. Events are triggered
by the driver program. The driver always starts before your program
and controls when the events are executed. You cannot place a
condition around an event name. All events are triggered again
when the user returns from the list. However, the selection screen
variables are not repopulated from the program's data area.
<LI>Use <TT>exit</TT>, <TT>check</TT>, and <TT>stop</TT> to leave
an event.
</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>What happens if I have two events of the same name within the same program?</B>
</TD></TR>
<TR VALIGN=TOP><TD WIDTH=48><CENTER><B>A</B></CENTER></TD><TD>If you have coded two statements of the same name within the same program, when the event is triggered the system executes them both in the order in which they appear in your program.
</TD></TR>
<TR VALIGN=TOP><TD WIDTH=48><CENTER><B>Q</B></CENTER></TD><TD><B>How can I tell the difference between an event name and a statement?</B>
</TD></TR>
<TR VALIGN=TOP><TD WIDTH=48><CENTER><B>A</B></CENTER></TD><TD>If you see a single word followed by a period, that is usually an event. Beyond that, events don't have any identifiable characteristics other than their names. They are simply reserved words and to recognize one you must memorize their names, just as you memorize the keywords in statements.
</TD></TR>
<TR VALIGN=TOP><TD WIDTH=48><CENTER><B>Q</B></CENTER></TD><TD><B>Can I see the driver program? Can I choose the driver? Can I create a driver? What are the differences between the driver programs available?</B>
</TD></TR>
<TR VALIGN=TOP><TD WIDTH=48><CENTER><B>A</B></CENTER></TD><TD><IMG SRC="../button/newterm.gif" tppabs="http://pbs.mcp.com/ebooks/0672312174/button/newterm.gif">
<P>
The driver program is called a <I>logical database</I>. You can choose the driver program by filling in the logical database fields in the program attributes screen. If you don't specify one, a default is automatically assigned. The only time you would want to choose the driver is when you want it to read data from the database and supply it to your program automatically. The default logical databases do not read from the database-they only trigger events. Regardless of the driver you choose, the events are always triggered in the same relative order. Even if you create your own logical database program, you cannot change the relative sequence of events or their names, nor can you create new ones. To see a driver program, start from within the source code editor. Choose the menu path Utilities-&gt;Help On. Choose the Logical Database radio button and type a logical database id (<TT>KDF</TT>, for example). You will then see the structure of the logical database. Choose the menu path Goto-&gt;Database Program to see the driver program. Remember, the code shown within the driver in this chapter was pseudocode, so you won't see statements like trigger initialization in there. Logical databases are a complex topic and require many chapters to fully describe.
</TD></TR>
<TR VALIGN=TOP><TD WIDTH=48><CENTER><B>Q</B></CENTER></TD><TD><B>If I put a <TT><B>write</B></TT> statement in an event that comes before <TT><B>start-of-selection</B></TT>, it works but only when there isn't a selection screen. If there is a selection screen, I don't see the output. Why?</B>
</TD></TR>
<TR VALIGN=TOP><TD WIDTH=48><CENTER><B>A</B></CENTER></TD><TD>R/3 uses something called a <I>screen context</I> to accept input and to display output to you. You can think of the screen context as one screen. Each screen context is processed in two phases: the dialog phase and the list phase. The dialog phase always occurs first, followed by the list. The dialog phase is used to display input fields to the user, such as those generated by a parameters statement. The list phase is used to display the output of <TT>write</TT> statement to the user. Both phases use the same screen context, so they overwrite each other. The dialog phase, however, pauses for user input and so allows itself to be shown before the list is shown. So if you issue a <TT>write</TT> statement in the <TT>initialization</TT> event and if the selection screen is shown, the selection screen overwrites the output from the <TT>write</TT> and that output is lost. You then choose Execute on the selection screen, and, if <TT>write</TT> statements are then issued, the list overwrites the selection screen and you see the output. However, if you don't have a selection screen, the writes that occur in <TT>initialization</TT> can still be seen.
</TD></TR>
</TABLE>
<P>
<H2><A NAME="Workshop"><FONT SIZE=5 COLOR=#FF0000>
Workshop</FONT></A></H2>
<P>
The Workshop provides you 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>Does the <TT>write to</TT> variation of the <TT>write</TT>
statement trigger <TT>top-of-page</TT>?
<LI>Which events should you not use <TT>stop</TT> in?
<LI>At what point is a variable known within a program?
</OL>
<H3><A NAME="Exercise">
Exercise 1</A></H3>
<P>
Create a report that accepts an input parameter named <TT>p_land1</TT>.
Define it like <TT>ztxlfa1-land1</TT>. Fill this field with the
default value <TT>US</TT> at runtime by using the <TT>Initialization</TT>
event. Your main program should have a single statement to call
a subroutine that selects records from <TT>ztxlfa1</TT> where
the <TT>land1</TT> field equals the <TT>p_land1</TT> parameter.
Inside the <TT>select</TT>, call another subroutine to write the
record out. Use the <TT>top-of-page</TT> event to create the report
title. Write the value of the <TT>p_land1</TT> field in the title.
<P>
<CENTER>
<HR SIZE=4>

<A HREF="../ch16/ch16.htm" tppabs="http://pbs.mcp.com/ebooks/0672312174/ch16/ch16.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="../ch18/ch18.htm" tppabs="http://pbs.mcp.com/ebooks/0672312174/ch18/ch18.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 + -