16-1 COBOL for the 21 st Century Nancy Stern Hofstra University Robert A. Stern Nassau Community...

53
16-1 COBOL for the 21 st Century Nancy Stern Hofstra University Robert A. Stern Nassau Community College James P. Ley University of Wisconsin- Stout (Emeritus) John Wiley & Sons, Inc. 11th edition

Transcript of 16-1 COBOL for the 21 st Century Nancy Stern Hofstra University Robert A. Stern Nassau Community...

Page 1: 16-1 COBOL for the 21 st Century Nancy Stern Hofstra University Robert A. Stern Nassau Community College James P. Ley University of Wisconsin-Stout (Emeritus)

16-1

COBOL for the 21st Century

Nancy Stern Hofstra University

Robert A. Stern Nassau Community College

James P. Ley University of Wisconsin-Stout (Emeritus)

John Wiley & Sons, Inc.11th edition

Page 2: 16-1 COBOL for the 21 st Century Nancy Stern Hofstra University Robert A. Stern Nassau Community College James P. Ley University of Wisconsin-Stout (Emeritus)

16-2

Improving Program Productivity Using The COPY, CALL, and Other Statements

Chapter 16

Page 3: 16-1 COBOL for the 21 st Century Nancy Stern Hofstra University Robert A. Stern Nassau Community College James P. Ley University of Wisconsin-Stout (Emeritus)

16-3

Chapter Objectives

• To familiarize you with

• COPY statement for copying parts of a program stored in a library

• CALL statement for executing called programs as subroutines

• Text manipulation with STRING and UNSTRING statements

Page 4: 16-1 COBOL for the 21 st Century Nancy Stern Hofstra University Robert A. Stern Nassau Community College James P. Ley University of Wisconsin-Stout (Emeritus)

16-4

Chapter Contents

• COPY Statement

• CALL Statement

• STRING and UNSTRING Statements

Page 5: 16-1 COBOL for the 21 st Century Nancy Stern Hofstra University Robert A. Stern Nassau Community College James P. Ley University of Wisconsin-Stout (Emeritus)

16-5

COPY Statement

• To copy prewritten COBOL entries stored in a library into a program

• Often used to copy – FD, 01 entries that define and describe

files and records– Tables– SCREEN SECTIONs– Standard modules used in PROCEDURE

DIVISION

Page 6: 16-1 COBOL for the 21 st Century Nancy Stern Hofstra University Robert A. Stern Nassau Community College James P. Ley University of Wisconsin-Stout (Emeritus)

16-6

Benefits of COPYing Entries

• Saves programmer coding, debugging time by using code already written

• Promotes program standardization - all entries copied from library are identical

• Reduces time needed to modify code - changes made once to library copy

• Library entries can be well-documented so meaningful to all users

Page 7: 16-1 COBOL for the 21 st Century Nancy Stern Hofstra University Robert A. Stern Nassau Community College James P. Ley University of Wisconsin-Stout (Emeritus)

16-7

COPY Statement

COPY test-name OF library-name.

IN

• Library-name is external-name, 1 to 8 characters, letters and digits only

• Use in ENVIRONMENT, DATA, or PROCEDURE DIVISION

Format

Page 8: 16-1 COBOL for the 21 st Century Nancy Stern Hofstra University Robert A. Stern Nassau Community College James P. Ley University of Wisconsin-Stout (Emeritus)

16-8

COPY Statement Example

• Suppose file called Customer, stored in library contains following entries:

01 Customer-Rec.

05 Cust-No Pic X(5).

05 Cust-Name Pic X(20).

05 Cust-Address Pic X(30).

05 Cust-Bal-Due Pic 9(4)V99.

Page 9: 16-1 COBOL for the 21 st Century Nancy Stern Hofstra University Robert A. Stern Nassau Community College James P. Ley University of Wisconsin-Stout (Emeritus)

16-9

COPY Statement Example

• Code COPY statement at point in program where entries should appear

Data Division.

File Section.

FD CustFile.

Copy Customer.

• In source listing, copied lines are marked with a C or other designator

Page 10: 16-1 COBOL for the 21 st Century Nancy Stern Hofstra University Robert A. Stern Nassau Community College James P. Ley University of Wisconsin-Stout (Emeritus)

16-10

Source Listing with COPY

… ...

11 FD CustFile.

12 Copy Customer.

13C 01 Customer-Rec.

14C 05 Cust-No Pic X(5).

15C 05 Cust-Name Pic X(20).

16C 05 Cust-Address Pic X(30).

17C 05 Cust-Bal-Due Pic 9(4)V99.

Line numbers followed by C are copied statements

