📄 ch17.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 17- Modularization: Events and 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="../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>
<H1>Sams Teach Yourself ABAP/4<sup>®</sup> in 21 Days</H1></CENTER>
<HR SIZE=4>
<H1>Day 17</H1>
<H1>Modularization: Events and Subroutines</H1>
<UL>
<LI><A HREF="#ModularizationUnitsinABAP">
Modularization Units in ABAP/4</A>
<LI><A HREF="#Events">
Events</A>
<UL>
<LI><A HREF="#DefiningEvents">
Defining Events</A>
<LI><A HREF="#Defaultingtostartofselection">
Defaulting to start-of-selection</A>
<LI><A HREF="#LeavinganEvent">
Leaving an Event</A>
<LI><A HREF="#ReturningfromtheList">
Returning from the List</A>
</UL>
<LI><A HREF="#Subroutines">
Subroutines</A>
<UL>
<LI><A HREF="#DefiningandCallingInternalSubroutines">
Defining and Calling Internal Subroutines</A>
<LI><A HREF="#LeavingaSubroutine">
Leaving a Subroutine</A>
<LI><A HREF="#DefiningGlobalandLocalVariables">
Defining Global and Local Variables</A>
</UL>
<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>Know the types of modularization units available in ABAP/4
<LI>Understand ABAP/4 events
<LI>Use the <TT>initialization</TT>, <TT>start-of-selection</TT>,
and <TT>end-of-selection</TT> events
<LI>Define internal and external subroutines
<LI>Define global, local, and static variables and table work
areas
<LI>Understand the effect of <TT>exit</TT>, <TT>check</TT>, and
<TT>stop</TT> within events and subroutines
</UL>
<H2><A NAME="ModularizationUnitsinABAP"><FONT SIZE=5 COLOR=#FF0000>
Modularization Units in ABAP/4</FONT></A></H2>
<P>
<IMG SRC="../button/newterm.gif" tppabs="http://pbs.mcp.com/ebooks/0672312174/button/newterm.gif">
<P>
A <I>modularization unit</I> is a like a shell into which you
can put code. It allows you to segregate a group of lines of code
from the rest, and then execute them at a specific time. The lines
of code within a modularization unit act very much like a mini-program
that can be called from another program.
<P>
ABAP/4 offers three types of modularization units:
<UL>
<LI>Events
<LI>Subroutines
<LI>Function modules
</UL>
<P>
This chapter explains events and subroutines. The next chapter
explains function modules.
<P>
Use modularization units to eliminate redundant code within your
program and to make your program easier to read. For example,
suppose you have a series of statements that format a mailing
address, and you need to format mailing addresses at several different
places in your program. Instead of duplicating the code within
your program, it's a good idea to place that code into a modularization
unit and call it whenever you need to format an address.
<H2><A NAME="Events"><FONT SIZE=5 COLOR=#FF0000>
Events</FONT></A></H2>
<P>
Contrary to first appearances, ABAP/4 programs are event-driven.
A good understanding of events are the key to a good understanding
of ABAP/4.
<H3><A NAME="DefiningEvents">
Defining Events</A></H3>
<P>
<IMG SRC="../button/newterm.gif" tppabs="http://pbs.mcp.com/ebooks/0672312174/button/newterm.gif">
<P>
An <I>event</I> is a tag that identifies a section of code. The
section of code associated with an event begins with an event
name and ends when the next event name is encountered. In Listing
17.1, the event names are <TT>initialization</TT>, <TT>start-of-selection</TT>,
and <TT>end-of-selection</TT>. Event names are reserved words.
You cannot create new events-you can only use existing ones.
<P>
<IMG SRC="../button/input.gif" tppabs="http://pbs.mcp.com/ebooks/0672312174/button/input.gif">
<HR>
<P>
<B>Listing 17.1 Three Basic Events<BR>
</B>
<BLOCKQUOTE>
<PRE>
1 report ztx1701.
2 initialization.
3 write / '1'.
4
5 start-of-selection.
6 write / '2'.
7
8 end-of-selection.
9 write / '3'.
</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.1 produces the following output:
<BLOCKQUOTE>
<PRE>
1
2
3
</PRE>
</BLOCKQUOTE>
<P>
<IMG SRC="../button/analysis.gif" tppabs="http://pbs.mcp.com/ebooks/0672312174/button/analysis.gif">
<UL>
<LI>Line 2 identifies the beginning of the <TT>initialization</TT>
event.
<LI>Line 3 is associated with the <TT>initialization</TT> event.
If more lines of code followed this one, they would all belong
to the <TT>initialization</TT> event.
<LI>Line 4 marks the end of the code belonging to <TT>initialization</TT>
and the beginning of the <TT>start-of-selection</TT> event.
<LI>Line 6 belongs to the <TT>start-of-selection</TT> event.
<LI>Line 8 marks the lower boundary of the code belonging to the
<TT>start-of-selection</TT> event and the upper boundary of the
<TT>end-of-selection</TT> event.
<LI>Line 9 belongs to the <TT>end-of-selection</TT> event.
<LI>The lower boundary of the code belonging to <TT>end-of-selection</TT>
is marked by the end of the program.
<LI>When you run this program, these events are triggered by a
driver program. The following paragraphs explain this concept
in detail.
</UL>
<P>
<IMG SRC="../button/newterm.gif" tppabs="http://pbs.mcp.com/ebooks/0672312174/button/newterm.gif">
<P>
A <I>driver program</I> is one program that controls another (driven)
program. SAP supplies driver programs with the R/3 system. You
supply the <I>driven</I> program. When you start your program,
a driver program always starts first, and then it calls the events
in your program. To reiterate, a driver program starts first and
then controls when your program gets control. This has always
been the case for all of the programs you have written so far;
you just have not been aware of it until now. Please be sure you
read this paragraph carefully. To repeat once more, when <I>you</I>
start <I>your</I> program, a driver program starts first and controls
your program by calling events within it.
<P>
The code associated with an event is triggered by a statement
in a driver program. Events are triggered by the driver program
in a predefined and predictable sequence. Figure 17.1 illustrates
this point.
<P>
<A HREF="javascript:popUp('f17-1.gif')"><B>Figure 17.1 :</B> <I>The driver program and the events it can
trigger</I>.</A>
<P>
Figure 17.1 has pseudocode within the driver program. The code
shown in the program on the right is actual code. When program
<TT>ztx1701</TT> is executed, the driver on the left starts first.
The program then follows this sequence of steps:
<UL>
<LI>It triggers the <TT>initialization</TT> event, causing the
code belonging to <TT>initialization</TT> to be executed. If you
have not coded an <TT>initialization</TT> event in your program,
the driver program skips this step.
<LI>It shows the selection screen for your program. (A selection
screen is the screen that contains the input fields for your parameter
statements.) If your program doesn't have a selection screen,
it skips this step.
<LI>It triggers the <TT>start-of-selection</TT> event, causing
the code belonging to that event to be executed. If you have not
coded a <TT>start-of-selection</TT> event in your program, it
skips this step.
<LI>It triggers the <TT>end-of-selection</TT> event in your program,
executing all of the code belonging to it. If you haven't coded
an <TT>end-of-selection</TT>, it skips this step.
<LI>It shows the output list to the user.
</UL>
<P>
The order of execution for events is determined by the driver
program, not by your program. Therefore, you can code the events
in any order and the execution order will still be the same. The
order of events in your program doesn't matter; they will always
be triggered in the same sequence by the driver program. Listing
17.2 illustrates this point.
<P>
<IMG SRC="../button/input.gif" tppabs="http://pbs.mcp.com/ebooks/0672312174/button/input.gif">
<HR>
<P>
<B>Listing 17.2 Events Triggered in the Order Dictated
by the Driver Program<BR>
</B>
<BLOCKQUOTE>
<PRE>
1 report ztx1702.
2 data f1 type i value 1.
3
4 end-of-selection.
5 write: / '3. f1 =', f1.
6
7 start-of-selection.
8 write: / '2. f1 =', f1.
9 f1 = 99.
10
11 initialization.
12 write: / '1. f1 =', f1.
13 add 1 to f1.
</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.2 produces the following output:
<BLOCKQUOTE>
<PRE>
1. f1 = 1
2. f1 = 2
3. f1 = 99
</PRE>
</BLOCKQUOTE>
<P>
<IMG SRC="../button/analysis.gif" tppabs="http://pbs.mcp.com/ebooks/0672312174/button/analysis.gif">
<UL>
<LI>When you start <TT>ztx1702</TT>, the driver program starts
first.
<LI>It triggers the <TT>initialization</TT> event.
<LI>The code associated with <TT>initialization</TT> is executed
(lines 12 and 13). The value of <TT>f1</TT> is written out and
1 is added to it. Control then returns to the driver program.
<LI>The driver program looks for a selection screen. There isn't
one for this program, so it looks for a <TT>start-of-selection</TT>
event. It finds it on line 7, and so branches to <TT>ztx1702</TT>
beginning at line 8. Lines 8 and 9 are executed and control then
returns to the driver.
<LI>The driver program then looks for an <TT>end-of-selection</TT>
event. Finding it on line 4, it transfers control <TT>ztx1702</TT>
beginning at line 5. Only line 5 belongs to the <TT>end-of-selection</TT>
event, so it is executed and control then returns to the driver.
<LI>The driver then displays the list to the user.
</UL>
<P>
This example illustrates that you can put the events in a different
order, but the execution sequence is not changed. The execution
sequence is always <TT>initialization</TT>, <TT>start-of-selection</TT>,
<TT>end-of-selection</TT>. There are other events as well; some
occur after <TT>initialization</TT> and some occur between <TT>start-of-selection</TT>
and <TT>end-of-selection</TT>.
<P>
Programmers usually position events in the order in which they
are triggered to make their programs easier to understand.
<P>
There are eleven different events in ABAP/4, categorized in Table
17.1 according to how they are triggered.<BR>
<P>
<CENTER><B>Table 17.1 ABAP/4 Events</B></CENTER><CENTER>
<TABLE BORDER=1>
<TR VALIGN=TOP><TD WIDTH=192><CENTER><B>Category</B></CENTER></TD><TD WIDTH=192><CENTER><B>Events</B></CENTER>
</TD></TR>
<TR VALIGN=TOP><TD WIDTH=192>Driver</TD><TD WIDTH=192><TT>initialization</TT>
<BR>
at selection-screen<BR>
start-of-selection<br>
get<br>
end-of-selection
</TD></TR>
<TR VALIGN=TOP><TD WIDTH=192>User</TD><TD WIDTH=192><TT>at line-selection</TT>
<br>
<TT>at pfn</TT>
<br>
<TT>at user-command</TT>
</TD></TR>
<TR VALIGN=TOP><TD WIDTH=192>Program</TD><TD WIDTH=192><TT>top-of-page</TT>
<br>
<TT>end-of-page</TT>
</TD></TR>
</TABLE>
</CENTER>
<P>
<P>
Driver events are triggered by the driver program. User events
are triggered by the user via the user interface. Program events
are those triggered from within your program. This chapter details
the use of <TT>initialization</TT>, <TT>start-of-selection</TT>,
and <TT>end-of-selection</TT>. Note that some of these events
occur between <TT>start-of-selection</TT> and <TT>end-of-selection</TT>.
<P>
In the previous chapter, "Formatting Techniques, Part 2,"
you learned about the <TT>top-of-page</TT> and <TT>bottom-of-page</TT>
events. The information in this chapter also applies to them,
but these are triggered by statements inside your program, not
from the driver.
<H4>Special Considerations When Using write Within Events</H4>
<P>
Events have two unusual affects on the <TT>write</TT> statement:
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -