You are on page 1of 57

Structured COBOL

Programming “Copyright @ 2000 John Wiley & Sons, In. All


rights reserved. Reproduction or translation of
this work beyond that permitted in Section 117 of

Nancy Stern
the 1976 United States Copyright Act without the
express permission of the copyright owner is
unlawful. Request for further information should
be addressed to the permissions Department ,

Hofstra University John Wily & Sons, Inc. The purchaser may make
back-up copies for his/her own use only and not
for distribution or resale. The Publisher assumes
no responsibility for errors, omissions, or

Robert A. Stern damages, caused by the use of these programs or


from the use of the information contained
herein.”

Nassau Community
College 9th Edition

PowerPoint Presentation:
Richard H. Baum, Ph.D.
DeVry Institute of Technology
CHAPTER 16
Improving Program
Performance Using
The COPY, CALL, and
Other Statements

Structured COBOL Programming, Stern & Stern, 9th Ed


ition
OBJECTIVES

• To familiarize you with:


1. The COPY statement for copying parts
of a program that are stored in a library.
2. The CALL statement for executing called
programs as subroutines.
3. Text manipulation with the STRING and
UNSTRING statements.

Structured COBOL Programming, Stern & Stern, 9th Ed


ition
CONTENTS
• COPY STATEMENT
– Introduction
– Entries that Can be Copied
– An Example
– The Full Format for the COPY Statement
– Self-Test

• CALL STATEMENT
– Why Use a CALL Statement?
– Format of the CALL Statement
• Called Program Requirements
• Calling Program Requirements

Structured COBOL Programming, Stern & Stern, 9th Ed


ition
CONTENTS
• Text Manipulation with the STRING and
UNSTRING Statements
– The STRING Statement
• The Basic Format
• OVERFLOW Option
• POINTER Option
• General Rules for Using the STRING
– The UNSTRING Statement
• The Basic Format
• General Rules for Using the UNSTRING

Structured COBOL Programming, Stern & Stern, 9th Ed


ition
COPY STATEMENT
• A COPY statement is used to bring into a
program a series of prewritten COBOL
entries that have been stored in a library.
• Benefits of copy libraries include:
(1) Saving the programmer a considerable
amount of coding and debugging time;
(2) Promoting program standardization since all
programs that copy entries from a library will
be using common data-names and/or
procedures;
Structured COBOL Programming, Stern & Stern, 9th Ed
ition
COPY STATEMENT

(3) Reducing the time it takes to make


modifications and reducing duplication of
effort;
• if a change needs to be made to a data entry, it
can be made just once in the library without
the need to alter individual programs;
(4) Providing extensively annotated library
entries that they are meaningful to all users;
this annotation results in better-documented
programs and systems.

Structured COBOL Programming, Stern & Stern, 9th Ed


ition
COPY STATEMENT

• Most often, the COPY statement is used to


copy FD and 01 entries that define and
describe files and records.
• In addition, standard modules to be used
in the PROCEDURE DIVISION of several
programs may also be stored in a library
and copied as needed.

Structured COBOL Programming, Stern & Stern, 9th Ed


ition
COPY STATEMENT

• An Example:--Creating a library entry


called CUSTOMER
To copy CUSTOMER into a source program
code the following :
COPY CUSTOMER

Structured COBOL Programming, Stern & Stern, 9th Ed