Page 11: 16-1 COBOL for the 21 st Century Nancy Stern Hofstra University Robert A. Stern Nassau Community College James P. Ley University of Wisconsin-Stout (Emeritus)

16-11

Changing COPYed Entries

REPLACING

==pseudo-text-1== ==pseudo-text-2==identifier-1 BY identifier-2 ...

literal-1 literal-2

word-1 word-2

• Add REPLACING clause to COPY to make changes to copied entries

Format

Page 12: 16-1 COBOL for the 21 st Century Nancy Stern Hofstra University Robert A. Stern Nassau Community College James P. Ley University of Wisconsin-Stout (Emeritus)

16-12

Changing COPYed Entries

Copy Customer Replacing

Cust-No By Cust-Number,

==X(5)== By ==X(6)==.

• Changes copied line 14C– From: 14C 05 Cust-No Pic

X(5).– To: 14C 05 Cust-Number Pic X(6).

Example

Page 13: 16-1 COBOL for the 21 st Century Nancy Stern Hofstra University Robert A. Stern Nassau Community College James P. Ley University of Wisconsin-Stout (Emeritus)

16-13

CALL Statement

• Used in PROCEDURE DIVISION to CALL or reference independent subprograms stored in library

• Calling program - main program that calls or references subprogram

• Called program - subprogram stored in separate file that is linked and executed within main program

Page 14: 16-1 COBOL for the 21 st Century Nancy Stern Hofstra University Robert A. Stern Nassau Community College James P. Ley University of Wisconsin-Stout (Emeritus)

16-14

Called Program (Subprogram)

• Compiled, debugged, stored in library to be called when needed

• Typical subprograms include:– Edit routines– Error control checks– Standard calculations– Summary, total printing

Page 15: 16-1 COBOL for the 21 st Century Nancy Stern Hofstra University Robert A. Stern Nassau Community College James P. Ley University of Wisconsin-Stout (Emeritus)

16-15

Advantages of Subprograms

• Avoids duplication of effort

• Improves programmer productivity

• Provides greater flexibility

• Changes to called program can be made without modifying calling program

• Results in greater standardization

Page 16: 16-1 COBOL for the 21 st Century Nancy Stern Hofstra University Robert A. Stern Nassau Community College James P. Ley University of Wisconsin-Stout (Emeritus)

16-16

CALL vs COPY

• COPY brings entries into program as is

• CALL causes an entire program (the called program) to be executed– Data passed from calling to called program– Entire called program (subprogram)

executed– Data passed back from called program– Control returns to calling program

Page 17: 16-1 COBOL for the 21 st Century Nancy Stern Hofstra University Robert A. Stern Nassau Community College James P. Ley University of Wisconsin-Stout (Emeritus)

16-17

CALL vs COPY

• Typically, COPY ENVIROMENT, DATA DIVISION entries into source program

• CALL subprograms in PROCEDURE DIVISION rather than COPY them

Page 18: 16-1 COBOL for the 21 st Century Nancy Stern Hofstra University Robert A. Stern Nassau Community College James P. Ley University of Wisconsin-Stout (Emeritus)

16-18

CALL Statement

CALL literal-1

[USING identifier-1 …]

• To call subprogram into main program

• literal-1 is PROGRAM-ID name in called program

• Enclose in quotes

Format

Page 19: 16-1 COBOL for the 21 st Century Nancy Stern Hofstra University Robert A. Stern Nassau Community College James P. Ley University of Wisconsin-Stout (Emeritus)

16-19

Calling and Called Programs

Identification Division....

Procedure Division....

Call 'Sub1'...

Call 'Sub2'...

Identification Division.

Program-ID. Sub1.…Procedure Division.

Exit Program.

Identification Division.

Program-ID. Sub2.…Procedure Division.

Exit Program.

1

2

3

4

Page 20: 16-1 COBOL for the 21 st Century Nancy Stern Hofstra University Robert A. Stern Nassau Community College James P. Ley University of Wisconsin-Stout (Emeritus)

16-20

Calling and Called Programs

1.Subprogram Sub1 called and executed in its entirety

2.Control returns to calling program

3.Subprogram Sub2 called and executed in its entirety

4.Control returns to calling program

Page 21: 16-1 COBOL for the 21 st Century Nancy Stern Hofstra University Robert A. Stern Nassau Community College James P. Ley University of Wisconsin-Stout (Emeritus)

16-21

CALL … USING

• USING clause required if data passed between calling and called program

• Fields passed are called parameters

• Parameter passed in several ways:– Value of parameter may be passed to and

used by subprogram but not changed– Value for parameter may be calculated by

instructions in subprogram and passed back to main program

