Enterprise COBOL Concepts: Dr. David Woolbright Woolbright - David@columbusstate - Edu 2013
Enterprise COBOL Concepts: Dr. David Woolbright Woolbright - David@columbusstate - Edu 2013
IBM COBOL:
http://www-01.ibm.com/software/awdtools/cobol/zos/
http://www-01.ibm.com/software/awdtools/cobol/zos/library/
Languagage Reference
http://publibfp.boulder.ibm.com/epubs/pdf/igy3lr50.pdf
Programming Guide
http://publibfp.boulder.ibm.com/epubs/pdf/igy3pg50.pdf
Program Organization
Program Organized like a book
Division Identification, Environment, Data, Procedure
Section
Paragraph
Sentence
Clause
Phrase
Word
Grammatical Hierarchy
The grammatical hierarchy follows this form:
Identification division
Paragraphs
Entries
Clauses
Environment division
Sections
Paragraphs
Entries
Clauses
Phrases
Data division
Sections
Entries
Clauses
Phrases
Procedure division
Sections
Paragraphs
Sentences
Statements
Phrases
Coding Rules
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT MSTRFILE ASSIGN TO MSTRFILE Internal File
SELECT CUSTOMER-FILE Name
ASSIGN TO CUSTMAST
ORGANIZATION IS INDEXED External DD
ACCESS MODE IS RANDOM File Name
RECORD KEY IS COSTOMER-KEY
FILE STATUS IS CUSTOMER-FILE-STATUS.
DATA DIVISION
Used to create variables and constant fields
Only three data types
numeric PIC 99999.
alphanumeric (text/string) PIC XXX.
alphabetic PIC AAA.
Level numbers indicate subordination of fields.
Use levels 01-49
Alphabetic is seldom used
Level Numbers
Group item a subdivided field
Elementary item a non-subdivided field
01 Group or independent item
Higher numbers indicate subordinate fields
Level Numbers
66, 77, 88 have special significance
66 Used to rename (no longer used)
77 An independent item (choose 01)
88 Condition name
Level Numbers
01 XXX.
05 YYY.
10 AAA PIC X.
10 BBB PIC X.
05 ZZZ PIC X(20).
Equivalent to IF TRAN-CODE = G
Level 88
Condition Names
Picture Clauses
Picture clause values usually use 9, X, V,
S, A
9 a decimal digit
X any alphanumeric character
V an implied decimal point
S a sign
A A-Z, and blank
Picture Clauses
PIC 9(6)
PIC 9(6)V99
PIC 999999V99
PICTURE X(10)
PIC XXXXXXXXXX
PIC S9(4)V9(4)
PIC S9999V9999
PIC 9(18)
Numeric Edited Fields
XXXBXXBXXXX
99/99/99
ZZ,ZZZ.99DB
***,***.99
----.99
$$$9.99
99999.99
USAGE Clause
Specifies the format in which data is stored in
memory
Normally, the phrase USAGE IS is omitted
FILE SECTION.
FD CUSTOMER-FILE.
01 CUSTOMER-MASTER.
05 CUST-NUM PIC 9(2).
05 CUST-FNAME PIC X(20).
05 CUST-LNAME PIC X(20).
FD SALES-REPORT.
01 REPORT-AREA PIC X(132).
Data Formats
Older terms: Modern terms:
COMPUTATIONAL BINARY
COMP BINARY
COMP-1 FLOATING POINT
COMP-2 FLOATING POINT
COMP-3 PACKED-DECIMAL
COMP-4 BINARY
COMP-5 BINARY (NATIVE)
05 XDATA PIC S9(5) PACKED-DECIMAL.
05 YDATA PIC S9(4) BINARY.
EBCDIC
EBCDIC is an IBM format for storing alphanumeric
characters
A - XC1 J - XD1
B - XC2 K - XD2 S XE2
C - XC3 L - XD3 T XE3
D - XC4 M - XD4 U XE4
E - XC5 N - XD5 V XE5
F - XC6 O - XD6 W XE6
G - XC7 P - XD7 X XE7
H - XC8 Q - XD8 Y XE8
I - XC9 R - XD9 Z XE9
EBCDIC
EBCDIC is an IBM format for storing alphanumeric
characters
0 - XF0 SPACE X40
1 XF1 . - X4B
2 - XF2 , - X6B
3 XF3 * - X5C
4 - XF4 - - X60
5 XF5
6 - XF6
7 XF7
8 XF8
9 XF9
BINARY DATA
Stored in 2s Complement format
WORKING-STORAGE SECTION.
01 TOTAL-FIELDS.
05 CUST-TOTAL PIC S9(7)V99 VALUE 0.
05 COST-TOTAL PIC S9(7)V99 VALUE 0.
01 DATE-AND-TIME.
05 CD-YEAR PIC 9999.
05 CD-MONTH PIC 99.
DATA RELATIONSHIPS
BINARY
PACKED-
DECIMAL
CHARACTER or ZONED-
ALPHANUMERIC DECIMAL
DATA DIVISION
Describe data that exists in another program,
or storage you want to associate with a
symbolic name in the LINKAGE SECTION.
LINKAGE SECTION.
01 LK-DATA-AREA
05 NAME PIC X(40).
05 AGE PIC 999.
DATA DIVISION
The LOCAL-STORAGE SECTION is used to have
storage allocated each time a program is
entered, and deallocated on return from the
program. Used for compatibility with C or Java.
LOCAL-STORAGE SECTION.
01 CUST-NO PIC X(3).
01 COST PIC 9(5)V99.
Initialization of Storage
WORKING-STORAGE for programs is allocated at
the start of the run unit.
IDENTIFICATION DIVISION.
PROGRAM-ID. MAIN IS INITIAL.
...
Initialization of Storage
For the duration of the run unit, WORKING-STORAGE
items persist in their last-used state. Exceptions are:
TABLE-ENTRY(SUB)
TABLE
NUM(SUB)
NAME(SUB)
ITEM(SUB1,SUB2)
Subscripts vs Indexes
Subscripts are defined separately from the table
definitions.
01 MYTABLE.
05 ITEM PIC X(3) OCCURS 10 TIMES.
01 I PIC 9(4) BINARY.
...
MOVE 1 TO I
MOVE ABC TO ITEM(I)
01 MYTABLE.
05 ITEM PIC X(3) OCCURS 10 TIMES.
01 I PIC 9(4) BINARY.
...
PERFORM VARYING I FROM 1 BY 1 UNTIL I > 10
DISPLAY ITEM(I)
END-PERFORM
Subscript Program
Subscripts vs Indexes
Indexes are created when a table is defined
01 MYTABLE.
10 LETTERVALS PIC X(10) VALUE 'ABCDEFGHIJ'.
10 LETTER REDEFINES LETTERVALS
PIC X(1) OCCURS 10 TIMES
INDEXED BY I.
Compatible moves:
-Alphanumeric to Alphanumeric
-Numeric to Numeric
-Numeric to Numeric edited
MOVE STATEMENT
Compatible moves:
-Alphanumeric to Numeric if the sending field is an
unsigned integer
MOVE X TO Y
MOVE STATEMENT
If the receiving field is larger than the sending
field, the receiving field is filled with leading 0s
in a numeric move:
01 A PIC S999V99.
01 B PIC ZZ9.99-.
01 C PIC S9(5)V9999 PACKED-DECIMAL.
W to W, X to X, Y to Y
MOVE CORRESPONDING
Subordinate items must not be identified by the keyword
FILLER
No reference modification for either identifier
Subordinate items must not include a REDEFINES,
RENAMES, OCCURS, INDEX or POINTER description
01 A-GROUP. 01 B-GROUP.
05 W PIC X(3). 05 P PIC X(3).
05 X PIC X(2). 05 X PIC X(2).
05 Y PIC 999. 05 W PIC 999.
MOVE CORRESPONDING A-GROUP TO B-GROUP
W to W, X to X
INITIALIZE
INITIALIZE
SPACE is the implied sending item for
receiving items of category alphabetic,
alphanumeric, alphanumeric-edited,
DBCS, national, or national-edited.
ZERO is the implied sending item for
receiving items of category numeric or
numeric-edited.
INITIALIZE
01 PRICE-FIELDS.
05 UNIT-PRICE PIC 9(5)V9(2) PACKED-DECIMAL.
05 DISCOUNT PIC V9(2).
05 UNIT-CODE PIC XX.
05 SALES-PRICE PIC S9(4) BINARY.
. . .
INITIALIZE PRICE-FIELDS
ADD
ADD Semantics
All identifiers or literals that precede the keyword
TO are added together, and this sum is added to
and stored in identifier-2. This process is
repeated for each successive occurrence of
identifier-2 in the left-to-right order in which
identifier-2 is specified.
ADD X Y Z TO P Q
Before X=1, Y=2, Z=3, P=4, Q=6
After X=1, Y=2, Z=3, P=10, Q=12
ADD EXAMPLES
ADD X
TO Y
ADD X
Y Z TO P
ADD X
Y TO P Q
ADD 1
TO Z
ADD X
TO Y ROUNDED
ADD X
TO Y
ON SIZE ERROR
DISPLAY ADD ERROR
END-ADD
ADD GIVING
ADDGIVING Semantics
All identifiers or literals that precede the keyword TO are
added together, and this sum is added to identifier-2 to
obtain a temporary sum. (Identifier-2 is unchanged)
The the temporary sum is moved to identifier-3.
ADD X Y Z TO V GIVING P
Before X=1, Y=2, Z=3, V=4, P=6
After X=1, Y=2, Z=3, V=4, P=10
SUBTRACT
SUBTRACT
All identifiers or literals preceding the keyword FROM are
added together and their sum is subtracted from and
stored immediately in identifier-2. This process is
repeated for each successive occurrence of identifier-2,
in the left-to-right order in which identifier-2 is specified.
SUBTRACT X Y FROM P Q
Before: X=1,Y=2, P=3,Q=4
After: X=1,Y=2, P=0,Q=1
SUBTRACT
SUBTRACT Semantics
All identifiers or literals preceding the keyword FROM are
added together and their sum is subtracted from
identifier-2 to obtain a temporary value which is moved
to identifier-3.
SUBTRACT X Y FROM P GIVING Q
Before: X=1,Y=2,P=5,Q=6
After: X=1,Y=2,P=5,Q=2
MULTIPLY
MULTIPLY Semantics
In format 1, the value of identifier-1 or literal-1 is
multiplied by the value of identifier-2; the product is then
placed in identifier-2. For each successive occurrence of
identifier-2, the multiplication takes place in the left-to-
right order in which identifier-2 is specified.
MULTIPLY X BY P Q
Before: X=2,P=4,Q=5
After: X=2,P=8,Q=10
MULTIPLY
MULTIPLY
In format 2, the value of identifier-1 or literal-1 is
multiplied by the value of identifier-2 or literal-2. The
product is then stored in the data items referenced by
identifier-3. Identifier-2 is unchanged.
MULTIPLY X BY Y GIVING Z
Before: X=2, Y=3, Z=4
After: X=2, Y=3, Z=6
DIVIDE
DIVIDE
In format 1, the value of identifier-1 or literal-1 is divided
into the value of identifier-2, and the quotient is then
stored in identifier-2. For each successive occurrence of
identifier-2, the division takes place in the left-to-right
order in which identifier-2 is specified.
DIVIDE X INTO Y Z
Before: X=3, Y=7, Z=12
After: X=3, Y=2, Z=4
DIVIDE
DIVIDE
In format 2, the value of identifier-1 or literal-1 is divided
into the value of identifier-2 or literal-2. The value of the
quotient is stored in each data item referenced by
identifier-3.
DIVIDE X INTO Y GIVING Z
Before: X = 2, Y = 13, Z = 1
After: X = 2, Y = 13, Z = 6
DIVIDE
DIVIDE
In format 3, the value of identifier-1 or literal-1 is
divided by the value of identifier-2 or literal-2.
The value of the quotient is stored in each data
item referenced by identifier-3.
DIVIDE X BY Y GIVING Z
Before: X = 10, Y = 3, Z = 1
After: X = 10, Y = 3, Z = 3
DIVIDE
DIVIDE
In format 4, the value of identifier-1 or literal-1 is divided
into identifier-2 or literal-2. The value of the quotient is
stored in identifier-3, and the value of the remainder is
stored in identifier-4.
DIVIDE X INTO Y
GIVING Z
REMAINDER R
Before: X = 2, Y = 9, Z = 8, R = 7
After: X = 2, Y = 9, Z = 4, R = 1
COMPUTE
COMPUTE
COMPUTE can be used to initialize a numeric field
Usually reserved for nontrivial computations. For
simple computations choose ADD, SUBTRACT,
MULTIPLY or DIVIDE
Your Program
Region
Move Mode I/O
READ MYFILE INTO
MYREC
WRITE RECOUT
FROM MYREC
Region
QSAM FILE OPERATIONS
ENVIRONMENT DIVISION.
INPUT-OUTPUT-SECTION.
FILE-CONTROL.
SELECT MY-INPUT-FILE
ASSIGN TO MASTER
FILE STATUS IS MAST-STAT.
ENVIRONMENT DIVISION.
INPUT-OUTPUT-SECTION.
FILE-CONTROL.
SELECT MY-INPUT-FILE
ASSIGN TO MASTER
FILE STATUS IS MAST-STAT.
DATA DIVISION.
FILE SECTION.
FD MY-INPUT-FILE
01 RECORD-AREA PIC X(80).
QSAM
Queued Sequential Access Method
For input files, records are buffered when the file
is OPENed
For output, records are buffered before being
written
Records are processed from the beginning
record sequentially to the end of the file
Very efficient access method for sequential files
Sometimes referred to as flat files
QSAM FILE OPERATIONS
Every file must be OPENed before it can
be processed.
Opening a QSAM Input file queues
records for subsequent read operations
OPEN INPUT MY-INPUT-FILE
OPEN OUTPUT MY-OUTPUT-FILE
Files should be closed when you have
finished processing the records
CLOSE MY-FILE
OPEN
CLOSE
READ
QSAM Input File Operations
Remember: READ a file, WRITE a record.
READ MY-INPUT-FILE
AT END MOVE NO TO MORE-RECS
END-READ
This is a locate-mode read and the most
efficient way to read records
File Reading
READ MY-INPUT-FILE INTO MY-REC
AT END MOVE NO TO MORE-RECS
END-READ WRITE
This is a move-mode read and the least
efficient way to deliver records
QSAM Output File Operations
Write a record!
WRITE MY-RECORD (locate mode)
WRITE MY-RECORD FROM REC-BUFF
END-WRITE (move mode)
CLOSE MY-OUTPUT-FILE
CLOSE MY-INPUT-FILE
Sequential File Reading Pattern
READ MYFILE
AT END MOVE N TO MORE-RECS
END-READ
PERFORM UNTIL MORE-RECS = N PRIMING READ
(process a record code)
READ MYFILE
AT END MOVE N TO MORE-RECS
END-READ
END-PERFORM
CONTINUATION
READ
File Status Codes
00 normal
10 end of file
2x invalid key
3x permanent i/o error
4x logic error
9x unsuccessful operation
Exercise #1
Create a file of 80 byte records
Each record has 3 fields
AFIELD ZONED DECIMAL
with 4 DIGITS & 2 DECIMALS
BFIELD PACKED DECIMAL
with 7 DIGITS & 3 DECIMALS
CFIELD - PACKED DECIMAL
with 7 DIGITS & 1 DECIMAL
Print a report with a column for each field and a column for the
computed value :
(AFIELD + BFIELD)/ CFIELD
Print the result with 2 decimals rounded.
Total each column.
FLOW OF CONTROL
There is a theoretical result in Computer Science by two
Italian mathematicians, Boehm and Jacopini, that states
that only 3 control structures are required to write any
program:
Sequence - Do this, now do this, now do this,
Selection - If something is true do this, else do that
Repetition While something is true, do this
Practice has shown that being able to create procedures
is helpful in overcoming complexity, but they arent
strictly necessary
One implication of this result is that GO TO statements
arent needed
FLOW OF CONTROL
F
?
T F
IF
IF
The condition is tested and either the true
or false blocks are selected for execution
Dont use NEXT SENTENCE if you are
using END-IF as the delimiter (and you
should). Use of NEXT SENTENCE causes
execution to continue with the next closest
period, which is probably the end of the
paragraph.
IF Examples
IF X < Y
ADD 1 TO X
DISPLAY AAA
ELSE
DISPLAY BBB
END-IF
IF X > Y
DISPLAY X WAS BIGGER
END-IF
NESTED IFs
Each ELSE is matched with the nearest preceding IF
IF X < Y
DISPLAY XXX
IF Y < Z
DISPLAY ZZZ
ELSE
DISPLAY AAA
END-IF
MORAL: Indent properly and terminate all if statements
with END-IF
EVALUATE
EVALUATE
EVALUATE PLANET-NUMBER
WHEN 1 MOVE "Mercury" TO PLANET-NAME
WHEN 2 MOVE "Venus " TO PLANET-NAME
WHEN 3 MOVE "Earth " TO PLANET-NAME
WHEN 4 MOVE "Mars " TO PLANET-NAME
WHEN 5 MOVE "Jupiter" TO PLANET-NAME
WHEN 6 MOVE "Saturn " TO PLANET-NAME
WHEN 7 MOVE "Uranus " TO PLANET-NAME
WHEN 8 MOVE "Neptune" TO PLANET-NAME
WHEN 9 MOVE "Pluto " TO PLANET-NAME
WHEN OTHER MOVE " " TO PLANET-NAME
END-EVALUATE.
EVALUATE
EVALUATE PLANET-NAME
WHEN "Mercury" MOVE 1 TO PLANET-NUMBER
WHEN "Venus " MOVE 2 TO PLANET-NUMBER
WHEN "Earth " MOVE 3 TO PLANET-NUMBER
WHEN "Mars " MOVE 4 TO PLANET-NUMBER
WHEN "Jupiter" MOVE 5 TO PLANET-NUMBER
WHEN "Saturn " MOVE 6 TO PLANET-NUMBER
WHEN "Uranus " MOVE 7 TO PLANET-NUMBER
WHEN "Neptune" MOVE 8 TO PLANET-NUMBER
WHEN "Pluto " MOVE 9 TO PLANET-NUMBER
WHEN OTHER MOVE 0 TO PLANET-NUMBER
END-EVALUATE.
EVALUATE
EVALUATE TRUE
WHEN PLANET-NAME = "Mercury" MOVE 1 TO PLANET-NUMBER
WHEN PLANET-NAME = "Venus " MOVE 2 TO PLANET-NUMBER
WHEN PLANET-NAME = "Earth " MOVE 3 TO PLANET-NUMBER
WHEN PLANET-NAME = "Mars " MOVE 4 TO PLANET-NUMBER
WHEN PLANET-NAME = "Jupiter" MOVE 5 TO PLANET-NUMBER
WHEN PLANET-NAME = "Saturn " MOVE 6 TO PLANET-NUMBER
WHEN PLANET-NAME = "Uranus " MOVE 7 TO PLANET-NUMBER
WHEN PLANET-NAME = "Neptune" MOVE 8 TO PLANET-NUMBER
WHEN PLANET-NAME = "Pluto " MOVE 9 TO PLANET-NUMBER
WHEN OTHER MOVE 0 TO PLANET-NUMBER
END-EVALUATE.
EVALUATE
EVALUATE Qty ALSO TRUE ALSO Member
WHEN 1 THRU 5 ALSO VOP < 501 ALSO "Y"
MOVE 2 TO Discount
WHEN 6 THRU 16 ALSO VOP < 501 ALSO "Y"
MOVE 3 TO Discount
WHEN 17 THRU 99 ALSO VOP < 501 ALSO "Y"
MOVE 5 TO Discount
WHEN 1 THRU 5 ALSO VOP < 2001 ALSO "Y"
MOVE 7 TO Discount
WHEN 6 THRU 16 ALSO VOP < 2001 ALSO "Y"
MOVE 12 TO Discount
WHEN 17 THRU 99 ALSO VOP < 2001 ALSO "Y"
MOVE 18 TO Discount
WHEN 1 THRU 5 ALSO VOP > 2000 ALSO "Y"
MOVE 10 TO Discount
WHEN 6 THRU 16 ALSO VOP > 2000 ALSO "Y"
MOVE 23 TO Discount
END-EVALUATE
EVALUATE
EVALUATE TRUE ALSO Position
WHEN L-Arrow ALSO 2 THRU 10
SUBTRACT 1 FROM Position
WHEN R-Arrow ALSO 1 THRU 9
ADD 1 TO Position
WHEN L-Arrow ALSO 1
MOVE 10 TO Position
WHEN R-Arrow ALSO 10
MOVE 1 TO Position
WHEN DelKey ALSO ANY
PERFORM DeleteChar
WHEN Char ALSO 1 THRU 9
PERFORM InsertChar
ADD 1 TO Position
WHEN Char ALSO 10
PERFORM InsertChar
WHEN OTHER PERFORM
DisplayErrorMessage
END-EVALUATE
PERFORM
PERFORM Paragraph
PERFORM paragraph name
Execute all instructions in the paragraph
Return control to the next instruction after the PERFORM
PERFORM 100-ROUTINE
PERFORM 200-ROUTINE
PERFORM 100-ROUTINE
100-ROUTINE.
200-ROUTINE.
300-ROUTINE.
PERFORM THRU
PERFORM paragraph name THRU paragraph name
PRINTS:
1
2
3
100
Inline PERFORM
PERFORM VARYING X FROM 5 BY -1
UNTIL X =0
DISPLAY X
END-PERFORM
PRINTS:
5
4
3
2
1
0
Inline PERFORM
MOVE 10 TO X
PERFORM WITH TEST AFTER
UNTIL X = 0
DISPLAY X
SUBTRACT 1 FROM X
END-PERFORM
PERFORM PARAGRAPH
PERFORM 100-RTN
WITH TEST AFTER
VARYING X FROM 1 BY 1
UNTIL X = 100
100-RTN.
.
Inline PERFORM
MOVE ZERO TO Y
PERFORM UNTIL X = 0
READ AT END MOVE 0 TO X
ADD X TO Y
DISPLAY Y
END-PERFORM
Alternate PERFORM
PERFORM 100-PARA VARYING I FROM 1 BY 1 UNTIL I > 5
AFTER J FROM 1 BY 1 UNTIL J > 3
END-PERFORM
100-PARA.
DISPLAY I J
.
1 1
1 2
1 3
2 1
2 2
2 3
3 1
3 2
3 3
4 1
Table Processing with Subscripts
01 TOT PIC S9(8) PACKED DECIMAL.
01 SUB1 PIC 99.
01 TEMP-REC.
05 TEMP OCCURS 12 TIMES
PIC S9(4).
MOVE 0 TO TOT
PERFORM VARYING SUB1 FROM 1 BY 1
UNTIL SUB1 > 12
ADD TEMP(SUB1) TO TOT
ENDPERFORM
Table Processing with Indexes
01 TOT PIC S9(8) PACKED DECIMAL.
01 TEMP-REC.
05 TEMP OCCURS 12 TIMES INDEXED BY K
PIC S9(4).
MOVE 0 TO TOT
PERFORM VARYING K FROM 1 BY 1
UNTIL K > 12
ADD TEMP(K) TO TOT
END-PERFORM
Manipulating Indexes
Indexes cant be manipulated with ordinary
arithmetic commands. Instead use SET.
SET K TO 3
SET K UP BY 1
SET K UP BY 2
SET K DOWN BY 3
CONTINUE
SET K TO 1
SEARCH TAB-ENTRIES
AT END MOVE 0 TO TAX
WHEN ZIPIN = ZIPCODE(K)
COMPUTE TAX = RATE(K) * AMOUNT
END-SEARCH
Exercise #4
Read the file from exercise #3
UNSTRING NAME
DELIMITED BY ,
INTO LNAME
FNAME
MI
END-UNSTRING
UNSTRING
UNSTRING JCL-DATA
DELIMITED BY ALL SPACES OR ALL ','
INTO WS-DATE-REQUESTED
WS-DATE1
WS-DATE2
END-UNSTRING
Exercise #6
Read the file
BCST.SICCC01.PDSLIB(STRINGS)
Print the first name, middle initial, and last
names in columns
Exercise #7
Read the file
BCST.SICCC01.PDSLIB(STRINGS1)
Print the digits followed by the letters. Use
/ as the delimiter of the two fields.
CALL
Static and Dynamic Call
Programs A and B that are linked together
prior to being loaded are statically linked.
If A calls B, the call is static
If programs A and B are separately
compiled and linked, A can call B
dynamically:
01 PGMNAME PIC X(8) VALUE B.
CALL PGMNAME
Calling Other Programs Statically
CALL literal program name
USING identifier-1,
Examples
CALL CUST1030 USING X,Y
CALL PROG1000
CALLING ANOTHER PROGRAM
CALL PROGXXXX USING A,B
IDENTIFICATION DIVISION.
PROGRAM-ID. PROGXXXX.
LINKAGE SECTION.
01 X PIC X(5).
01 Y PIC 999V99.
PROCEDURE DIVISION USING X,Y.
GOBACK
.
Static and Dynamic CALLS
Two methods for generating dynamic
calls
1. CALL MYPROG USING X,Y,Z
(Using an identifier insures a dynamic
call.)
2. DYNAM/NODYNAM compiler option
determines whether a static or dynamic
call occurs
3. At TSYS all calls are dynamic.
Exercise #8
Write a main program that,
1) Prints I am in the main program,
2) Calls your subprogram,
3) Prints I am back in the main program
Write a subprogram that prints I am in the
subprogram.
Compile and link the programs, execute
the main program
Exercise #9
Write a main program that passes 3
packed decimal numbers to a subprogram
and a fourth variable in which to receive a
result.
Write a subprogram that accepts the 3
integers, computes their sum and returns
the answer to the main
Have the main print the result
Exercise #10
Write a main program that passes a
variable X by reference and Y by content
Have the subprogram add one to both
numbers
Have the main program print X and Y after
returning from the subprogram
NUMERIC Class Test
Before using a suspect field that has a PIC of
9s, use the NUMERIC class test to verify the
field before doing arithmetic
IF AMOUNT-IN IS NUMERIC
ADD 1 TO AMOUNT-IN
ELSE
DISPLAY AMOUNT IS NOT NUMERIC
END-IF
Sign Test
Numeric data can be tested for positive, negative, and
zero values
IF AMOUNT-IN IS POSITIVE
ADD 1 TO AMOUNT-IN
END-IF
IF AMOUNT-IN IS NEGATIVE
DISPLAY AMOUNT-IN
END-IF
IF AMOUNT-IN IS ZERO
DISPLAY THE FIELD IS ZERO
END-IF
INSPECT (TALLYING)
INSPECT (TALLYING)
INSPECT MYLINE
TALLYING ECOUNT FOR ALL E
AFTER INITIAL START"
BEFORE INITIAL END
END-INSPECT
INSPECT WORK TALLYING
COUNT1 FOR LEADING *
COUNT2 FOR CHARACTERS
END-INSPECT
INSPECT WORK TALLYING
COUNT1 FOR ALL * BEFORE .
COUNT2 FOR ALL CHARACTERS AFTER .
END-INSPECT
INSPECT (REPLACING)
INSPECT (REPLACING)
INSPECT MYDATA REPLACING ALL X" BY Y
AFTER INITIAL A"
BEFORE INITIAL Z
FIELDA(1:3) ABC
FIELDA(2:2) BC
FIELDA(4: ) DEFG
Qualification of Names
COBOL allows the same variable name to be used to
define fields in different records or group items.
Duplicate names must be qualified when they are
referenced
01 XFIELD.
05 YFIELD.
10 ZFIELD PIC X(3).
If ZFIELD is a duplicate name it can be qualified in two
ways: ZFIELD OF YFIELD
ZFIELD OF XFIELD
Intrinsic Functions
COBOL does not permit user-defined
functions or procedures
Intrinsic (built-in) Functions can be used in
your programs
Three broad categories of intrinsic
functions: date functions, numeric
functions and string functions.
Intrinsic Functions
Intrinsic Function values are replaced in the position
where they occur by the function result.
Capitalize the state name found in the table. Print message next to each
state name separated by a dash.
Use the SEARCH command perform a sequential search of the table for each look
up. After the program is working, modify it to perform a binary search with SEARCH
ALL
Files with Multiple Record Types
FD TransFile.
01 InsertRec.
02 RECI PIC X.
02 STUDENTIDI PIC 9(7).
02 STUDENTNAME.
03 SURNAME PIC X(8).
03 INITIALS PIC XX.
02 DOB.
03 YOBIRTH PIC 9(4).
03 MOBIRTH PIC 99.
03 DOBIRTH PIC 99.
02 COURSECODE PIC X(4).
02 GENDER PIC X.
01 DELETEREC.
02 RECD PIC X.
02 STUDENTIDD PIC 9(7).
01 UpdateRec.
02 STUDENTIDU PIC 9(7).
02 NEWCOURSECODE PIC X(4).
Multiple O1 File Descriptions
Any number of 01 record descriptions can be
coded with the FD
Only one buffer is used no matter how many
record descriptions have been coded
Record fields cant be referenced before the file
is opened or after it is closed
With multiple record formats, there needs to be a
fixed field to indicate the record type
Value clauses are only used for 88 level items
Writing With Carriage Control
Variable Length Records
The RECORD IS VARYING IN SIZE clause
specifies a file containing variable length
records.
Variable Length Records
The RECSIZE number in the DEPENDING
ON phase must be an elementary
unsigned integer data-item declared in the
WORKING-STORAGE SECTION.
FD TRANFILE
RECORD IS VARYING IN SIZE
FROM 1 TO 80 CHARACTERS
DEPENDING ON RECSIZE.
Variable Length Record Processing
When writing a variable length record, the size of
the record must be placed in the RECSIZE
variable before the WRITE is issued.
When reading a variable length record, the
length of the record is delivered into the
RECSIZE variable.
The 01 Record description must be long enough
to accommodate the largest record
Variable length Record Processing
FD TRANFILE
RECORD IS VARYING IN SIZE
FROM 1 TO 80 CHARACTERS
DEPENDING ON RECSIZE.
01 TRANREC PIC X(80).
88 END-OF-RECS VALUE HIGH-VALUES.
WORKING-STORAGE SECTION.
01 RECSIZE PIC 99.
RECORD TYPE A
1 BYTE TYPE CODE PIC X CONTAINING A
5 BYTE CUSTOMER ID PIC X(5)
RECORD TYPE B
1 BYTE TYPE CODE PIC X CONTAINING B
5 BYTE PART NUMBER PIC X(5)
6 BYTE COST PIC 9(4)V99
EXERCISE #14
Read the variable length records you created in exercise
#5. Produce a report similar to the one below:
CLOSE file-name
KSDS File Statements
START file-name
KEY IS = data-name
>
>=
[INVALID KEY imperative]
[NOT INVALID KEY imperative]
[END-START]
KSDS File Statements
DELETE file-name RECORD
[INVALID KEY imperative]
[NOT INVALID KEY imperative]
[END-DELETE]
Exercise #15
Read BCST.SICCC01.PDSLIB(COBDATA6)
COL 1-5 KEY
COL 6-25 NAME
Allocate a KSDS with record size 25 and a key
field in cols 1-5
Write out a KSDS record for each record in the
file. Write out the records sequentially.
Exercise #16
Read
BCST.SICCC01.PDSLIB(COBDATA7)
COL 1-5 KEY
Read a KSDS record (randomly) for each
record in the file. Write out the names you
find sequentially. If the record doesnt
exist, print a message Not Found
Nested Programs
COBOL programs can be nested. There are many
advantages for doing this:
1) The monolithic working storage of most COBOL
programs leads to difficulty in debugging because all
data is global
2) Nested programs break the working storage into
smaller areas that can only be accessed by programs
that need access.
3) Nested programs provide for parameter passing by
techniques found in all modern languages (by value,
by reference)
4) There is no execution degradation because of nested
programs. In fact, calling a nested program is more
efficient than calling a separately compiled program.
Nested Programs
Calling a nested program is as efficient as
performing a paragraph
Nested programs provide design flexibility and
encourage good program design
A nested program would be called a function or
subroutine in any other language
Nested programs unleash the power of COBOL
pointers and allow COBOL programmers to
design data structures that encourage efficient
programming techniques
Nested Programs
PROGRAM-ID. MAIN.
PROGRAM-ID. SUB1.