ition
COPY STATEMENT
• The source listing might appear as
follows(lower case is the copied library; the
numbers are source line numbers; the lines
that include a C after the line number are the
copied statements):
1 IDENTIFICATION DIVISION.
2 PROGRAM-ID. CUST01.
...
10 DATA DIVISION.
11 FD CUSTFILE LABEL RECORDS ARE STANDARD.
12 COPY CUSTOMER.
13 c 01 customer-rec.
14 c 05 cust-no pic x(5).
15 c 05 cust-name pic x(20).
16 c 05 cust-address pic x(30).
17 c 05 cust-bal-due pic 9(4)v99.
Structured COBOL Programming, Stern & Stern, 9th Ed
ition
The Full Format for the COPY Statement
COPY text-name-1 [{OF}{IN} library-name-1]
[REPLACING {{==pseudo-text-1==}
{identifier-1}{literal-1} {word-1}
BY{==pseudo-text-2} {identifier-2}{literal-2}
{word-2}. . . ]
– If the REPLACING clause is omitted from the COPY
statement, the library text is copied unchanged.
– The REPLACING option allows virtually any library
entry to be changed when it is being copied into the
user's source program.

Structured COBOL Programming, Stern & Stern, 9th Ed


ition
The Full Format for the COPY
Statement
• The REPLACING clause does not alter the
prewritten entries in the library. That is,
the changes are made to the user's source
program only.
• Typically, FDs with long or complex record
descriptions are copied into programs, as
are SCREEN SECTIONs and even modules
or paragraphs that are common to more
than one program.
• Tables are also often copied.
Structured COBOL Programming, Stern & Stern, 9th Ed
ition
QUESTIONS?

Structured COBOL Programming, Stern & Stern, 9th Ed


ition
SELF-TEST

1. A single series of file or record


description entries may be used in several
different programs by placing it in a
_____ and _____ it when needed.

Solution: library; copying

Structured COBOL Programming, Stern & Stern, 9th Ed


ition
SELF-TEST

2. With the statement _______you can


include prewritten entries in your
program.

Solution: COPY
Structured COBOL Programming, Stern & Stern, 9th Ed
ition
SELF-TEST

3. (T or F) A user or source program can


copy library routines and make changes
to the field-names initially specified in the
library.

Solution: T

Structured COBOL Programming, Stern & Stern, 9th Ed


ition
SELF-TEST

4. (T or F) Using the REPLACING option of


the COPY statement, it is possible to alter
the field-names stored in the library itself.

Solution: F--This option only alters


library functions for the user program

Structured COBOL Programming, Stern & Stern, 9th Ed


ition
SELF-TEST

5. Two purposes of using library functions


are to _____ and to ____ .

Solution: make coding and debugging


easier; increase standardization

Structured COBOL Programming, Stern & Stern, 9th Ed


ition
CALL STATEMENT
Why Use the CALL Statement?
• Structured programs should consist of a series of
independent modules that are executed from the
main module.
– Modules within a program can be viewed as
subroutines that are called or executed from the main
module.

• But a program may also CALL or reference


independent subprograms stored in a library that
are entirely separate from the main program.

Structured COBOL Programming, Stern & Stern, 9th Ed


ition
CALL STATEMENT

• The program that references--or calls--a


subprogram is referred to as the calling
program.
• The subprogram that is linked and
executed within the calling program is the
called program.

Structured COBOL Programming, Stern & Stern, 9th Ed


ition
WHY USE A CALL STATEMENT?

• The called program would need to be


compiled, debugged, and catalogued or
stored in a library so that it may be called
when needed.
• Typical subprograms that may be used by
numerous calling programs include edit
routines, error control checks, standard
calculations, and summary and total
printing.
Structured COBOL Programming, Stern & Stern, 9th Ed
ition
ADVANTAGES OF CALLING
SUBPROGRAMS
1. Avoids duplication of effort.
– When modules need to be included in more
than one program, it is best to write them
separately and call them into each program
as needed.

2. Improves programmer productivity.


– Programmers can specialize or code modules
that make use of their specific talents or
skills.

Structured COBOL Programming, Stern & Stern, 9th Ed


ition
ADVANTAGES OF CALLING
SUBPROGRAMS
3. Provides greater flexibility.
– Subprograms may be written in any
programming language, but are typically
written in a language best suited to the
specific task required.

4. Allows changes to the called program to


be made without needing to modify the
calling program.
5. Results in greater standardization.
Structured COBOL Programming, Stern & Stern, 9th Ed
ition
Differences between CALL and
COPY
• The COPY brings into a user program
separate ENVIRONMENT, DATA, or
PROCEDURE DIVISION segments as is.
– The copied entries are compiled and executed
together with the source program.

• The CALL causes an entire program, which is


already in machine language, to be executed.
– The calling and called programs are separate, but
data may be passed from the called program to
the calling program or from the calling program
to the called program.

Structured COBOL Programming, Stern & Stern, 9th Ed


ition
Differences between CALL and
COPY
• When the CALL is performed, data is
passed from the calling to the called
program.
– The entire called program is executed, data is
passed from the called program back to the
calling program, and control returns to the
calling program.

• Typically, we COPY ENVIRONMENT and


DATA DIVISION entries into a source
program and we CALL programs from a
library rather than COPY them.
Structured COBOL Programming, Stern & Stern, 9th Ed
ition
Format of the CALL Statement
• Format:
CALL literal-1
[USING identifier-1....]
– Literal-1 is the name of the called program as
specified in its PROGRAM-ID statement; it is
enclosed in quotes like a nonnumeric literal.
– The USING clause of the CALL statement is
required if the subprogram performs any
operations in which data is to be passed from one
program to another.

Structured COBOL Programming, Stern & Stern, 9th Ed


ition
Format of the CALL Statement
• The CALL ... USING identifies fields in the
main or calling program that will be either
passed to the called program before it is
executed, or passed back to the calling
program after the called program has
been executed.
– Since the purpose of calling a subprogram is
to perform operations or calculations on data,
we almost always employ the USING option.

Structured COBOL Programming, Stern & Stern, 9th Ed


ition
Called Program Requirements
PROGRAM-ID.
• The literal used in the CALL statement of the
main program to extract a subprogram or
routine from a library and execute it must be
identical to the called program's PROGRAM-
ID.
– In the calling program, we code CALL 'literal-
1' USING ... .
– In the called program, we code PROGRAM-
ID. literal-1.

Structured COBOL Programming, Stern & Stern, 9th Ed


ition
Called Program Requirements
LINKAGE SECTION.
• A LINKAGE SECTION must be defined in the
called program for identifying those items that:
(1) will be passed to the called program from the
calling program and
(2) passed back from the called program to the
calling program.

• The LINKAGE SECTION of the called program,


then, describes all items to be passed between
the two programs.

Structured COBOL Programming, Stern & Stern, 9th Ed


ition
Called Program Requirements
PROCEDURE DIVISION USING.
• The identifiers specified in the USING clause
in the PROCEDURE DIVISION entry include
all fields defined in the LINKAGE SECTION;
– these identifiers will be passed from one
program to the other.
– They are passed to and from corresponding
identifiers in the CALL ... USING of the main
program.

Structured COBOL Programming, Stern & Stern, 9th Ed


ition
Called Program Requirements

EXIT PROGRAM.
• The last executed statement in the called
program must be the EXIT PROGRAM.
– It signals the computer to return control back
to the calling program.

Structured COBOL Programming, Stern & Stern, 9th Ed


ition
Calling Program Requirements
• To execute a subprogram stored in a
library, the only statement required in the
calling program is the
CALL 'literal-1' USING . . .
• The literal specified in the CALL statement
of the main program should be the same
as in the PROGRAM-ID of the called
program, but it is enclosed in quotes in the
CALL.
Structured COBOL Programming, Stern & Stern, 9th Ed
ition
COBOL 85

• With COBOL 74, you pass 01-level items.


• With COBOL 85 you can pass parameters
at any level as long as they are
elementary items.

Structured COBOL Programming, Stern & Stern, 9th Ed


ition
TEXT MANIPULATION
WITH THE STRING AND
UNSTRING STATEMENTS

Structured COBOL Programming, Stern & Stern, 9th Ed


ition
Interactive Processing

• When data is entered interactively, we


sometimes need to convert it into a more
concise form for processing purposes or
for storing it on disk.
• Similarly, when data is to be displayed, we
sometimes need to convert it from a
concise form to a more readable form.
• We can use the STRING and UNSTRING for
these purposes.
Structured COBOL Programming, Stern & Stern, 9th Ed
ition
The STRING Statement

The Basic Format


• A STRING statement may be used to
combine several fields to form one
concise field.
• This process is called concatenation.

Structured COBOL Programming, Stern & Stern, 9th Ed


ition
The STRING Statement

• A simplified format of the STRING


statement is:
STRING {{identifier-1}{literal-1} . . .
DELIMITED BY {identifier-2}
{literal-2}{SIZE}} . . .
INTO identifier-3
[END-STRING]

Structured COBOL Programming, Stern & Stern, 9th Ed


ition
The STRING Statement
• For example, we can instruct the computer to
transmit only significant or nonblank
characters in FIRST-NAME, MIDDLE-NAME,
and LAST-NAME. Once a blank is reached, we
stop transmitting that field:
STRING
FIRST-NAME DELIMITED BY ' '
MIDDLE-NAME DELIMITED BY ' '
LAST-NAME DELIMITED BY ' '
INTO NAME-OUT
– The delimiter itself would not be placed in the receiving field.

Structured COBOL Programming, Stern & Stern, 9th Ed


ition
The STRING Statement
• We can also use literals between clauses, to insert
blanks between significant characters as follows:
STRING
FIRST-NAME DELIMITED BY ‘ ‘
‘ ‘ DELIMITED BY SIZE
MIDDLE-NAME DELIMITED BY ‘ ‘
‘ ‘ DELIMITED BY SIZE
LAST-NAME DELIMITED BY ‘ ‘
‘ ‘ DELIMITED BY SIZE
INTO NAME
– In this instance the NAME would be displayed as:
THOMAS ALVA EDISON

Structured COBOL Programming, Stern & Stern, 9th Ed


ition
The STRING Statement

• The delimiter SIZE means that the entire


content of the specified literal is
transmitted (it could have been a field as
well).
• Each time ' ' DELIMITED BY SIZE is
executed, a one-position blank is
transmitted.

Structured COBOL Programming, Stern & Stern, 9th Ed


ition
The STRING Statement
• As noted, the delimiter SIZE means that the
entire content of the field is transmitted.
• The delimiter for STATE and ZIP can also be
SIZE because we will always print two
characters for STATE and five characters for
ZIP.
• Note that this coding would not be correct
if city consisted of more than 1 word ( NEW
ORLEANS).

Structured COBOL Programming, Stern & Stern, 9th Ed


ition
The STRING Statement
• There are numerous additional options
available with the STRING, only some of
which we will discuss.
OVERFLOW Option
STRING...
[ON OVERFLOW imperative- statement-1]
– The OVERFLOW option specifies the operation(s)
to be performed if the receiving field is not large
enough to accommodate the result.

Structured COBOL Programming, Stern & Stern, 9th Ed


ition
The STRING Statement
POINTER Option
• We may also count the number of
characters actually moved in a STRING
statement:
STRING ...
[WITH POINTER identifier- 1]
[ON OVERFLOW ...]
– The identifier will specify the number of
nonblank characters moved to the receiving
field if it is initialized at one.
Structured COBOL Programming, Stern & Stern, 9th Ed
ition
The STRING Statement
• RULES FOR USING THE STRING STATEMENT
1. The DELIMITED BY clause is required. It can
indicate:
– SIZE: The entire sending field is transmitted.
– Literal: The transfer of data is terminated when
the specified literal is encountered; the literal
itself is not moved.
– Identifier: The transfer of data is terminated
when the contents of the identifier is
encountered.

Structured COBOL Programming, Stern & Stern, 9th Ed


ition
The STRING Statement

2. The receiving field must be an


elementary data item with no editing
symbols or JUSTIFIED RIGHT clause.
3. All literals must be described as
nonnumeric.

Structured COBOL Programming, Stern & Stern, 9th Ed


ition
The STRING Statement

4. The identifier specified with the


POINTER clause must be an elementary
numeric item.
5. 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, unlike an
alphanumeric MOVE.

Structured COBOL Programming, Stern & Stern, 9th Ed


ition
THE UNSTRING STATEMENT
The Basic Format
• The UNSTRING statement may be used to
convert keyed data to a more appropriate form
for storing it on disk.
– For example, a program may include a statement that
causes the following to be displayed on a screen:

ENTER NAME: LAST, FIRST, MIDDLE INITIAL : USE


COMMAS TO SEPARATE ENTRIES
– The message to the operator is fairly clear. When the
name is entered, is stored in an alphanumeric field
called NAME- IN.

Structured COBOL Programming, Stern & Stern, 9th Ed


ition
THE UNSTRING STATEMENT
• Suppose we wish to store the name in an
output disk record as follows:

01 PAYROLL-REC.
05 NAME-OUT.
10 LAST-NAME PIC X(20).
10 FIRST-NAME PIC X(15).
10 MIDDLE-INITIAL PIC X.

Structured COBOL Programming, Stern & Stern, 9th Ed


ition
THE UNSTRING STATEMENT

• With an UNSTRING statement, we can


instruct the computer to separate the
NAME-IN into its components and store
them without the commas:
UNSTRING NAME-IN
DELIMITED BY ‘,’
INTO LAST-NAME
FIRST-NAME
MIDDLE-INITIAL
Structured COBOL Programming, Stern & Stern, 9th Ed
ition
THE UNSTRING STATEMENT

UNSTRING Format
UNSTRING identifier-1
[DELIMITED BY [ALL]{identifier-2}{literal-1}
[OR [ALL] {identifier-3}{literal-2}]...]
INTO identifier-4...
[END-UNSTRING]

Structured COBOL Programming, Stern & Stern, 9th Ed


ition
THE UNSTRING STATEMENT
• We may use any literal, even a blank, as a
delimiter.
– We may also ACCEPT a name from a keyboard
and UNSTRING it so that we can use just the
last name for looking up a corresponding disk
record with last name as a RECORD KEY or
ALTERNATE RECORD KEY.

• When the ALL phrase is included, one or


more occurrences of the literal or identifier
are treated as just one occurrence.

Structured COBOL Programming, Stern & Stern, 9th Ed


ition
THE UNSTRING STATEMENT
• SUMMARY RULES FOR USING THE UNSTRING
STATEMENT
1. The sending field must be nonnumeric. The
receiving fields may be numeric or nonnumeric.
2. Each literal must be nonnumeric.
3. The [WITH POINTER identifier] and [ON
OVERFLOW imperative-statement] clauses may
be used in the same way as with the STRING.

Structured COBOL Programming, Stern & Stern, 9th Ed


ition
CHAPTER SLIDES END HERE

CHAPTER SUMMARY COMES NEXT

Structured COBOL Programming, Stern & Stern, 9th Ed


ition
CHAPTER SUMMARY

A. COPY Statement
1. To copy entries stored in a library to a user
program.
2. ENVIRONMENT, DATA, and PROCEDURE
DIVISION entries may be copied.
3. Most often used for copying standard file and
record description entries or modules to be
used in the PROCEDURE DIVISION.
4. The format is: COPY text-name {OF}{IN}
library-name.
Structured COBOL Programming, Stern & Stern, 9th Ed
ition
CHAPTER SUMMARY
B. CALL Statement
1. To call or reference entire programs stored in a
library.
2. The user program is referred to as the calling
program; the program accessed from the library will
serve as a subprogram and is referred to as the called
program.
3. To pass data from the called program to the calling
program.
a. The CALL statement can include a USING clause that
lists the names of the fields in the calling program that
are passed to the called program and fields that will be
passed back from the called program.

Structured COBOL Programming, Stern & Stern, 9th Ed


ition
CHAPTER SUMMARY
b. The PROCEDURE DIVISION statement of the
called program also includes a USING clause to
indicate identifiers specified in this
subprogram that will correspond to identifiers
in the calling program.
c. Identifiers in the called and calling program
may be the same or they may be different, but
their picture clauses should match.
d. The called program must have a LINKAGE
SECTION in which fields to be passed to and
from the calling program are defined. This is
the last section of the DATA DIVISION.

Structured COBOL Programming, Stern & Stern, 9th Ed


ition
CHAPTER SUMMARY
e. The called program must end with an EXIT
PROGRAM statement. This must be in a
separate paragraph only for COBOL 74.

C. The STRING statement joins or


concatenates fields or portions of fields
into one field.
• The UNSTRING statement enables
processing of a portion of a sending field.

Structured COBOL Programming, Stern & Stern, 9th Ed


ition

You might also like