BudiBadu Logo
Samplebadu

COBOL by Example: Intrinsic Functions

COBOL 2002

Using built-in intrinsic functions from COBOL-85 and later standards, implementing string manipulation with UPPER-CASE and LOWER-CASE, performing mathematical calculations with SQRT MAX MIN, and simplifying code without custom subroutines.

Code

       IDENTIFICATION DIVISION.
       PROGRAM-ID. FUNC-DEMO.
       
       DATA DIVISION.
       WORKING-STORAGE SECTION.
       01  TEXT-VAR    PIC X(10) VALUE "Hello".
       01  NUM-VAR     PIC 99    VALUE 16.
       01  SQRT-VAL    PIC 99V99.
       
       PROCEDURE DIVISION.
           DISPLAY "Original: " TEXT-VAR.
           DISPLAY "Upper: " FUNCTION UPPER-CASE(TEXT-VAR).
           DISPLAY "Lower: " FUNCTION LOWER-CASE(TEXT-VAR).
           
           COMPUTE SQRT-VAL = FUNCTION SQRT(NUM-VAR).
           DISPLAY "Square Root of 16 is: " SQRT-VAL.
           STOP RUN.

Explanation

Since the COBOL-85 standard (and expanded in 2002), the language has included Intrinsic Functions. These provide standard capabilities like string manipulation, mathematical calculations, and date/time access without needing external subroutines.

To use a function, you typically prefix the function name with the keyword FUNCTION. Common functions include UPPER-CASE, LOWER-CASE, SQRT, MAX, MIN, and LENGTH. These functions return a temporary value that can be used in expressions or moved to a variable.

Intrinsic functions significantly reduce the amount of code needed for common tasks. Before their introduction, programmers had to write complex logic or call assembly routines to perform simple tasks like finding the square root or converting text case.

Code Breakdown

11
FUNCTION UPPER-CASE(TEXT-VAR). Converts the content of TEXT-VAR to uppercase. It returns a temporary result and does not modify TEXT-VAR itself.
14
FUNCTION SQRT(NUM-VAR). Calculates the square root. The result is used in the COMPUTE statement.
12
FUNCTION LOWER-CASE(TEXT-VAR). Similarly, converts text to lowercase.