MainframeMaster
MainframeMaster

COBOL Tutorial

Progress0 of 0 lessons

COBOL USING Clause

The USING clause in the PROCEDURE DIVISION header specifies which LINKAGE SECTION items receive parameters from a calling program. When a program is called with the CALL statement using parameters, those parameters are passed to the items listed in the USING clause. The USING clause connects the calling program's data to the called program's LINKAGE SECTION definitions, enabling efficient parameter passing between COBOL programs.

What is the USING Clause?

The USING clause appears in the PROCEDURE DIVISION header and lists the LINKAGE SECTION items that receive parameters. Key characteristics:

  • Parameter specification: Identifies which parameters the program receives
  • LINKAGE connection: Links to items defined in LINKAGE SECTION
  • Position-based: Parameters matched by position and order
  • Optional: Only needed if program receives parameters
  • Bidirectional: Allows both input and output parameters

Basic Syntax

cobol
1
PROCEDURE DIVISION [USING parameter-1 [parameter-2 ...]].

The USING clause lists one or more LINKAGE SECTION items, separated by spaces.

Complete 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
IDENTIFICATION DIVISION. PROGRAM-ID. PROCESS-TRANSACTION. DATA DIVISION. LINKAGE SECTION. 01 INPUT-PARAMETERS. 05 CUSTOMER-ID PIC 9(8). 05 TRANSACTION-AMOUNT PIC 9(9)V99. 05 OPERATION-TYPE PIC X. 01 OUTPUT-PARAMETERS. 05 RETURN-CODE PIC 9(4). 05 RETURN-MESSAGE PIC X(50). 05 NEW-BALANCE PIC 9(9)V99. PROCEDURE DIVISION USING INPUT-PARAMETERS OUTPUT-PARAMETERS. MAIN-LOGIC. *> Access input parameters DISPLAY 'Processing customer: ' CUSTOMER-ID DISPLAY 'Amount: ' TRANSACTION-AMOUNT *> Process transaction PERFORM PROCESS-TRANSACTION-LOGIC *> Set output parameters MOVE 0 TO RETURN-CODE MOVE 'Transaction successful' TO RETURN-MESSAGE MOVE 1500.00 TO NEW-BALANCE EXIT PROGRAM.

The calling program would call it like this:

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
WORKING-STORAGE SECTION. 01 INPUT-DATA. 05 CUST-ID PIC 9(8) VALUE 12345678. 05 TRANS-AMT PIC 9(9)V99 VALUE 500.00. 05 OP-TYPE PIC X VALUE 'D'. 01 OUTPUT-DATA. 05 RET-CODE PIC 9(4). 05 RET-MESSAGE PIC X(50). 05 NEW-BAL PIC 9(9)V99. PROCEDURE DIVISION. MAIN-PARA. CALL 'PROCESS-TRANSACTION' USING INPUT-DATA OUTPUT-DATA IF RET-CODE = 0 DISPLAY 'Success: ' RET-MESSAGE DISPLAY 'New balance: ' NEW-BAL END-IF STOP RUN.

Parameter Matching

Parameters are matched by position:

  • First parameter in CALL matches first item in USING
  • Second parameter matches second item, and so on
  • Order must match exactly between CALL and USING
  • Data structures should match in size and type

Multiple Parameters

You can specify multiple parameters in the USING clause:

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
LINKAGE SECTION. 01 PARAM-1 PIC X(10). 01 PARAM-2 PIC 9(5). 01 PARAM-3 PIC X(20). PROCEDURE DIVISION USING PARAM-1 PARAM-2 PARAM-3. MAIN-LOGIC. *> Access all three parameters DISPLAY 'Param 1: ' PARAM-1 DISPLAY 'Param 2: ' PARAM-2 DISPLAY 'Param 3: ' PARAM-3 EXIT PROGRAM.

USING with Main Programs

Main programs can use USING to receive system parameters:

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
IDENTIFICATION DIVISION. PROGRAM-ID. MAIN-PROGRAM. DATA DIVISION. LINKAGE SECTION. 01 SYSTEM-PARM PIC X(100). PROCEDURE DIVISION USING SYSTEM-PARM. MAIN-PARA. *> SYSTEM-PARM receives parameters from JCL PARM *> or operating system DISPLAY 'System parameter: ' SYSTEM-PARM STOP RUN.

Best Practices

Follow these best practices when using USING:

  • Match structures: Ensure CALL and USING parameter structures match
  • Document parameters: Clearly document what each parameter represents
  • Validate parameters: Check parameter values before use
  • Use meaningful names: Name LINKAGE items clearly
  • Order consistently: Maintain consistent parameter order
  • Handle errors: Check for invalid parameter values

Common Mistakes

Avoid these common mistakes:

  • Parameter count mismatch: Different number of parameters in CALL and USING
  • Wrong order: Parameters in different order than expected
  • Structure mismatch: Data structures don't match between caller and called
  • Missing LINKAGE: Items in USING not defined in LINKAGE SECTION
  • Uninitialized parameters: Not validating parameter values

Explain Like I'm 5: USING

Think of USING like a delivery system:

  • The calling program is like a sender with packages
  • The USING clause is like a list of delivery addresses
  • LINKAGE SECTION is like the mailboxes at those addresses
  • Parameters are like the packages being delivered
  • Position matching is like matching package 1 to address 1, package 2 to address 2

The USING clause tells the program "these are the mailboxes where I'll receive the packages (parameters) from the sender (calling program)"!

Test Your Knowledge

1. Where is the USING clause specified?

  • In the DATA DIVISION
  • In the PROCEDURE DIVISION header
  • In the ENVIRONMENT DIVISION
  • In WORKING-STORAGE SECTION

2. What must be defined before using items in the USING clause?

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

3. How are parameters matched between CALL and USING?

  • By name
  • By position and order
  • By data type only
  • Automatically by compiler

4. Can a program have no USING clause?

  • No, USING is required
  • Yes, if the program receives no parameters
  • Only for main programs
  • Only for subprograms

5. What is the default parameter passing method with USING?

  • BY CONTENT
  • BY REFERENCE
  • BY VALUE
  • No default

Related Concepts

Related Pages