Page 22: 16-1 COBOL for the 21 st Century Nancy Stern Hofstra University Robert A. Stern Nassau Community College James P. Ley University of Wisconsin-Stout (Emeritus)

16-22

CALL … USING Example

• Suppose a main program – Reads in employee records– Produces report showing employee salary,

Social Security tax and Medicaid tax (taxes referred to as FICA taxes)

• Subprogram is to calculate amount of each tax based on the salary field– Assume PROGRAM-ID is FICAProg

Page 23: 16-1 COBOL for the 21 st Century Nancy Stern Hofstra University Robert A. Stern Nassau Community College James P. Ley University of Wisconsin-Stout (Emeritus)

16-23

CALL … USING Example

• Parameters needed – Salary (WS-Ann-Sal)

• Passed to subprogram, used to calculate taxes but not changed by subprogram

– Tax fields Soc-Sec and Med-Tax• Subprogram calculates values for these

parameters and passes them back to main program

Page 24: 16-1 COBOL for the 21 st Century Nancy Stern Hofstra University Robert A. Stern Nassau Community College James P. Ley University of Wisconsin-Stout (Emeritus)

16-24

Calling Program Requirements

• CALL statement with:– Literal same as PROGRAM-ID of called

program– List of parameters in USING clause

Call 'FICAProg'

Using WS-Ann-Sal, Soc-Sec, Med-Tax

Example

Page 25: 16-1 COBOL for the 21 st Century Nancy Stern Hofstra University Robert A. Stern Nassau Community College James P. Ley University of Wisconsin-Stout (Emeritus)

16-25

Called Program Requirements

• PROGRAM-ID identifier is literal used in CALL in main program

• LINKAGE SECTION must be defined in DATA DIVISION– Follows FILE and WORKING-STORAGE

SECTIONs– Describes all items passed between main

and subprogram– Format same as WORKING-STORAGE

Page 26: 16-1 COBOL for the 21 st Century Nancy Stern Hofstra University Robert A. Stern Nassau Community College James P. Ley University of Wisconsin-Stout (Emeritus)

16-26

Called Program Requirements

• PROCEDURE DIVISION header must include USING clause– Lists all parameters or fields defined in

LINKAGE SECTION

• EXIT PROGRAM– Last executed statement in called program– Returns control back to calling program

Page 27: 16-1 COBOL for the 21 st Century Nancy Stern Hofstra University Robert A. Stern Nassau Community College James P. Ley University of Wisconsin-Stout (Emeritus)

16-27

Subprogram Example

Identification Division.

Program-ID. FICAProg.

Data Division.

Linkage Section.

01 WS-Ann-Sal Pic 9(6).

01 Soc-Sec Pic 9(4)V99.

01 Med-Tax Pic 9(5)V99.

Page 28: 16-1 COBOL for the 21 st Century Nancy Stern Hofstra University Robert A. Stern Nassau Community College James P. Ley University of Wisconsin-Stout (Emeritus)

16-28

Subprogram - Procedure Division

Procedure Division

Using WS-Ann-Sal, Soc-Sec, Med-Tax. If WS-Ann-Sal <= 84900

Compute Soc-Sec = .062 * WS-Ann-Sal

Else

Compute Soc-Sec = .062 * 84900

End-If

Compute Med-Tax = .0145 * WS-Ann-Sal

Exit Program.

Page 29: 16-1 COBOL for the 21 st Century Nancy Stern Hofstra University Robert A. Stern Nassau Community College James P. Ley University of Wisconsin-Stout (Emeritus)

16-29

Subprogram Example

• When FICAProg is called– Value of WS-Ann-Sal passed to

subprogram– Value of Soc-Sec and Med-Tax undefined

• When FICAProg finished– Values calculated for Soc-Sec and Med-

Tax passed back to main program

Page 30: 16-1 COBOL for the 21 st Century Nancy Stern Hofstra University Robert A. Stern Nassau Community College James P. Ley University of Wisconsin-Stout (Emeritus)

16-30

Parameter Correspondence

• Parameters passed in sequence– First parameter in CALL … USING passed

to first parameter in PROCEDURE DIVISION USING clause and so on

• Number of parameters in both USING clause should be the same

• PIC clauses for corresponding parameters must be same

Page 31: 16-1 COBOL for the 21 st Century Nancy Stern Hofstra University Robert A. Stern Nassau Community College James P. Ley University of Wisconsin-Stout (Emeritus)

16-31

Parameter Correspondence

• Data-names of corresponding parameters may be same or different

• Parameters paired by sequence, not by data-name

• List parameters in same order in both USING clauses

