MainframeMaster
MainframeMaster

COBOL Tutorial

Progress0 of 0 lessons

COBOL Program Communication

Program communication in COBOL enables one program to call and interact with another program, creating modular, maintainable, and reusable code structures. This fundamental capability allows you to break complex applications into smaller, manageable pieces, each handling specific responsibilities. Understanding program communication is essential for building professional mainframe applications that follow best practices for code organization and maintainability.

In this comprehensive guide, you'll learn how COBOL programs communicate with each other through the CALL statement, how to define and receive parameters using the LINKAGE SECTION, and the different methods of passing data between programs. We'll explore parameter passing mechanisms, error handling, and best practices that will help you write robust, efficient COBOL applications that leverage the power of modular programming.

Understanding Program Communication

Program communication in COBOL is the mechanism that allows programs to work together as a cohesive unit. When one program (the calling program) needs functionality provided by another program (the called program or subprogram), it uses the CALL statement to invoke that program. The called program executes, performs its task, and then returns control to the calling program, often passing back results or modified data.

This communication model enables several important programming concepts:

  • Modularity: Break large programs into smaller, focused modules that each handle a specific task. This makes programs easier to understand, test, and maintain.
  • Code Reuse: Write a program once and call it from multiple places, eliminating code duplication and ensuring consistency across your application.
  • Separation of Concerns: Each program can focus on a single responsibility, making the overall system design cleaner and more logical.
  • Team Development: Different programmers can work on different programs simultaneously, as long as they agree on the interface (parameters) between programs.

The communication between programs happens through parameters - data items passed from the calling program to the called program. These parameters can be input (data the called program needs), output (results the called program produces), or both (data that is modified by the called program).

The CALL Statement

The CALL statement is the primary mechanism for program communication in COBOL. It transfers control from the calling program to a subprogram, executes that subprogram, and then returns control to the statement immediately following the CALL when the subprogram completes.

Basic CALL Statement Syntax

The simplest form of the CALL statement invokes a program without passing any parameters:

cobol
1
CALL 'program-name'.

However, most real-world scenarios require passing data between programs. The CALL statement with parameters uses the USING clause:

cobol
1
2
3
4
5
6
7
8
CALL 'program-name' USING parameter-1 parameter-2 parameter-3 ON EXCEPTION error-handling-statements NOT ON EXCEPTION success-handling-statements END-CALL.

Let's break down each component:

  • 'program-name': The name of the program to call, specified as a literal (in quotes) or as a data item containing the program name. This must match the PROGRAM-ID of the called program.
  • USING: This clause lists the parameters to pass to the called program. Parameters are separated by spaces and passed in the order listed.
  • ON EXCEPTION: Optional clause that executes if the called program cannot be found or loaded. This is crucial for error handling in production applications.
  • NOT ON EXCEPTION: Optional clause that executes when the call succeeds. Useful for logging or performing follow-up actions after a successful call.
  • END-CALL: Explicitly marks the end of the CALL statement. While not always required, it's good practice for clarity and to avoid ambiguity.

Simple CALL Statement Example

Here's a basic example of a calling program that invokes a subprogram to calculate a total:

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
IDENTIFICATION DIVISION. PROGRAM-ID. MAIN-PROGRAM. *> This program calls a subprogram to calculate a total DATA DIVISION. WORKING-STORAGE SECTION. 01 CALCULATION-DATA. 05 FIRST-NUMBER PIC 9(5)V99 VALUE 100.50. 05 SECOND-NUMBER PIC 9(5)V99 VALUE 25.25. 05 RESULT PIC 9(6)V99 VALUE ZERO. PROCEDURE DIVISION. MAIN-LOGIC. DISPLAY 'Main program starting' DISPLAY 'First number: ' FIRST-NUMBER DISPLAY 'Second number: ' SECOND-NUMBER *> Call the calculation subprogram CALL 'CALCULATE-TOTAL' USING CALCULATION-DATA DISPLAY 'Result after call: ' RESULT DISPLAY 'Main program ending' STOP RUN.

