Chuyển đến nội dung chính
eLearner.app
Mô-đun 3 · Bài học 1 trong tổng số 25/10 trong khóa học~12 min
Bài học theo mô-đun (1/2)

Đoạn văn và THỰC HIỆN

In COBOL, modularity and logical structuring of code inside the PROCEDURE DIVISION do not occur via functions or methods, but rather through Paragraphs and the PERFORM statement.

Defining Paragraphs

A paragraph is a labeled section of code starting with a name at column 8 (Area A) ending with a period ., followed by a sequence of instructions at column 12 (Area B).

Code
DISPLAY-MESSAGE.
    DISPLAY "This is a paragraph!".

Paragraphs allow splitting potentially very long programs into reusable and self-documenting blocks.

Executing Paragraphs with PERFORM

The PERFORM statement invokes and executes a paragraph, then returns control to the instruction immediately following the PERFORM statement.

Code
PROCEDURE DIVISION.
MAIN-PROCEDURE.
    DISPLAY "Starting program...".
    PERFORM PROCESS-DATA.
    DISPLAY "Ending program.".
    STOP RUN.

PROCESS-DATA.
    DISPLAY "Processing data in memory...".

In the above listing:

  1. The DISPLAY inside MAIN-PROCEDURE is executed.
  2. PERFORM PROCESS-DATA branches execution to the PROCESS-DATA paragraph.
  3. Once the PROCESS-DATA paragraph ends, execution returns to MAIN-PROCEDURE to print "Ending program." and halt using STOP RUN..

Try it yourself

tập thể dục#cobol.m3.l1.e1
Nỗ lực: 0Đang tải…

Define a paragraph named DISPLAY-TOTAL that prints 'TOTAL IS DONE' using DISPLAY, and call it inside MAIN-PROCEDURE using PERFORM before stopping the program.

Đang tải trình chỉnh sửa…
Hiển thị gợi ý

Write PERFORM DISPLAY-TOTAL. at column 12, and below the MAIN-PROCEDURE block declare the DISPLAY-TOTAL. paragraph at column 8.

Giải pháp khả dụng sau 3 lần thử

tập thể dục#cobol.m3.l1.e2
Nỗ lực: 0Đang tải…

Call two paragraphs sequentially, first INITIALIZE-DATA and then PROCESS-DATA, within the MAIN-PROCEDURE block.

Đang tải trình chỉnh sửa…
Hiển thị gợi ý

Insert the two PERFORM statements in sequential order at column 12.

Giải pháp khả dụng sau 3 lần thử