Page 32: 16-1 COBOL for the 21 st Century Nancy Stern Hofstra University Robert A. Stern Nassau Community College James P. Ley University of Wisconsin-Stout (Emeritus)

16-32

Parameter Correspondence

• Assume USING clauses are:Call 'FICAProg'

Using Soc-Sec, Med-Tax, WS-Ann-Sal

Procedure Division

Using WS-Ann-Sal, Soc-Sec, Med-Tax.

– Soc-Sec passed as value for WS-Ann-Sal– Calculations in subprogram will be

incorrect, values passed back incorrect

Page 33: 16-1 COBOL for the 21 st Century Nancy Stern Hofstra University Robert A. Stern Nassau Community College James P. Ley University of Wisconsin-Stout (Emeritus)

16-33

STRING Statement

• To combine or concatenate several fields into one

• Consider following entries:

05 Name.

10 Last-Name Pic X(10).

10 First-Name Pic X(10).

10 Middle-Name Pic X(6).

Page 34: 16-1 COBOL for the 21 st Century Nancy Stern Hofstra University Robert A. Stern Nassau Community College James P. Ley University of Wisconsin-Stout (Emeritus)

16-34

STRING Statement

• Suppose name fields have values below

• Print name with single blank between parts: THOMAS ALVA EDISON

Last-NameE D I S O N

First-NameT H O M A S

Middle-NameA L V A

Page 35: 16-1 COBOL for the 21 st Century Nancy Stern Hofstra University Robert A. Stern Nassau Community College James P. Ley University of Wisconsin-Stout (Emeritus)

16-35

STRING Statement Format

STRING identifier-1 ...

literal-1

identifier-2

DELIMITED BY literal-2 ...

SIZE

INTO identifier-3

[END-STRING]

Page 36: 16-1 COBOL for the 21 st Century Nancy Stern Hofstra University Robert A. Stern Nassau Community College James P. Ley University of Wisconsin-Stout (Emeritus)

16-36

STRING Statement

• identifier-1 or literal-1 is field or value to be combined

• identifier-3 is field in which all identifiers or literals are combined

• DELIMITED BY clause– Transmitting of characters ends when

value of identifier-2 or literal-2 encountered– SIZE means entire contents to be copied

Page 37: 16-1 COBOL for the 21 st Century Nancy Stern Hofstra University Robert A. Stern Nassau Community College James P. Ley University of Wisconsin-Stout (Emeritus)

16-37

STRING Example

String

First-Name Delimited By ' '

Middle-Name Delimited By ' '

Last-Name Delimited By ' '

Into Name-Out

• Copies characters from named fields to Name-Out up to first blank in field

• Name-Out is THOMASALVAEDISON

Page 38: 16-1 COBOL for the 21 st Century Nancy Stern Hofstra University Robert A. Stern Nassau Community College James P. Ley University of Wisconsin-Stout (Emeritus)

16-38

STRING Example

• To insert a blank between parts of name

String

First-Name Delimited By ' '

' ' Delimited By Size

Middle-Name Delimited By ' '

' ' Delimited By Size

Last-Name Delimited By ' '

Into Name-Out

Places blank after each field

Page 39: 16-1 COBOL for the 21 st Century Nancy Stern Hofstra University Robert A. Stern Nassau Community College James P. Ley University of Wisconsin-Stout (Emeritus)

16-39

OVERFLOW Option

• Specifies operation(s) to be performed if receiving field not large enough to accommodate result

• NOT ON OVERFLOW option may also be used

• END-STRING scope terminator also available

Page 40: 16-1 COBOL for the 21 st Century Nancy Stern Hofstra University Robert A. Stern Nassau Community College James P. Ley University of Wisconsin-Stout (Emeritus)

16-40

POINTER Option

• To count number of characters moved in STRING statement

• Increments specified field by one for every character moved

Page 41: 16-1 COBOL for the 21 st Century Nancy Stern Hofstra University Robert A. Stern Nassau Community College James P. Ley University of Wisconsin-Stout (Emeritus)

16-41

POINTER Option Example

Move 1 To WS-Count

String First-Name Delimited By ' '

Into Name-Out

With Pointer WS-Count

• If First-Name is 'Paul', WS-Count is five after STRING

• Subtract one from WS-Count to get length of actual move (4)

Page 42: 16-1 COBOL for the 21 st Century Nancy Stern Hofstra University Robert A. Stern Nassau Community College James P. Ley University of Wisconsin-Stout (Emeritus)

16-42

POINTER Option

• Also used to move data to receiving field beginning at some point other than first position

• If WS-Count initialized to 15 before STRING, First-Name moved to Name-Out beginning with fifteenth position of Name-Out

