MainframeMaster

COBOL Tutorial

COBOL END-METHOD Clause - Quick Reference

Progress0 of 0 lessons

Overview

The END-METHOD clause is used to terminate a method definition in object-oriented COBOL. This clause marks the end of a method scope and completes the method structure, working in conjunction with the METHOD-ID clause to define complete method definitions within classes.

Purpose and Usage

  • Method termination - Mark the end of a method definition
  • Scope management - Close method scope properly
  • Structure completion - Complete method structure
  • OOP support - Support object-oriented programming
  • Compilation aid - Help compiler understand method boundaries

Method Structure Concept

Method Definition: [METHOD-ID] → [Method Content] → [END-METHOD]
Method Scope: [Start of Method] → [Logic/Operations] → [End of Method]
Structure: [METHOD-ID method-name] → [Method Body] → [END-METHOD]
END-METHOD properly terminates the method definition

END-METHOD provides proper method termination and scope management.

Syntax

The END-METHOD clause follows specific syntax patterns and is used to terminate method definitions in object-oriented COBOL.

Basic Syntax

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
* Basic END-METHOD clause syntax METHOD-ID method-name. * Method content goes here * Logic, operations, etc. END METHOD. * Examples METHOD-ID CalculateTotal. * Calculation logic here END METHOD. METHOD-ID ValidateData. * Validation logic here END METHOD. * Complete example METHOD-ID CONSTRUCTOR. DATA DIVISION. LINKAGE SECTION. 01 PARAM1 PIC X(50). 01 PARAM2 PIC 9(5). PROCEDURE DIVISION USING PARAM1 PARAM2. MOVE PARAM1 TO INSTANCE-VAR1 MOVE PARAM2 TO INSTANCE-VAR2 END METHOD. METHOD-ID GET-VALUE. DATA DIVISION. LINKAGE SECTION. 01 RETURN-VALUE PIC X(50). PROCEDURE DIVISION RETURNING RETURN-VALUE. MOVE INSTANCE-VAR1 TO RETURN-VALUE END METHOD.

END-METHOD terminates a method definition that was started with METHOD-ID.

Method Structure Requirements

ComponentRequiredPurpose
METHOD-IDYesStart method definition
Method contentOptionalLogic, operations, data
END-METHODYesTerminate method definition
PROCEDURE DIVISIONOptionalMethod logic implementation

Multiple Methods 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
34
35
36
37
38
39
40
41
* Multiple methods in a single class CLASS-ID CustomerClass. DATA DIVISION. WORKING-STORAGE SECTION. 01 CUSTOMER-NAME PIC X(50). 01 CUSTOMER-ID PIC 9(5). METHOD-ID CONSTRUCTOR. DATA DIVISION. LINKAGE SECTION. 01 CUST-NAME PIC X(50). 01 CUST-ID PIC 9(5). PROCEDURE DIVISION USING CUST-NAME CUST-ID. MOVE CUST-NAME TO CUSTOMER-NAME MOVE CUST-ID TO CUSTOMER-ID END METHOD. METHOD-ID GET-NAME. DATA DIVISION. LINKAGE SECTION. 01 RETURN-NAME PIC X(50). PROCEDURE DIVISION RETURNING RETURN-NAME. MOVE CUSTOMER-NAME TO RETURN-NAME END METHOD. METHOD-ID GET-ID. DATA DIVISION. LINKAGE SECTION. 01 RETURN-ID PIC 9(5). PROCEDURE DIVISION RETURNING RETURN-ID. MOVE CUSTOMER-ID TO RETURN-ID END METHOD. METHOD-ID UPDATE-NAME. DATA DIVISION. LINKAGE SECTION. 01 NEW-NAME PIC X(50). PROCEDURE DIVISION USING NEW-NAME. MOVE NEW-NAME TO CUSTOMER-NAME END METHOD. END-CLASS.

Each method must be properly terminated with its own END-METHOD clause.

Common Use Cases

END-METHOD is commonly used in specific scenarios where object-oriented programming with methods is needed.

Constructor Methods

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
* Constructor method for object initialization METHOD-ID CONSTRUCTOR. DATA DIVISION. LINKAGE SECTION. 01 INIT-NAME PIC X(50). 01 INIT-ID PIC 9(5). 01 INIT-ADDRESS PIC X(100). PROCEDURE DIVISION USING INIT-NAME INIT-ID INIT-ADDRESS. MOVE INIT-NAME TO CUSTOMER-NAME MOVE INIT-ID TO CUSTOMER-ID MOVE INIT-ADDRESS TO CUSTOMER-ADDRESS MOVE "ACTIVE" TO CUSTOMER-STATUS MOVE FUNCTION CURRENT-DATE TO CREATION-DATE END METHOD.

Define constructor methods for object initialization.

Getter and Setter 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
* Getter and setter methods for data access METHOD-ID GET-CUSTOMER-NAME. DATA DIVISION. LINKAGE SECTION. 01 RETURN-NAME PIC X(50). PROCEDURE DIVISION RETURNING RETURN-NAME. MOVE CUSTOMER-NAME TO RETURN-NAME END METHOD. METHOD-ID SET-CUSTOMER-NAME. DATA DIVISION. LINKAGE SECTION. 01 NEW-NAME PIC X(50). PROCEDURE DIVISION USING NEW-NAME. IF NEW-NAME NOT = SPACES MOVE NEW-NAME TO CUSTOMER-NAME MOVE FUNCTION CURRENT-DATE TO LAST-UPDATE-DATE END-IF END METHOD. METHOD-ID GET-CUSTOMER-ID. DATA DIVISION. LINKAGE SECTION. 01 RETURN-ID PIC 9(5). PROCEDURE DIVISION RETURNING RETURN-ID. MOVE CUSTOMER-ID TO RETURN-ID END METHOD.

