IBM - VisualAge Cobol for Windows Programming Guide
634 pág.

IBM - VisualAge Cobol for Windows Programming Guide


DisciplinaCobol25 materiais168 seguidores
Pré-visualização50 páginas
data items are reinitialized on the first reentry into
the program following the CANCEL.
WORKING-STORAGE is deallocated at the termination of the run unit.
LOCAL-STORAGE is allocated each time a program is called, and is deallocated when
the program returns by means of an EXIT PROGRAM, GOBACK, or STOP RUN. Note
however that for nested programs, LOCAL-STORAGE is allocated upon entry to, and
deallocated upon exit from, the containing outermost program. Any LOCAL-STORAGE
data items with VALUE clauses in a nested program are initialized to the appropriate
value each time the nested program is called.
RELATED CONCEPTS
\u201cExample: storage sections\u201d on page 13
RELATED TASKS
\u201cEnding and reentering main programs or subprograms\u201d on page 360
12 Programming Guide
RELATED REFERENCES
Working-Storage section (IBM COBOL Language Reference)
Local-Storage section (IBM COBOL Language Reference)
Example: storage sections
The following is an example of a recursive program that uses both
WORKING-STORAGE and LOCAL-STORAGE.
CBL apost,pgmn(lu)
*********************************
* Recursive Program - Factorials
*********************************
IDENTIFICATION DIVISION.
Program-Id. factorial recursive.
ENVIRONMENT DIVISION.
DATA DIVISION.
Working-Storage Section.
01 numb pic 9(4) value 5.
01 fact pic 9(8) value 0.
Local-Storage Section.
01 num pic 9(4).
PROCEDURE DIVISION.
move numb to num.
if numb = 0
move 1 to fact
else
subtract 1 from numb
call 'factorial'
multiply num by fact
end-if.
display num '! = ' fact.
goback.
End Program factorial.
The following tables show the changing values of the data items in LOCAL-STORAGE
(L-S) and WORKING-STORAGE (W-S) in the successive recursive calls of the program,
and in the ensuing gobacks. During the gobacks, fact progressively accumulates
the value of 5! (five factorial).
Recursive
CALLs: Main 1 2 3 4 5
___________________________________
L-S num 5 4 3 2 1 0
___________________________________
W-S numb 5 4 3 2 1 0
fact 0 0 0 0 0 0
___________________________________
Recursive
GOBACKs: 5 4 3 2 1 Main
____________________________________
L-S num 0 1 2 3 4 5
____________________________________
W-S numb 0 0 0 0 0 0
fact 1 1 2 6 24 120
____________________________________
RELATED CONCEPTS
\u201cComparison of WORKING-STORAGE and LOCAL-STORAGE\u201d on page 12
Using data from another program
How you share data depends on whether the programs are separately compiled or
are nested, as discussed in the topics referenced below.
Chapter 1. Structuring your program 13
RELATED TASKS
\u201cSharing data in separately compiled programs\u201d
\u201cSharing data in nested programs\u201d
\u201cSharing data in recursive or multithreaded programs\u201d
Sharing data in separately compiled programs
Many applications consist of separately compiled programs that call and pass data
to one another. Use the LINKAGE SECTION in the called program to describe the data
passed from another program. In the calling program, use a CALL . . . USING or
INVOKE . . . USING statement to pass the data.
RELATED TASKS
\u201cPassing data\u201d on page 373
Sharing data in nested programs
Some applications consist of nested programs\u2014programs that are contained in
other programs. Level-01 LINKAGE SECTION data items can include the GLOBAL
attribute. This attribute allows any nested program that includes the declarations
to access these LINKAGE SECTION data items.
A nested program can also access data items in a sibling program (one at the same
nesting level in the same containing program) that is declared with the COMMON
attribute.
RELATED CONCEPTS
\u201cNested programs\u201d on page 361
Sharing data in recursive or multithreaded programs
If you compile your program as RECURSIVE or with the THREAD option, data defined
in the LINKAGE SECTION might not be accessible between entries.
To address a record in the LINKAGE SECTION, use either of these techniques:
v Pass an argument to the program and specify the record in an appropriate
position in the USING phrase in the program.
v Use the format-5 SET statement.
If you compile your program as RECURSIVE or with the THREAD option, the address
to that record is valid for a particular instance of the program invocation. The
address to the record in another execution instance of the same program must be
reestablished for that execution instance. Unpredictable results will occur if you
make reference to a data item whose address has not been established.
RELATED CONCEPTS
\u201cMultithreading\u201d on page 403
RELATED TASKS
\u201cMaking recursive calls\u201d on page 370
RELATED REFERENCES
SET statement (IBM COBOL Language Reference)
Processing the data
In the PROCEDURE DIVISION of a program, you code the executable statements that
process the data you have defined in the other divisions. The PROCEDURE DIVISION
contains one or two headers and the logic of your program.
14 Programming Guide
|
|
|
|
|
|
|
|
|
|
|
The PROCEDURE DIVISION begins with the division header and a procedure-name
header. The division header for a program can simply be:
PROCEDURE DIVISION.
You can code your division header to receive parameters with the USING phrase or
to return a value with the RETURNING phrase.
To receive an argument that was passed by reference (the default) or by content,
code the division header for a program in either of these ways:
PROCEDURE DIVISION USING dataname
PROCEDURE DIVISION USING BY REFERENCE dataname
Be sure to define the dataname in the LINKAGE SECTION of the DATA DIVISION.
To receive a parameter that was passed by value, code the division header for a
program as follows:
PROCEDURE DIVISION USING BY VALUE dataname
To return a value as a result, code the division header as follows:
PROCEDURE DIVISION RETURNING dataname2
You can also combine USING and RETURNING in a PROCEDURE DIVISION header:
PROCEDURE DIVISION USING dataname RETURNING dataname2
Be sure to define dataname and dataname2 in the LINKAGE SECTION of the DATA
DIVISION.
RELATED CONCEPTS
\u201cHow logic is divided in the PROCEDURE DIVISION\u201d
RELATED TASKS
\u201cEliminating repetitive coding\u201d on page 463
How logic is divided in the PROCEDURE DIVISION
The PROCEDURE DIVISION of a program is divided into sections and paragraphs,
which contain sentences and statements:
Section
Logical subdivision of your processing logic.
A section has a section header and is optionally followed by one or more
paragraphs.
A section can be the subject of a PERFORM statement. One type of section is
for declaratives.
Paragraph
Subdivision of a section, procedure, or program.
A paragraph has a name followed by a period and zero or more sentences.
A paragraph can be the subject of a statement.
Sentence
Series of one or more COBOL statements ending with a period.
Many structured programs do not have separate sentences. Each paragraph
can contain one sentence.
Chapter 1. Structuring your program 15
Statement
Performs a defined step of COBOL processing, such as adding two
numbers.
A statement is a valid combination of words, beginning with a COBOL
verb. Statements are imperative (indicating unconditional action),
conditional, or compiler-directing. Using explicit scope terminators instead
of periods to show the logical end of a statement is preferred.
Phrase
A subdivision of a statement.
RELATED CONCEPTS
\u201cCompiler-directing statements\u201d on page 17
\u201cScope terminators\u201d on page 17
\u201cImperative statements\u201d
\u201cConditional statements\u201d
\u201cDeclaratives\u201d on page 19
RELATED REFERENCES
PROCEDURE DIVISION structure (IBM COBOL Language Reference)
Imperative statements
An imperative statement indicates an unconditional action to be taken (such as
ADD, MOVE, INVOKE, or CLOSE).
An imperative statement can be ended with an implicit or explicit scope
terminator.
A conditional statement that ends with an explicit scope