Page 43: 16-1 COBOL for the 21 st Century Nancy Stern Hofstra University Robert A. Stern Nassau Community College James P. Ley University of Wisconsin-Stout (Emeritus)

16-43

STRING Statement Rules

• DELIMITED BY clause required

• Receiving field must be elementary data item - no editing symbols or JUSTIFIED RIGHT clause

• All literals must be nonnumeric

• Identifier with POINTER clause must be elementary

• Moves data left to right but does not pad with low-order blanks

Page 44: 16-1 COBOL for the 21 st Century Nancy Stern Hofstra University Robert A. Stern Nassau Community College James P. Ley University of Wisconsin-Stout (Emeritus)

16-44

UNSTRING Statement

• To separate a field into its components

• Suppose Name-In equals

TAFT, WILLIAM, H

• The last name, first name and middle initial can be stored in separate fields without commas with the following UNSTRING statement

Page 45: 16-1 COBOL for the 21 st Century Nancy Stern Hofstra University Robert A. Stern Nassau Community College James P. Ley University of Wisconsin-Stout (Emeritus)

16-45

UNSTRING Example

Unstring Name-In

Delimited By ','

Into Last-Name

First-Name

Middle-Initial

• TAFT will be stored in Last-Name, William in First-Name and H in Middle-Initial

Page 46: 16-1 COBOL for the 21 st Century Nancy Stern Hofstra University Robert A. Stern Nassau Community College James P. Ley University of Wisconsin-Stout (Emeritus)

16-46

UNSTRING Statement Format

UNSTRING identifier-1

DELIMITED BY [ALL] identifier-2

literal-1

OR [ALL] identifier-3

literal-2 …

INTO identifier-4 …

[END-UNSTRING]

Page 47: 16-1 COBOL for the 21 st Century Nancy Stern Hofstra University Robert A. Stern Nassau Community College James P. Ley University of Wisconsin-Stout (Emeritus)

16-47

UNSTRING Statement

• Sending field, as well as literal, must be nonnumeric

• Receiving fields may be numeric or nonnumeric

• ALL phrase means one or more occurrences of literal or identifier treated as one

• POINTER and OVERFLOW clauses may also be used

Page 48: 16-1 COBOL for the 21 st Century Nancy Stern Hofstra University Robert A. Stern Nassau Community College James P. Ley University of Wisconsin-Stout (Emeritus)

16-48

Chapter Summary

COPY Statement

• To copy entries stored in a library to a user program

• Entries for ENVIRONMENT, DATA, and PROCEDURE DIVISIONs may be copied

Page 49: 16-1 COBOL for the 21 st Century Nancy Stern Hofstra University Robert A. Stern Nassau Community College James P. Ley University of Wisconsin-Stout (Emeritus)

16-49

Chapter Summary

COPY statement most often used for

• Copying standard file and record description entries

• Modules used in PROCEDURE DIVISION

Page 50: 16-1 COBOL for the 21 st Century Nancy Stern Hofstra University Robert A. Stern Nassau Community College James P. Ley University of Wisconsin-Stout (Emeritus)

16-50

Chapter Summary

CALL Statement

• To call or reference entire programs stored in a library

• Calling program is user program

• Called program (subprogram) is program accessed from library

Page 51: 16-1 COBOL for the 21 st Century Nancy Stern Hofstra University Robert A. Stern Nassau Community College James P. Ley University of Wisconsin-Stout (Emeritus)

16-51

Chapter Summary

To pass data between calling program and called program

• CALL statement must include USING clause listing name of fields being passed

• PROCEDURE DIVISION statement of called program must also have USING clause

• Called program must have LINKAGE SECTION defining fields passed

• Identifiers in calling and called program may be same or different

Page 52: 16-1 COBOL for the 21 st Century Nancy Stern Hofstra University Robert A. Stern Nassau Community College James P. Ley University of Wisconsin-Stout (Emeritus)

16-52

Chapter Summary

• STRING statement joins or concatenates fields or portions of fields into one field

• UNSTRING statement breaks a long string into parts and thus enables processing of portion of sending field

Page 53: 16-1 COBOL for the 21 st Century Nancy Stern Hofstra University Robert A. Stern Nassau Community College James P. Ley University of Wisconsin-Stout (Emeritus)

16-53

Copyright © 2003 John Wiley & Sons, Inc. All rights reserved. Reproduction or translation of this work beyond that permitted in Section 117 of the 1976 United States Copyright Act without the express written permission of the copyright owner is unlawful. Request for further information should be addressed to the Permissions Department, John Wiley & 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 damages, caused by the use of these programs or from the use of the information contained herein.