In this example, the main program defines a group item CALCULATION-DATA containing two input numbers and a result field. It calls the subprogram 'CALCULATE-TOTAL', passing the entire group. The subprogram (which we'll see next) receives this data, performs the calculation, and places the result back into the same group item, which the main program then displays.

The LINKAGE SECTION

The LINKAGE SECTION is a special section in the DATA DIVISION of a called program where you define parameters received from the calling program. Unlike WORKING-STORAGE SECTION items, which have their own storage, LINKAGE SECTION items don't allocate storage - they reference the storage of the corresponding parameters in the calling program.

This is a crucial concept to understand: when you define an item in LINKAGE SECTION, you're not creating new storage; you're creating a reference (like a pointer) to the storage that already exists in the calling program. This means:

  • Changes made to LINKAGE items in the called program are immediately visible in the calling program (when using BY REFERENCE, which is the default).
  • The called program can both read from and write to these parameters, enabling two-way communication.
  • No data copying occurs (with BY REFERENCE), making it very efficient even for large data structures.

LINKAGE SECTION Structure

The LINKAGE SECTION appears in the DATA DIVISION, typically after the WORKING-STORAGE SECTION:

cobol
1
2
3
4
5
6
7
8
DATA DIVISION. WORKING-STORAGE SECTION. *> Local working storage items go here LINKAGE SECTION. *> Parameters from calling program go here 01 parameter-name-1 PIC data-type. 01 parameter-name-2 PIC data-type.

Called Program Example

Here's the called program that corresponds to our earlier calling program example:

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
IDENTIFICATION DIVISION. PROGRAM-ID. CALCULATE-TOTAL. *> This subprogram receives data, calculates a total, and returns it DATA DIVISION. WORKING-STORAGE SECTION. *> Local variables for this program only 01 WS-TEMP-RESULT PIC 9(7)V99. LINKAGE SECTION. *> Parameters received from calling program *> These reference the storage in the calling program 01 LK-CALCULATION-DATA. 05 LK-FIRST-NUMBER PIC 9(5)V99. 05 LK-SECOND-NUMBER PIC 9(5)V99. 05 LK-RESULT PIC 9(6)V99. PROCEDURE DIVISION USING LK-CALCULATION-DATA. *> The USING clause in PROCEDURE DIVISION must match LINKAGE items CALCULATE-LOGIC. DISPLAY 'Subprogram CALCULATE-TOTAL starting' DISPLAY 'Received first number: ' LK-FIRST-NUMBER DISPLAY 'Received second number: ' LK-SECOND-NUMBER *> Perform the calculation COMPUTE LK-RESULT = LK-FIRST-NUMBER + LK-SECOND-NUMBER DISPLAY 'Calculated result: ' LK-RESULT DISPLAY 'Subprogram CALCULATE-TOTAL ending' *> Return control to calling program EXIT PROGRAM.

Key points about this called program:

  • LINKAGE SECTION: Defines LK-CALCULATION-DATA with the same structure as the calling program's CALCULATION-DATA. The names don't have to match, but the structure (data types and sizes) should be compatible.
  • PROCEDURE DIVISION USING: Must include a USING clause that lists the linkage items in the same order as they appear in the CALL statement. This connects the parameters.
  • EXIT PROGRAM: Returns control to the calling program. The called program remains in memory and can be called again.
  • Modification: When the subprogram modifies LK-RESULT, it's actually modifying the RESULT field in the calling program's CALCULATION-DATA, because they share the same storage.

Parameter Passing Methods

COBOL provides three methods for passing parameters, each with different characteristics and use cases. Understanding these methods is crucial for writing correct and efficient programs.

BY REFERENCE (Default)

BY REFERENCE is the default method when no passing method is specified. It passes the address (memory location) of the parameter to the called program. This means:

  • The called program accesses the same memory location as the calling program.
  • Any changes made in the called program are immediately visible in the calling program.
  • No data copying occurs, making it very efficient, especially for large data structures.
  • Both programs are working with the same data, so synchronization is automatic.
cobol
1
2
3
4
*> BY REFERENCE is the default - these are equivalent: CALL 'PROGRAM-NAME' USING PARAM-1 PARAM-2. CALL 'PROGRAM-NAME' USING BY REFERENCE PARAM-1 BY REFERENCE PARAM-2.

Use BY REFERENCE when:

  • You want the called program to modify the parameter (output or input-output parameters).
  • You're passing large data structures and want to avoid copying overhead.
  • You want maximum efficiency and the parameter can be safely modified.

BY CONTENT

BY CONTENT creates a copy of the parameter's value and passes that copy to the called program. This means:

  • The called program receives a copy of the data, not the original.
  • Changes made in the called program do NOT affect the original data in the calling program.
  • Data copying occurs, which has a performance cost, especially for large items.
  • The original data is protected from modification.
cobol
1
2
3
*> Pass a copy - original is protected CALL 'PROGRAM-NAME' USING BY CONTENT INPUT-VALUE BY REFERENCE OUTPUT-RESULT.

Use BY CONTENT when:

  • You want to protect the original data from modification (input-only parameters).
  • The parameter is small enough that copying overhead is acceptable.
  • You want to ensure the calling program's data remains unchanged regardless of what the called program does.

BY VALUE

BY VALUE is similar to BY CONTENT but is typically used with numeric literals or constants. It passes the actual value rather than a reference:

cobol
1
2
3
*> Pass a literal value CALL 'PROGRAM-NAME' USING BY VALUE 100 BY VALUE 200.

BY VALUE is less commonly used in traditional COBOL but can be useful when passing constant values that don't need to be modified.

Mixed Parameter Passing Example

Here's a practical example showing different parameter passing methods:

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
IDENTIFICATION DIVISION. PROGRAM-ID. MAIN-PROGRAM. DATA DIVISION. WORKING-STORAGE SECTION. 01 CUSTOMER-ID PIC 9(8) VALUE 12345678. 01 ACCOUNT-BALANCE PIC S9(9)V99 VALUE 1000.00. 01 INTEREST-RATE PIC 9(2)V999 VALUE 0.050. 01 CALCULATED-INTEREST PIC S9(9)V99 VALUE ZERO. 01 ORIGINAL-BALANCE PIC S9(9)V99 VALUE 1000.00. PROCEDURE DIVISION. MAIN-LOGIC. DISPLAY 'Before call:' DISPLAY ' Customer ID: ' CUSTOMER-ID DISPLAY ' Account Balance: ' ACCOUNT-BALANCE DISPLAY ' Original Balance: ' ORIGINAL-BALANCE *> Pass customer ID by content (protect it) *> Pass balance by reference (allow modification) *> Pass interest rate by content (protect it) *> Pass calculated interest by reference (get result back) CALL 'CALCULATE-INTEREST' USING BY CONTENT CUSTOMER-ID BY REFERENCE ACCOUNT-BALANCE BY CONTENT INTEREST-RATE BY REFERENCE CALCULATED-INTEREST ON EXCEPTION DISPLAY 'Error: Could not call CALCULATE-INTEREST' STOP RUN NOT ON EXCEPTION DISPLAY 'Call successful' END-CALL DISPLAY 'After call:' DISPLAY ' Customer ID: ' CUSTOMER-ID ' (unchanged - passed BY CONTENT)' DISPLAY ' Account Balance: ' ACCOUNT-BALANCE ' (may be modified)' DISPLAY ' Original Balance: ' ORIGINAL-BALANCE ' (unchanged)' DISPLAY ' Calculated Interest: ' CALCULATED-INTEREST STOP RUN.

And the corresponding called program:

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
IDENTIFICATION DIVISION. PROGRAM-ID. CALCULATE-INTEREST. DATA DIVISION. WORKING-STORAGE SECTION. *> Local working storage LINKAGE SECTION. *> Parameters - order must match CALL statement 01 LK-CUSTOMER-ID PIC 9(8). 01 LK-ACCOUNT-BALANCE PIC S9(9)V99. 01 LK-INTEREST-RATE PIC 9(2)V999. 01 LK-CALCULATED-INTEREST PIC S9(9)V99. PROCEDURE DIVISION USING LK-CUSTOMER-ID LK-ACCOUNT-BALANCE LK-INTEREST-RATE LK-CALCULATED-INTEREST. CALCULATE-LOGIC. *> LK-CUSTOMER-ID is a copy (BY CONTENT) - can modify locally *> but won't affect original DISPLAY 'Processing customer: ' LK-CUSTOMER-ID *> LK-ACCOUNT-BALANCE is BY REFERENCE - modification affects original *> Calculate interest COMPUTE LK-CALCULATED-INTEREST = LK-ACCOUNT-BALANCE * LK-INTEREST-RATE *> Add interest to balance (modifies original in calling program) COMPUTE LK-ACCOUNT-BALANCE = LK-ACCOUNT-BALANCE + LK-CALCULATED-INTEREST DISPLAY 'Interest calculated: ' LK-CALCULATED-INTEREST DISPLAY 'New balance: ' LK-ACCOUNT-BALANCE EXIT PROGRAM.

In this example:

  • CUSTOMER-ID (BY CONTENT): Protected from modification. Even if the subprogram changes LK-CUSTOMER-ID, the original in the main program remains 12345678.
  • ACCOUNT-BALANCE (BY REFERENCE): Can be modified. When the subprogram adds interest to LK-ACCOUNT-BALANCE, it's actually modifying the original ACCOUNT-BALANCE in the main program.
  • INTEREST-RATE (BY CONTENT): Protected from modification.
  • CALCULATED-INTEREST (BY REFERENCE): Used to return the calculated interest value back to the main program.

Returning Control from Called Programs

When a called program completes its work, it must return control to the calling program. COBOL provides two primary statements for this: EXIT PROGRAM and GOBACK. Understanding the difference is important for proper program design.

EXIT PROGRAM

EXIT PROGRAM is the preferred statement for returning from a called program. It:

  • Returns control to the calling program at the statement following the CALL.
  • Keeps the called program in memory, so it can be called again without reloading.
  • Is explicit and clear about returning from a subprogram.
  • Preserves the program's state (WORKING-STORAGE values) between calls.
cobol
1
2
3
4
5
6
7
PROCEDURE DIVISION USING parameter-list. SUBPROGRAM-LOGIC. *> Perform processing PERFORM PROCESS-DATA *> Return to calling program EXIT PROGRAM.

GOBACK

GOBACK is a more general statement that can be used in both main and called programs:

  • In a called program, it works similarly to EXIT PROGRAM.
  • In the main program, it terminates the entire run unit.
  • Is more flexible but less explicit about intent.
cobol
1
2
3
4
5
6
7
PROCEDURE DIVISION USING parameter-list. SUBPROGRAM-LOGIC. *> Perform processing PERFORM PROCESS-DATA *> Return to calling program (or terminate if main program) GOBACK.

Best Practice: Use EXIT PROGRAM in called programs for clarity. Use GOBACK when you want a single statement that works in both contexts, or use STOP RUN in the main program to explicitly terminate the run unit.

Error Handling in Program Communication

Robust error handling is essential when programs communicate with each other. The called program might not be found, might fail during execution, or might return error indicators. Proper error handling makes your applications reliable and maintainable.

ON EXCEPTION Handling

The ON EXCEPTION clause handles cases where the called program cannot be found or loaded:

cobol
1
2
3
4
5
6
7
8
9
CALL 'PROGRAM-NAME' USING PARAM-1 PARAM-2 ON EXCEPTION DISPLAY 'Error: Program not found' MOVE 8 TO RETURN-CODE PERFORM ERROR-HANDLING-ROUTINE NOT ON EXCEPTION DISPLAY 'Program called successfully' PERFORM SUCCESS-HANDLING-ROUTINE END-CALL.

Return Code Pattern

A common pattern is to pass a return code parameter that the called program sets to indicate success or failure:

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
IDENTIFICATION DIVISION. PROGRAM-ID. MAIN-PROGRAM. DATA DIVISION. WORKING-STORAGE SECTION. 01 PROCESSING-DATA. 05 INPUT-VALUE PIC 9(5). 05 OUTPUT-RESULT PIC 9(6). 01 RETURN-CODE PIC 9(2). 88 SUCCESS VALUE 00. 88 ERROR-INVALID VALUE 01. 88 ERROR-SYSTEM VALUE 99. PROCEDURE DIVISION. MAIN-LOGIC. MOVE 12345 TO INPUT-VALUE MOVE 0 TO RETURN-CODE CALL 'PROCESS-DATA' USING PROCESSING-DATA RETURN-CODE ON EXCEPTION DISPLAY 'Fatal: Could not load PROCESS-DATA' MOVE 99 TO RETURN-CODE STOP RUN NOT ON EXCEPTION IF SUCCESS DISPLAY 'Processing successful: ' OUTPUT-RESULT ELSE DISPLAY 'Processing failed with code: ' RETURN-CODE PERFORM HANDLE-PROCESSING-ERROR END-IF END-CALL STOP RUN. HANDLE-PROCESSING-ERROR. *> Handle specific error conditions based on return code EVALUATE RETURN-CODE WHEN 01 DISPLAY 'Invalid input data' WHEN 02 DISPLAY 'Resource unavailable' WHEN OTHER DISPLAY 'Unknown error' END-EVALUATE.

And the called program sets the return code:

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
IDENTIFICATION DIVISION. PROGRAM-ID. PROCESS-DATA. DATA DIVISION. LINKAGE SECTION. 01 LK-PROCESSING-DATA. 05 LK-INPUT-VALUE PIC 9(5). 05 LK-OUTPUT-RESULT PIC 9(6). 01 LK-RETURN-CODE PIC 9(2). PROCEDURE DIVISION USING LK-PROCESSING-DATA LK-RETURN-CODE. PROCESS-LOGIC. *> Validate input IF LK-INPUT-VALUE = ZERO MOVE 01 TO LK-RETURN-CODE *> Invalid input EXIT PROGRAM END-IF *> Perform processing COMPUTE LK-OUTPUT-RESULT = LK-INPUT-VALUE * 2 *> Set success return code MOVE 00 TO LK-RETURN-CODE EXIT PROGRAM.

The CANCEL Statement

The CANCEL statement removes a called program from memory. This is useful when you want to:

  • Reset a program's WORKING-STORAGE to initial values before the next call.
  • Free memory resources when you're done with a program.
  • Ensure a fresh start for a program that maintains state between calls.
cobol
1
2
3
4
5
6
7
8
*> Call a program multiple times CALL 'PROGRAM-NAME' USING PARAM-1 *> Later, cancel it to reset its state CANCEL 'PROGRAM-NAME' *> Next call will reload the program with fresh WORKING-STORAGE CALL 'PROGRAM-NAME' USING PARAM-2

Important Notes:

  • CANCEL is optional - programs are automatically canceled when the run unit ends.
  • After CANCEL, the next CALL must reload the program, which has a small performance cost.
  • Use CANCEL when you need to reset state, not for every call.

Nested Program Calls

COBOL supports nested program calls - a called program can itself call another program. This creates a call chain or call stack. Control returns through the chain when each program completes.

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
IDENTIFICATION DIVISION. PROGRAM-ID. MAIN-PROGRAM. DATA DIVISION. WORKING-STORAGE SECTION. 01 DATA-ITEM PIC 9(5). PROCEDURE DIVISION. MAIN-LOGIC. DISPLAY 'Main program calling LEVEL-1' CALL 'LEVEL-1' USING DATA-ITEM DISPLAY 'Main program received control back' STOP RUN. *> ============================================ IDENTIFICATION DIVISION. PROGRAM-ID. LEVEL-1. DATA DIVISION. LINKAGE SECTION. 01 LK-DATA-ITEM PIC 9(5). PROCEDURE DIVISION USING LK-DATA-ITEM. LEVEL-1-LOGIC. DISPLAY 'LEVEL-1 calling LEVEL-2' CALL 'LEVEL-2' USING LK-DATA-ITEM DISPLAY 'LEVEL-1 received control back' EXIT PROGRAM. *> ============================================ IDENTIFICATION DIVISION. PROGRAM-ID. LEVEL-2. DATA DIVISION. LINKAGE SECTION. 01 LK-DATA-ITEM PIC 9(5). PROCEDURE DIVISION USING LK-DATA-ITEM. LEVEL-2-LOGIC. DISPLAY 'LEVEL-2 processing' ADD 100 TO LK-DATA-ITEM EXIT PROGRAM.

In this example, the call chain is: MAIN-PROGRAM → LEVEL-1 → LEVEL-2. When LEVEL-2 executes EXIT PROGRAM, control returns to LEVEL-1. When LEVEL-1 executes EXIT PROGRAM, control returns to MAIN-PROGRAM. The modification made in LEVEL-2 is visible all the way back to MAIN-PROGRAM because parameters are passed BY REFERENCE by default.

Complete Example: Customer Account Processing

Let's put it all together with a comprehensive example that demonstrates program communication in a realistic scenario:

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
IDENTIFICATION DIVISION. PROGRAM-ID. ACCOUNT-MANAGER. *> Main program that manages customer accounts DATA DIVISION. WORKING-STORAGE SECTION. 01 CUSTOMER-ACCOUNT. 05 CUSTOMER-ID PIC 9(8) VALUE 12345678. 05 ACCOUNT-BALANCE PIC S9(9)V99 VALUE 5000.00. 05 ACCOUNT-TYPE PIC X(1) VALUE 'C'. 88 CHECKING-ACCOUNT VALUE 'C'. 88 SAVINGS-ACCOUNT VALUE 'S'. 01 TRANSACTION-DATA. 05 TRANSACTION-AMOUNT PIC S9(9)V99 VALUE 250.00. 05 TRANSACTION-TYPE PIC X(1) VALUE 'D'. 88 DEPOSIT VALUE 'D'. 88 WITHDRAWAL VALUE 'W'. 01 PROCESSING-RESULT. 05 NEW-BALANCE PIC S9(9)V99. 05 PROCESSING-STATUS PIC X(1). 88 SUCCESS VALUE 'S'. 88 FAILURE VALUE 'F'. 05 ERROR-MESSAGE PIC X(50). 01 RETURN-CODE PIC 9(2) VALUE 0. PROCEDURE DIVISION. MAIN-LOGIC. DISPLAY '=== Account Manager Starting ===' DISPLAY 'Customer ID: ' CUSTOMER-ID DISPLAY 'Current Balance: $' ACCOUNT-BALANCE DISPLAY 'Transaction: $' TRANSACTION-AMOUNT ' (' TRANSACTION-TYPE ')' *> Call validation program CALL 'VALIDATE-TRANSACTION' USING BY CONTENT CUSTOMER-ACCOUNT BY CONTENT TRANSACTION-DATA BY REFERENCE PROCESSING-RESULT BY REFERENCE RETURN-CODE ON EXCEPTION DISPLAY 'ERROR: Validation program not found' MOVE 99 TO RETURN-CODE STOP RUN NOT ON EXCEPTION IF RETURN-CODE NOT = 0 DISPLAY 'Validation failed: ' ERROR-MESSAGE STOP RUN END-IF END-CALL IF SUCCESS *> Call processing program CALL 'PROCESS-TRANSACTION' USING BY REFERENCE CUSTOMER-ACCOUNT BY CONTENT TRANSACTION-DATA BY REFERENCE PROCESSING-RESULT BY REFERENCE RETURN-CODE ON EXCEPTION DISPLAY 'ERROR: Processing program not found' MOVE 99 TO RETURN-CODE NOT ON EXCEPTION IF RETURN-CODE = 0 MOVE NEW-BALANCE TO ACCOUNT-BALANCE DISPLAY 'Transaction processed successfully' DISPLAY 'New Balance: $' ACCOUNT-BALANCE ELSE DISPLAY 'Processing failed: ' ERROR-MESSAGE END-IF END-CALL END-IF DISPLAY '=== Account Manager Ending ===' STOP RUN.

Validation subprogram:

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
IDENTIFICATION DIVISION. PROGRAM-ID. VALIDATE-TRANSACTION. *> Validates transaction before processing DATA DIVISION. LINKAGE SECTION. 01 LK-CUSTOMER-ACCOUNT. 05 LK-CUSTOMER-ID PIC 9(8). 05 LK-ACCOUNT-BALANCE PIC S9(9)V99. 05 LK-ACCOUNT-TYPE PIC X(1). 01 LK-TRANSACTION-DATA. 05 LK-TRANSACTION-AMOUNT PIC S9(9)V99. 05 LK-TRANSACTION-TYPE PIC X(1). 01 LK-PROCESSING-RESULT. 05 LK-NEW-BALANCE PIC S9(9)V99. 05 LK-PROCESSING-STATUS PIC X(1). 05 LK-ERROR-MESSAGE PIC X(50). 01 LK-RETURN-CODE PIC 9(2). PROCEDURE DIVISION USING LK-CUSTOMER-ACCOUNT LK-TRANSACTION-DATA LK-PROCESSING-RESULT LK-RETURN-CODE. VALIDATE-LOGIC. *> Initialize MOVE 'F' TO LK-PROCESSING-STATUS MOVE SPACES TO LK-ERROR-MESSAGE MOVE 1 TO LK-RETURN-CODE *> Validate customer ID IF LK-CUSTOMER-ID = ZERO OR LK-CUSTOMER-ID > 99999999 MOVE 'Invalid customer ID' TO LK-ERROR-MESSAGE EXIT PROGRAM END-IF *> Validate transaction amount IF LK-TRANSACTION-AMOUNT <= ZERO MOVE 'Transaction amount must be positive' TO LK-ERROR-MESSAGE EXIT PROGRAM END-IF *> Validate withdrawal doesn't exceed balance IF LK-TRANSACTION-TYPE = 'W' IF LK-TRANSACTION-AMOUNT > LK-ACCOUNT-BALANCE MOVE 'Insufficient funds' TO LK-ERROR-MESSAGE EXIT PROGRAM END-IF END-IF *> Validation successful MOVE 'S' TO LK-PROCESSING-STATUS MOVE 0 TO LK-RETURN-CODE EXIT PROGRAM.

Processing subprogram:

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
IDENTIFICATION DIVISION. PROGRAM-ID. PROCESS-TRANSACTION. *> Processes the validated transaction DATA DIVISION. LINKAGE SECTION. 01 LK-CUSTOMER-ACCOUNT. 05 LK-CUSTOMER-ID PIC 9(8). 05 LK-ACCOUNT-BALANCE PIC S9(9)V99. 05 LK-ACCOUNT-TYPE PIC X(1). 01 LK-TRANSACTION-DATA. 05 LK-TRANSACTION-AMOUNT PIC S9(9)V99. 05 LK-TRANSACTION-TYPE PIC X(1). 01 LK-PROCESSING-RESULT. 05 LK-NEW-BALANCE PIC S9(9)V99. 05 LK-PROCESSING-STATUS PIC X(1). 05 LK-ERROR-MESSAGE PIC X(50). 01 LK-RETURN-CODE PIC 9(2). PROCEDURE DIVISION USING LK-CUSTOMER-ACCOUNT LK-TRANSACTION-DATA LK-PROCESSING-RESULT LK-RETURN-CODE. PROCESS-LOGIC. *> Process based on transaction type EVALUATE LK-TRANSACTION-TYPE WHEN 'D' *> Deposit - add to balance COMPUTE LK-NEW-BALANCE = LK-ACCOUNT-BALANCE + LK-TRANSACTION-AMOUNT MOVE LK-NEW-BALANCE TO LK-ACCOUNT-BALANCE WHEN 'W' *> Withdrawal - subtract from balance COMPUTE LK-NEW-BALANCE = LK-ACCOUNT-BALANCE - LK-TRANSACTION-AMOUNT MOVE LK-NEW-BALANCE TO LK-ACCOUNT-BALANCE WHEN OTHER MOVE 'Invalid transaction type' TO LK-ERROR-MESSAGE MOVE 2 TO LK-RETURN-CODE EXIT PROGRAM END-EVALUATE *> Success MOVE 'S' TO LK-PROCESSING-STATUS MOVE 0 TO LK-RETURN-CODE EXIT PROGRAM.

This example demonstrates:

  • Multiple program calls in sequence (validation, then processing).
  • Mixed parameter passing (BY CONTENT for input, BY REFERENCE for output).
  • Comprehensive error handling with return codes and error messages.
  • Real-world data structures and business logic.
  • Clear separation of concerns (validation separate from processing).

Best Practices for Program Communication

Following best practices ensures your programs are maintainable, reliable, and efficient:

1. Always Use ON EXCEPTION

Always include ON EXCEPTION handling in production code. Programs can fail to load for various reasons (missing program, authorization issues, system problems), and your application should handle these gracefully rather than terminating unexpectedly.

2. Document Parameter Interfaces

Clearly document what each parameter represents, whether it's input, output, or both, and what values are expected. This is crucial when multiple programmers work on different programs.

3. Match Data Structures Carefully

Ensure LINKAGE SECTION structures match the calling program's parameters in order, type, and size. Mismatches can cause data corruption or program failures. Use consistent naming conventions (e.g., LK- prefix for linkage items) to make the distinction clear.

4. Use Appropriate Parameter Passing

Choose BY REFERENCE for large structures or when modification is needed. Use BY CONTENT for input-only parameters that should be protected. This balances efficiency with data safety.

5. Implement Return Codes

Use return codes to indicate success or failure, and different codes for different error conditions. This allows calling programs to handle errors appropriately.

6. Keep Programs Focused

Each program should have a single, well-defined responsibility. This makes programs easier to understand, test, and maintain.

7. Use EXIT PROGRAM in Called Programs

Use EXIT PROGRAM rather than GOBACK in called programs for clarity. It explicitly indicates returning from a subprogram.

Common Pitfalls and How to Avoid Them

Understanding common mistakes helps you write better code:

Parameter Order Mismatch

Problem: Parameters in CALL don't match LINKAGE SECTION order.

Solution: Always verify parameter order matches between CALL statement, LINKAGE SECTION, and PROCEDURE DIVISION USING clause. Use consistent naming to make this obvious.

Forgetting ON EXCEPTION

Problem: Program terminates unexpectedly when called program is missing.

Solution: Always include ON EXCEPTION handling, especially in production code.

Modifying BY CONTENT Parameters

Problem: Expecting modifications to BY CONTENT parameters to affect the calling program.

Solution: Remember that BY CONTENT creates a copy. Use BY REFERENCE if you need modifications to be visible.

Incorrect LINKAGE Section Structure

Problem: LINKAGE items don't match the structure of parameters being passed.

Solution: Ensure data types, sizes, and structures match exactly. Use copy books or shared data definitions when possible.

Explain Like I'm Five

Imagine you're working on a big school project, but instead of doing everything yourself, you ask your friends to help with different parts. Program communication in COBOL is like that!

The main program is like you - the project manager. When you need something done, you "call" one of your friends (another program) and give them the information they need (parameters). Your friend does their part of the work, maybe changes some of the information you gave them, and then gives everything back to you. You continue with your project, now with the updated information.

BY REFERENCE is like giving your friend the actual notebook page - if they write on it, you see the changes. BY CONTENT is like making a photocopy - your friend can write on their copy, but your original stays the same.

The LINKAGE SECTION is like your friend's instruction sheet - it tells them what information you're giving them and in what order. They need to follow it exactly, or things get mixed up!

Summary

Program communication is a fundamental aspect of COBOL programming that enables modular, maintainable applications. The CALL statement invokes subprograms, the LINKAGE SECTION receives parameters, and various passing methods (BY REFERENCE, BY CONTENT, BY VALUE) provide flexibility in how data is shared. Proper error handling, return codes, and following best practices ensure robust, reliable programs that work together seamlessly.

By mastering program communication, you can build complex mainframe applications that are well-organized, testable, and maintainable. Each program can focus on its specific responsibility, and programs can be developed, tested, and maintained independently while working together as a cohesive system.

Test Your Knowledge

1. What is the primary purpose of the CALL statement in COBOL?

  • To terminate a program
  • To invoke another COBOL program and transfer control to it
  • To define parameters
  • To open files

2. Where are parameters received in a called program defined?

  • WORKING-STORAGE SECTION
  • FILE SECTION
  • LINKAGE SECTION
  • LOCAL-STORAGE SECTION

3. What is the default parameter passing method in COBOL?

  • BY CONTENT
  • BY REFERENCE
  • BY VALUE
  • BY COPY

4. What happens when you use BY CONTENT to pass a parameter?

  • The called program can modify the original data
  • A copy of the parameter is passed, protecting the original
  • The parameter is passed by value only for numeric data
  • The parameter cannot be used in the called program

5. Which statement should be used to return from a called program?

  • STOP RUN
  • EXIT PROGRAM
  • RETURN
  • END PROGRAM

6. What does the CANCEL statement do?

  • Cancels a file operation
  • Removes a called program from memory
  • Cancels a transaction
  • Stops program execution

7. What happens if a called program cannot be found?

  • The program continues execution
  • The ON EXCEPTION clause is executed if specified
  • The system automatically creates the program
  • An error is ignored

8. Can a called program call another program?

  • No, only the main program can call other programs
  • Yes, COBOL supports nested program calls
  • Only if both programs are in the same file
  • Only with special system permissions

Related Pages