Create getter and setter methods for controlled data access.

Business Logic 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
41
42
43
* Business logic methods for complex operations METHOD-ID CALCULATE-TOTAL-BALANCE. DATA DIVISION. LINKAGE SECTION. 01 RETURN-BALANCE PIC 9(10)V99. PROCEDURE DIVISION RETURNING RETURN-BALANCE. COMPUTE TOTAL-BALANCE = CHECKING-BALANCE + SAVINGS-BALANCE MOVE TOTAL-BALANCE TO RETURN-BALANCE END METHOD. METHOD-ID VALIDATE-CUSTOMER-DATA. DATA DIVISION. LINKAGE SECTION. 01 IS-VALID PIC X. PROCEDURE DIVISION RETURNING IS-VALID. IF CUSTOMER-NAME NOT = SPACES AND CUSTOMER-ID > 0 AND CUSTOMER-ADDRESS NOT = SPACES MOVE "Y" TO IS-VALID ELSE MOVE "N" TO IS-VALID END-IF END METHOD. METHOD-ID PROCESS-TRANSACTION. DATA DIVISION. LINKAGE SECTION. 01 TRANSACTION-AMOUNT PIC 9(8)V99. 01 TRANSACTION-TYPE PIC X. 01 SUCCESS-FLAG PIC X. PROCEDURE DIVISION USING TRANSACTION-AMOUNT TRANSACTION-TYPE RETURNING SUCCESS-FLAG. IF TRANSACTION-TYPE = "D" ADD TRANSACTION-AMOUNT TO ACCOUNT-BALANCE MOVE "Y" TO SUCCESS-FLAG ELSE IF TRANSACTION-TYPE = "W" IF ACCOUNT-BALANCE >= TRANSACTION-AMOUNT SUBTRACT TRANSACTION-AMOUNT FROM ACCOUNT-BALANCE MOVE "Y" TO SUCCESS-FLAG ELSE MOVE "N" TO SUCCESS-FLAG END-IF END-IF END METHOD.

Define business logic methods for complex operations.

Best Practices and Tips

Following these best practices ensures effective use of the END-METHOD clause for proper method structure and termination.

END-METHOD Design Principles

  • Always include END-METHOD - Never forget to terminate method definitions
  • Match METHOD-ID and END-METHOD - Ensure proper method scope closure
  • Use meaningful method names - Choose descriptive method names
  • Organize method logic - Structure method operations logically
  • Document method purpose - Clearly document what each method does
  • Test method functionality - Verify that methods work correctly

Common Pitfalls to Avoid

PitfallProblemSolution
Missing END-METHODCompilation errors, undefined method scopeAlways include END-METHOD after method definition
Mismatched METHOD-ID/END-METHODMethod structure errorsEnsure each METHOD-ID has corresponding END-METHOD
Poor method organizationDifficult to maintain and understandOrganize method logic logically
Unclear method namesConfusing code structureUse descriptive and meaningful method names
Not documenting methodsDifficult to understand method purposeDocument method purpose and functionality

Performance Considerations

  • Method call overhead - Method calls have performance overhead
  • Parameter passing - Parameter passing may have performance impact
  • Memory usage - Methods consume additional memory
  • Compilation time - Object-oriented features may increase compilation time
  • Runtime performance - Consider performance impact of OOP features
  • Method complexity - Complex methods may affect performance

When to Use END-METHOD

Use CaseEND-METHOD SuitabilityReasoning
Object-oriented programmingEssentialRequired for proper method structure
Class method definitionExcellentPerfect for defining class behaviors
Code organizationGoodHelps organize code into logical units
Traditional COBOLNot applicableOnly for object-oriented COBOL
Simple programsPoorUnnecessary complexity for simple logic

END-METHOD Clause Quick Reference

UsageSyntaxExample
Basic method terminationEND METHOD.END METHOD.
Complete method structureMETHOD-ID name. ... END METHOD.METHOD-ID MyMethod. ... END METHOD.
Multiple methodsMultiple METHOD-ID/END METHOD pairsMETHOD-ID Method1. ... END METHOD.
METHOD-ID Method2. ... END METHOD.
Method with parametersMETHOD-ID name. ... PROCEDURE DIVISION USING params. ... END METHOD.METHOD-ID MyMethod. ... PROCEDURE DIVISION USING PARAM1. ... END METHOD.
Method with return valueMETHOD-ID name. ... PROCEDURE DIVISION RETURNING value. ... END METHOD.METHOD-ID MyMethod. ... PROCEDURE DIVISION RETURNING RESULT. ... END METHOD.

Test Your Knowledge

1. What is the primary purpose of the END-METHOD clause in COBOL?

  • To define data types
  • To terminate a method definition in object-oriented COBOL
  • To control file operations
  • To perform calculations

2. What must precede the END-METHOD clause?

  • Any COBOL statement
  • A METHOD-ID clause
  • A PROCEDURE DIVISION
  • A DATA DIVISION

3. What is the relationship between METHOD-ID and END-METHOD?

  • They are independent
  • METHOD-ID starts a method, END-METHOD ends it
  • They serve the same purpose
  • They are optional

4. When is END-METHOD most useful?

  • In traditional COBOL programs
  • In object-oriented COBOL programs with methods
  • Only during compilation
  • Only during program termination

5. How does END-METHOD relate to object-oriented programming?

  • They are completely independent
  • END-METHOD is essential for proper method structure in OOP
  • END-METHOD overrides OOP features
  • They serve the same purpose

Frequently Asked Questions