MainframeMaster

COBOL Tutorial

COBOL METHOD-ID - Quick Reference

Progress0 of 0 lessons

Overview

The METHOD-ID statement in object-oriented COBOL defines a method within a class or interface. It specifies the method name, parameters, and implementation, allowing objects to perform specific operations.

Purpose and Usage

  • Define object behavior through methods
  • Encapsulate functionality within classes
  • Support object-oriented programming concepts
  • Enable method invocation on objects

Syntax

The METHOD-ID statement follows a specific structure:

Basic Syntax

cobol
1
2
3
4
5
6
7
8
9
METHOD-ID. method-name. DATA DIVISION. LINKAGE SECTION. 01 parameter1 PIC X(10). 01 parameter2 PIC 9(5). PROCEDURE DIVISION USING parameter1, parameter2. * Method implementation EXIT METHOD. END METHOD method-name.

Methods can have parameters and return values.

Example: Simple Method

cobol
1
2
3
4
5
6
7
8
METHOD-ID. GetName. DATA DIVISION. LINKAGE SECTION. 01 return-name PIC X(30). PROCEDURE DIVISION RETURNING return-name. MOVE employee-name TO return-name EXIT METHOD. END METHOD GetName.

Example: Method with Parameters

cobol
1
2
3
4
5
6
7
8
9
10
METHOD-ID. CalculateSalary. DATA DIVISION. LINKAGE SECTION. 01 hours-worked PIC 9(3). 01 hourly-rate PIC 9(3)V99. 01 total-salary PIC 9(6)V99. PROCEDURE DIVISION USING hours-worked, hourly-rate RETURNING total-salary. COMPUTE total-salary = hours-worked * hourly-rate EXIT METHOD. END METHOD CalculateSalary.

Practical Examples

Here are some practical uses of METHOD-ID in object-oriented COBOL:

Employee Class 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
CLASS-ID. Employee. ENVIRONMENT DIVISION. CONFIGURATION SECTION. OBJECT-COMPUTER. DATA DIVISION. WORKING-STORAGE SECTION. 01 employee-name PIC X(30). 01 employee-id PIC 9(6). 01 employee-salary PIC 9(7)V99. PROCEDURE DIVISION. METHOD-ID. SetName. DATA DIVISION. LINKAGE SECTION. 01 new-name PIC X(30). PROCEDURE DIVISION USING new-name. MOVE new-name TO employee-name EXIT METHOD. END METHOD SetName. METHOD-ID. GetSalary. DATA DIVISION. LINKAGE SECTION. 01 return-salary PIC 9(7)V99. PROCEDURE DIVISION RETURNING return-salary. MOVE employee-salary TO return-salary EXIT METHOD. END METHOD GetSalary. END CLASS Employee.

Methods for getting and setting employee data.

Calculator Methods

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
METHOD-ID. Add. DATA DIVISION. LINKAGE SECTION. 01 a PIC 9(5). 01 b PIC 9(5). 01 result PIC 9(6). PROCEDURE DIVISION USING a, b RETURNING result. COMPUTE result = a + b EXIT METHOD. END METHOD Add. METHOD-ID. Multiply. DATA DIVISION. LINKAGE SECTION. 01 a PIC 9(5). 01 b PIC 9(5). 01 result PIC 9(10). PROCEDURE DIVISION USING a, b RETURNING result. COMPUTE result = a * b EXIT METHOD. END METHOD Multiply.

Mathematical operation methods.

Best Practices

  • Use descriptive method names that clearly indicate their purpose.
  • Keep methods focused on a single responsibility.
  • Use appropriate parameter types and sizes.
  • Always use EXIT METHOD to properly end method execution.
  • Document method parameters and return values.

Common Pitfalls

  • Forgetting to use EXIT METHOD to end method execution.
  • Not properly defining parameter types in LINKAGE SECTION.
  • Using methods outside of class or interface definitions.
  • Mixing procedural and object-oriented concepts incorrectly.

Test Your Knowledge

1. What is the primary purpose of the METHOD-ID statement in COBOL?

  • To define a program ID
  • To define a method within a class or interface
  • To identify a file
  • To declare a variable

2. In which type of COBOL is METHOD-ID used?

  • Standard COBOL
  • Object-oriented COBOL
  • Procedural COBOL
  • Legacy COBOL

3. What sections can a METHOD-ID contain?

  • Only PROCEDURE DIVISION
  • DATA DIVISION and PROCEDURE DIVISION
  • ENVIRONMENT DIVISION, DATA DIVISION, and PROCEDURE DIVISION
  • All four divisions

4. How do you pass parameters to a method?

  • Using the USING clause
  • Using global variables
  • Using the WITH clause
  • Using the PASS clause

5. What is the difference between a method and a paragraph?

  • They are the same
  • Methods belong to classes, paragraphs belong to programs
  • Methods are faster
  • Paragraphs are obsolete

Frequently Asked Questions