CHAPTER 16
IMPROVING PROGRAM
PRODUCTIVITY
USING THE COPY, CALL,
AND OTHER STATEMENTS
CHAPTER OBJECTIVES
Upon
completion of this chapter, the student should be able to:
1. List the advantages of using the COPY
statement.
2.
Describe the format of the COPY
statement.
3.
Design and code a program that uses the
COPY statement.
4.
List the advantages of using subprograms.
5.
Describe the format of the CALL
statement.
6.
Design and code a subprogram that
processes data passed in from a calling program.
7.
Design and code a program that calls a
subprogram.
8.
Describe the differences between using a
COPY statement and calling a subprogram.
9.
Given a STRING statement, explain what
the statement does.
10.
Code STRING statements to solve specific
problems.
11.
Given an UNSTRING statement, explain what
the statement does.
12.
Code UNSTRING statements to solve
specified problems.
LECTURE OUTLINE
I. COPY Statement
A. Introduction
1. The COPY statement is used to bring a
series of prewritten COBOL entries, stored in a library, into a program.
2. Use of the COPY statement is beneficial
since it:
a. Saves coding and debugging time.
b. Promotes program standardization
through the use of common data-names and procedures.
c. Reduces the time required to make
modifications.
d. Results in better-documented programs.
3. Entries that are most often copied:
a. File and record definitions (FD and 01
entries).
b. Standard modules to be used in the
PROCEDURE DIVISION of several programs.
4. Each computer has its own operating
system commands for creating and accessing a library.
B. Entries that can be Copied
1. Prewritten ENVIRONMENT, DATA, or
PROCEDURE DIVISION entries may be saved in a library and copied into a program.
2. Format of the COPY statement:
COPY
text-name [{OF/IN} library-name]
C. Example
Review
the example presented in this section of the text.
D. Full Format of the COPY Statement
1. Review the format of the COPY statement
that is presented in this section of the text.
2. The COPY statement can be used to make
changes to the prewritten entries as they are copied into the program.
3. The REPLACING clause allows library
entries to be altered when they are copied into the source program. The REPLACING clause does not alter the
library entries themselves.
II. CALL Statement
A. Why Use A CALL Statement?
1. Programs may CALL or execute
independent subprograms that are entirely separate from the main program
itself. The subprograms are typically
stored in a special library.
2. The main program that references or
calls a subprogram is referred to as the calling program.
3. The subprogram that is called and
executed is referred to as the called program.
4. The called subprogram is independent of
the main program. It has to be compiled
and catalogued or stored in a library.
5. A subprogram may be called by different
main programs.
6. Data items may be passed between the
calling program and the called program.
7. Advantages of calling subprograms:
a. Avoids duplication of effort.
b. Improves programmer productivity.
c. Provides greater flexibility (different
languages may be used to write the subprograms).
d. Changes to the called program can be
made without the need to modify the calling program.
e. Results in greater standardization.
8. The CALL statement and the COPY
statement perform very different functions.
a. Copied entries are compiled and
executed as part of the main program.
b. Calling and called programs are
designed, coded and compiled separately.
B. Format of the CALL statement
1. The format of the CALL statement is:
CALL
literal-1
[USING identifier-1 ... ]
2. Literal-1 is the name of the called
program as specified in its PROGRAM-ID statement. The literal is enclosed in quotation marks
when used in the CALL statement.
3. The USING clause identifies the fields
in the calling program that will be passed either to or from the called
program.
4. A LINKAGE SECTION must be defined in
the DATA DIVISION of the called program to define those items that will be
passed between the main program and the subprogram.
5. The LINKAGE SECTION appears after the
WORKING-STORAGE SECTION in the DATA DIVISION.
6. VALUE clauses are not permitted in the
LINKAGE SECTION.
7. The PROCEDURE DIVISION entry of the
called program includes a USING clause that lists all entries defined in the
LINKAGE SECTION.
8. Data items named in the CALL ... USING
of the main program are passed in sequence to the corresponding items in the
PROCEDURE DIVISION USING statement of the subprogram.
9. EXIT PROGRAM must be the last executed
statement in the called program. It
signals the computer to return control to the calling program. With COBOL 74, it must be the only statement
in the last paragraph.
10. With COBOL 74, only 01-level items may be
passed. With newer versions of COBOL,
parameters at all levels may be passed as long as they are elementary items.
11. The INITIAL PROGRAM clause in the called
program restores all WORKING-STORAGE entries to their original values before
and after each call. The INITIAL PROGRAM
clause is coded in the PROGRAM-ID paragraph of the called program.
12. Data items passed to a subprogram may
have their values protected from modification by using the BY CONTENT clause of
the CALL ... USING statement.
C. Examples
Review the examples
presented in this section.
III. Text
Manipulation with the STRING and UNSTRING Statements
A. STRING Statement
1. The STRING statement concatenates
several fields together to form one field.
2. The STRING statement is used primarily
for text manipulation.
3. The format for the STRING statement is:
STRING
{{identifier-l/literal-l} ...
DELIMITED BY
{identifier-2/literal-2/SIZE}} ...
INTO identifier-3
[END-STRING]
4. The delimiter identifies the point in
the sending field at which the transfer of data should be terminated. The delimiter itself is not placed in the
receiving field.
5. The DELIMITED BY SIZE clause means that
the entire contents of the specified literal or field is transmitted to the
receiving field.
6. Unlike the MOVE statement, the STRING
statement does not replace rightmost characters with spaces.
7. The OVERFLOW option specifies the
operation(s) to be performed if the receiving field is not large enough to
accommodate the result. The NOT ON OVERFLOW option is also available.
8. The POINTER option may be used to count
the number of characters actually moved by a STRING statement. (Initialize the
pointer field to 1 before the STRING; after the STRING subtract 1 to get the
actual number of characters moved.)
9. The POINTER option may also be used to
move data to a receiving field beginning at some point other than the first
position.
10. Rules for use of the STRING statement:
a. The DELIMITED BY clause is required.
b. The receiving field must be an
elementary item with no editing symbols.
c. All literals must be described as
nonnumeric.
d. The POINTER field must be an elementary
numeric item.
e. The STRING statement moves data from
left to right just like alphanumeric fields are moved, but a STRING does not
pad with low-order blanks the way an alphanumeric MOVE does.
B. The UNSTRING Statement
1. The UNSTRING may be used to convert
data entered from a terminal into a form more appropriate for storage on disk.
2. The UNSTRING separates a large field
into component parts.
3. The format of the UNSTRING statement
is:
UNSTRING
identifier-1
[DELIMITED BY [ALL]
{identifier-2/literal-1}
[OR [ALL]
{identifier-3/literal-2}] ...]
INTO identifier-4 ...
[END-UNSTRING]
4. Any literal may be used as a delimiter
to denote the end of a portion of the field.
5. When the ALL phrase is included, one or
more occurrences of the literal or identifier are treated as a single
delimiter.
6. POINTER and ON OVERFLOW options are
available. A NOT ON OVERFLOW clause is
also available.
7. Rules for using the UNSTRING statement:
a. The sending field must be
nonnumeric. The receiving fields may be
numeric or nonnumeric.
b. Each literal must be nonnumeric.
c. The POINTER and ON OVERFLOW clauses may
be used in the same way as with the STRING.
SOLUTIONS
TO REVIEW QUESTIONS
I. True-False Questions
1. F The
COPY statement retrieves and includes into a source program a
series of prewritten
COBOL entries. The CALL statement causes a
separate subprogram to
be executed.
2. T On
PC systems, these items are stored in special directories (folders).
3. T
4. F The
COPY statement may be used in the ENVIRONMENT, DATA and
PROCEDURE
DIVISIONs.
5. F Only
the order, size, and type of the variables are important, not the
names.
6. F The
called program is the subprogram.
7. F If
no data is passed, no LINKAGE SECTION is required.
8. F The
CALLED program ends with an EXIT PROGRAM entry.
9. T
10. T
II. General Questions
1. The COPY statement retrieves and places
a series of prewritten COBOL entries into the body of the source program. These copied entries are compiled and
executed together with the source program.
The CALL statement
causes an entire program (subprogram), which must be in machine language, to be
executed. The subprogram can be debugged
independently of the calling program, but may not be executed independently.
2. COPY INVENTORY-REC.
3. PROCEDURE DIVISION.
.
.
.
CALL ’VALIDATE’ USING COUNT1
4. LINKAGE SECTION.
01 SUM-IT PIC 9(3).
.
.
.
PROCEDURE DIVISION USING SUM-IT.
III. Validating Data
1. The program should include a routine to
verify that:
a. SOC-SEC-NO is a valid numeric value.
b. EMPLOYEE-NAME and EMPLOYEE-ADDRESS are
present.
c. overflow, using an ON OVERFLOW clause,
does not occur during the UNSTRING statement.
2. A control listing of totals should
include:
a. the number of transaction records
processed from the EMPLOYEE-FILE file.
b. the number of transaction records
containing errors.
c. a detailed explanation of each error
found in the transaction file.
SOLUTIONS
TO DEBUGGING EXERCISES
a. There
should not be a hyphen in LINKAGE SECTION.
b. The USING clause in the PROCEDURE
DIVISION needs to reference fields defined in the LINKAGE SECTION of the called
program. It should be coded:
PROCEDURE DIVISION USING Q, R.
c. The last statement executed in a
subprogram is EXIT PROGRAM, not EXIT.