Calling a Subprogram• Syntax for CALL CALL “subprog name” [ USING [BY REFERENCE | BY CONTENT] ident1 …] END-CALL • The subprog name usually refers to an 8 byte field that contains th
Trang 1Advanced COBOL
Trang 2IBM Enterprise COBOL for z/OS
http://www-306.ibm.com/software/awdtools/cobol/zos/library/
Especially helpful for programmers:
Language Reference Manual
Programming Guide
Trang 7QSAM File Processing
Queued Sequential Access
Method
Trang 8QSAM Files
• Unkeyed, Sequentially created and processed
• Records cannot change length or position
• QSAM files on direct access storage can be modified with REWRITE
Trang 10Environment Division - File Control
Trang 11Environment Division
File-CONTROL
• Optional – used for files opened in I-O, INPUT,
or EXTEND File doesn’t have to be present
when the program is executed.
• File-name1 – identifies an FD entry (internal file name)
• Assignment-name – identifies the external file If name component of the SELECT clause is found
in the JCL it is treated as a DD name If not
found in the JCL, then “name” is treated an an
environment variable
Trang 12QSAM File Name
name
label-
• Label – documents for the programmer the device and device class to
which the file is assigned No effect on execution Must end with a
dash
• S – Optional Indicates sequential organization
Trang 13Environment Variables
Trang 14Exercise A (Dynamic Files)
• Statically allocate and read
BCST.SICCC01.TESTPDS(DYNAMDAT)
• This file contains member names of other members in BCST.SICCC01.TESTPDS
• Dynamically Read each member that is listed and
display the records in each member
• After you can display all the records, try writing out the records to a dynamically allocated file
• Use BCST.SICCC01.PDSLIB(DYNAM2) to help you read a file dynamically
• Use BCSC.SICCC01.PDSLIB(DYNAM1) to help you write a file dynamically
Trang 16Reserve Clause (optional)
RESERVE integer
AREA
AREAS
Trang 17RESERVE Clause
• Specifies the number of I/O buffers
allocated the file at run-time
• If omitted, the number of buffers is taken from the DD statement If none are
specified, the system default is taken
Trang 18QSAM Buffering
• QSAM buffers can be allocated above the 16 MB line if all of the following are true:
- Enterprise COBOL
- z/OS Language Environment
- the programs are compiled with RENT and DATA(31)
or
compiled with NORENT and | RMODE(ANY)
- the program is executing in AMODE 31
- the program is executing on MVS
- the ALL31(ON) run-time option is used (for
EXTERNAL files)
Trang 19ORGANIZATION Clause (optional)
• ORGANIZATION IS SEQUENTIAL
• Other non-QSAM options: INDEXED,
RELATIVE, LINE SEQUENTIAL
• Records are read and written in a serial manner
Trang 20PADDING Clause
PADDING data name CHARACTER IS literal
Specifies a character for block padding on sequential files
Data name – a one character field
Literal – a one character alphanumeric literal or national symbol
Trang 21ACCESS MODE Clause
ACCESS SEQUENTIAL MODE IS
Default mode is SEQUENTIAL
Options for other types of files include RANDOM and DYNAMIC
Trang 22FILE STATUS Clause
STATUS dname1
FILE IS dname2
- The operating system moves a value to dname1 and possibly dname2 after each I/O operation.
-dname1 - a two character alphanumeric or national field
-dname2 – used for VSAM
Trang 23APPLY WRITE-ONLY ON MYFILE.
(Used for sequential variable blocked files.)
Trang 24Defining QSAM Files and Records
FILE-CONTROL
SELECT CUSTOMER-MASTER ASSIGN TO CUSTMASTORGANIZATION IS SEQUENTIALACCESS MODE IS SEQUENTIAL FILE STATUS IS RC
Trang 25DATA DIVISION FILE SECTION - Sequential
Trang 27DATA DIVISION
FILE SECTION - FD
Trang 28• The EXTERNAL clause specifies that a file connector is external, permitting file sharing between two programs in the
same run unit
Trang 29• GLOBAL clause specifies the
file-connector name is available to the declaring program and all programs contained directly or indirectly
• Used for nested programs
Trang 30BLOCK CONTAINS
• BLOCK CONTAINS 0 RECORDS
• If this clause is omitted, records are unblocked
by default!
• Allows the blocksize to be specified in the JCL or
by the operating system
• Code this Statement! (TSYS Standard)
Trang 31• RECORD CONTAINS 80 CHARACTERS
• RECORD CONTAINS 50 TO 80 CHARACTERS
• RECORD IS VARYING IN SIZE
FROM 40 TO 60 CHARACTERS
DEPENDING ON REC-COUNT.
Trang 32RECORDING MODE
• Specifies the format of physical records in
a QSAM file (ignored for VSAM)
• F – fixed size, V – variable size, U –
unblocked, fixed or variable, S – spanned, large records that span a block
• RECORDING MODE IS F
• RECORDING MODE IS V
• RECORDING MODE IS U
• RECORDING MODE IS S
Trang 33DATA RECORD Clause
• DATA RECORD clause identifies the data areas associated with the file
• Syntax checked but is only documentation
DATA RECORD IS INPUT-AREA
DATA RECORDS ARE INPUT-AREA1
INPUT-AREA2
Trang 34FD Example
FD IN-FILE IS GLOBAL
RECORDING MODE F
BLOCK CONTAINS 0 RECORDS
LABEL RECORDS ARE STANDARD
RECORD CONTAINS 80 CHARACTERS DATA RECORD IS IN-AREA.
01 IN-AREA.
05 …
Trang 35
LABEL RECORDS
• Label records are records written at the
beginning and end of DASD and Tape
files that provide information about file
• Enterprise COBOL only supports standard labels
LABEL RECORDS ARE STANDARD
LABEL RECORDS ARE OMITTED
Trang 36Subprograms
Trang 37Calling a Subprogram
• Syntax for CALL
CALL “subprog name”
[ USING [BY REFERENCE | BY CONTENT]
ident1 …]
END-CALL
• The subprog name usually refers to an 8 byte field that contains the program name to be called
• Static call is made when subprogram name is
hard-coded and compiler option = NODYNAM
• Subprogram can be written in any supported
language
Trang 38• The variable-name usually refers to an 8 byte
field that contains the program name to be called
• Names can be longer with Enterprise COBOL
• The variable-name can be modified as the
program is running to call different programs
Trang 39Calling a Subprogram
• Linking to the called program is dynamic
• At TSYS, all calls are dynamic ( DYNAM
compiler option)
• BY REFERENCE is the default
• BY REFERENCE provides the subprogram with access to a main program variable The
receiving variable is an alias for the passed
variable
• BY CONTENT provides the subprogram with
access to a copy of a main program variable
Trang 40Calling a Subprogram
• BY CONTENT ADDRESS OF provides a copy of the address of the passed variable (must be a linkage area name)
• BY CONTENT LENGTH provides a copy
of the length of a variable
Trang 41Example Parameters
Trang 42The Called Program
• Specifies the names of the receiving variables with a USING
statement in the PROCEDURE DIVISION statement or in an
PROCEDURE DIVISION USING A COST RESULT.
• The variables in the using statement are 01 group items defined in the LINKAGE SECTION or 77 items
LINKAGE SECTION.
01 A PIC X(8).
O1 COST PIC S9(5) PACKED-DECIMAL.
01 RESULT PIC S9(5) BINARY.
Trang 43The Called Program
• The called program can return values to the calling program by modifying variables that are passed by reference
PROCEDURE DIVISION USING
COST.
…
MOVE ITEM-COST TO COST
Trang 44• Call the subprogram
• Print “I am in the subprogram” in the
subprogram
• Print “I am back in the main program” in the main
Trang 45Exercise #2
• Create a two variables X and Y in the main program (you pick the type and value).
• Print the values of X and Y in the main program
• Pass X BY REFERENCE and Y BY CONTENT to the
subprogram
• Print the variables in the subprogram
• Change the values of each variable in the subprogram
• Print the length of x by passing the length using BY
CONTENT LENGTH (Receiving variable PIC S9(8)
BINARY)
• Print the values of the variables again in the main
program
Trang 46Canceling a Subprogram
• CANCEL syntax
CANCEL literal
CANCEL identifier
• Canceling a program means the program will be
in its initial state if the program is called again
• Canceling a program closes all files associated with an internal file connector of the canceled
program
• No action is taken when canceling a previously canceled program or one that has not been
dynamically called
Trang 47Exercise #3
• Have the main program call a subprogram four times
• Create a local numeric variable Z in the
subprogram with initial value 1
• Each time the program is called, print Z
and then add 1 to it
• Repeat the experiment after adding “IS
INITIAL” to the PROGRAM-ID
Trang 48• Subprograms remain in their last used
state when they terminate with EXIT
PROGRAM or GOBACK
• A program that is coded with INITIAL will always be called with its initial state
Trang 49
Exercise #4
• Repeat Exercise #3, canceling each program after each subprogram call
Trang 50Return Codes
• Use the RETURN-CODE special register
to test and transmit return codes through register 15
• After calling a subprogram, test CODE to see if the subprogram completed normally
• At the end of a suprogram, set CODE to indicate the results of the call
Trang 51RETURN-Exercise #5
Write a main program that passes a numeric
parameter, say X, to a subprogram If the
parameter is negative have the subprogram set
a return code of 4 If the parameter is
non-negative, the subprogram should set the return code to 0 Have the main program test the
return code after the subprogram has
completed The main program should print a message indicating the type of number the
subprogram received Try running the main
program passing negative and non-negative
values for X.
Trang 54Reading Records
• READ MYFILE INTO MY-RECORD
– Move mode IO – Data flows from an I/O buffer into working storage.
• READ MYFILE
– Locate mode I/O – Data remains in an I/O
buffer Efficient processing technique
– TSYS standard
Trang 55External Files
• Make the file status field external so there
is only one shared field for all programs Each program declares:
01 MYSTATUS PIC 99 EXTERNAL
• Be sure to work in locate-mode
Trang 56Exercise #6
• Write a main program that opens a sequential file and calls a subprogram each time it needs a record Write a subprogram that reads a single record and returns to the main program Have the main program print all the records in the
sequential file and then close the file.
• Share the same file between the two programs
by making the file external with a shared file
status field.
Trang 57PROCEDURE DIVISION…
RETURNING
• An alternate form of passing information back to a calling program is provided:
PROCEDURE DIVISION RETURNING dataname
• To call a “Function” the invocation is:
CALL program-name RETURNING dataname
• Avoid this alternative in favor of Pass By
Reference.
Trang 58Nested Programs
• Avoided in production programs at TSYS
• Convenient for developing (one file, one compilation)
• Nested programs can be separated easily into regular programs after debugging
• Can be used instead of PERFORM
• CALL to a nested program is as efficient
as a PERFORM
• Each program ends with END PROGRAM
Trang 59Nested Program Structure
X1
Trang 60Exercise #7
• Convert one of your main programs and
subprograms to a nested program version
• Canceling only makes sense for
dynamically called programs
• Cause an abend in your subprogram
Look at the storage dump and error
information Is it any harder to debug than
a regular program?
Trang 61COBOL is Recursive Now
• A COBOL program can call itself
• To make a program recursive, add “IS RECURSIVE” to the PROGRAM-ID
statement
PROGRAM-ID SUBPROG IS RECURSIVE.
• Nested programs cannot be recursive
Trang 62Passing a Parm with JCL
• A parm can be coded on the EXEC statement in order to pass a parameter to the program that is being executed: // EXEC PGM=PROGNAME,PARM=‘HI there!'
• The COBOL program will receive the parm through the LINKAGE SECTION
• Code a LINKAGE SECTION description similar to this:
01 PARM-BUFF.
05 PARM-LEN PIC S9(4) BINARY.
05 PARM-DATA PIC X(256).
• Code a using statement on the PROCEDURE DIVISION
PROCEDURE DIVISION USING PARM-BUFF.
Trang 63Passing a Parm with JCL
• The parm field is variable in length
• Use the length field and reference
modification to move variable length data MOVE PARM-DATA(1:PARM-LEN)TO PARMO
Trang 65Omitted Parameters
• You can leave out some arguments when coding
a CALL statement by coding OMITTED in place
of the passed variable
CALL “THATPROG” USING P1,OMITTED,P3
• Test for the OMITTED parameter by checking to see if the address of the received parm is NULL PROCEDURE DIVISION USING X Y Z.
…
IF ADDRESS OF Y = NULL
DISPLAY “PARM Y WAS NOT PASSED”
END-IF
Trang 67Tables
Trang 68Creating A Single Dimension Table
• Build a storage area with list of data
values defined with multiple picture
clauses
• Redefine the storage area as a single
dimension table by defining a typical table entry as an “occuring” item
Trang 69Creating A Single Dimension Table
01 DAY-TABLE-VALUES.
05 PIC X(9) VALUE 'SUNDAY '.
05 PIC X(9) VALUE 'MONDAY '.
05 PIC X(9) VALUE 'TUESDAY '.
05 PIC X(9) VALUE 'WEDNESDAY'.
05 PIC X(9) VALUE 'THURSDAY '.
05 PIC X(9) VALUE 'FRIDAY '.
05 PIC X(9) VALUE 'SATURDAY '
01 DAY-TABLE REDEFINES DAY-TABLE-VALUES.
05 WEEKDAY PIC X(9) OCCURS 7 TIMES.
Trang 70“Fat” Single-Dimension Tables
Trang 72Exercise #9
• Implement a single dimension table of
days Print the table from beginning to
end
• Turn the table into a “fat” table by adding a column with the number of letters in each day name
• Print each day name and the number of
letters it contains
Trang 74HRS(1,1) HRS(2,1) HRS(3,1)
HRS(1,2) HRS(1,3) HRS(2,2) HRS(2,3)
HRS(3,3) HRS(3,2)
EMPLOYEE(3)
Trang 75Exercise #10
• Create a table of integers with 4 rows and
5 columns
• Print the table row by row
• Print the table column by column
• Compute and print the sum of each row
• Compute and print the sum of each
column
• Compute and print the sum of all entries in the table
Trang 76Creating Tables with Indexes
Trang 77Subscripts vs Indexes
• Subscripts
– Represent an occurrence number
– User defined as a numeric field – best to choose USAGE IS BINARY
– Printable (since they are numeric)
– Can use relative subscripts J+1 or J-3 – Manipulated with PERFORM loops,
assignments, and arithmetic commands
Trang 78Subscripts vs Indexes
• Indexes
– Represent a displacement value from the start
of a table.
– More efficient than subscripts
– Created automatically when a table is defined with indexes
– Not really designed to be printed
– Manipulated with PERFORM loops, and SET statements
Trang 80Exercise #11
• Convert Exercise #10 so that you are using indexes instead of subscripts
Trang 81Sequential Search
• COBOL provides a SEARCH command that provides a sequential search for
tables that have indexes
• Table entries do not have to be sorted
• AT END clause provides code in the
situation that the search is unsuccessful
• Searching starts with the current index value
Trang 82SEARCH
Trang 86Exercise #12
• Create a fat single dimension table with
the data in the file DATA1 Read the file and store the second (Item #) and third
fields (Item name) in the table
• Assume a fixed size table of 40 items
• Sequentially search the table for item #s in the range 400 to 450 Print out the results
of the search
Trang 87Binary Searching
• Entire table is searched No need to initialize an index
• Table must have an ASCENDING or
DESCENDING KEY IS clause Table must be sorted.
• Only one WHEN clause allowed The WHEN
clause is comprised of one or more “equal” tests joined by AND operators
• AT END clause is invoked if the WHEN clause is never satisfied