Master Mainframe Technologies - COBOL, JCL, DB2, VSAM, CICS & More
ABEND Codes SQLCODEs File Status Interview Prep Contact
💻 COBOL

COBOL FUNCTION-POINTER

Advanced 🕑 12 min read 👁 0 views

COBOL FUNCTION-POINTER

Function pointers allow dynamic invocation of programs and methods.

Definition

```cobol 01 WS-FUNC-PTR USAGE FUNCTION-POINTER. ```

Setting Function Pointer

```cobol SET WS-FUNC-PTR TO ENTRY 'SUBPROG'. ```

Calling via Function Pointer

```cobol CALL WS-FUNC-PTR USING param-1 param-2. ```

Procedure Pointers

```cobol 01 WS-PROC-PTR USAGE PROCEDURE-POINTER. SET WS-PROC-PTR TO ENTRY 'CALCPROG'. CALL WS-PROC-PTR USING BY REFERENCE data-1. ```

Use Cases

  • Callback functions
  • Plugin architectures
  • Runtime program selection
  • Strategy patterns

Comparing Pointers

```cobol IF WS-FUNC-PTR = NULL DISPLAY 'Pointer not set' END-IF. ```

Code Example

       IDENTIFICATION DIVISION.
       PROGRAM-ID. FUNC-PTR-DEMO.
       DATA DIVISION.
       WORKING-STORAGE SECTION.
       01 WS-FUNC-PTR FUNCTION-POINTER.
       01 WS-RESULT PIC 9(5).
       01 WS-INPUT PIC 9(5) VALUE 100.
       PROCEDURE DIVISION.
      * Set function pointer
           SET WS-FUNC-PTR TO ENTRY 'CALCPROG'
           
      * Call via pointer
           IF WS-FUNC-PTR NOT = NULL
               CALL WS-FUNC-PTR USING WS-INPUT WS-RESULT
               DISPLAY 'Result: ' WS-RESULT
           ELSE
               DISPLAY 'Function pointer not set'
           END-IF
           STOP RUN.