Menu
CoddyTech

Intrinsic Functions

Part of the Tables and Text section of Coddy's COBOL Programming journey. Lesson 15 of 19.

COBOL has built-in functions. A call is the word FUNCTION, the name and the arguments in brackets:

DISPLAY FUNCTION UPPER-CASE(CITY).

Text functions: UPPER-CASE, LOWER-CASE, REVERSE, and TRIM, which drops the spaces at both ends. Calls nest:

DISPLAY "[" FUNCTION TRIM(NAME-IN) "]".
DISPLAY FUNCTION UPPER-CASE(
    FUNCTION TRIM(NAME-IN)).

NUMVAL turns text such as " 12.50" into a number. TEST-NUMVAL returns 0 when the text is a valid number, so a program checks first:

IF FUNCTION TEST-NUMVAL(PRICE-IN) = 0
    COMPUTE PRICE =
        FUNCTION NUMVAL(PRICE-IN)
END-IF.

Number functions include MAX, MIN, MOD and INTEGER. Store a numeric result with COMPUTE before displaying it: displayed directly, FUNCTION MOD(17 5) prints 0000000002.

challenge icon

Challenge

Easy

The program reads a price as text into PRICE-IN. Compute TOTAL as that price plus 2 for shipping, with FUNCTION NUMVAL, so 12.50 prints Total:   14.50.

Example input, one line per ACCEPT: 12.50.

Try it yourself

       IDENTIFICATION DIVISION.
       PROGRAM-ID. SHIPPING.
       DATA DIVISION.
       WORKING-STORAGE SECTION.
       01 PRICE-IN PIC X(10).
       01 TOTAL PIC 9(4)V99.
       01 TOTAL-OUT PIC Z(3)9.99.
       PROCEDURE DIVISION.
           ACCEPT PRICE-IN.
           *> Compute TOTAL here
           MOVE TOTAL TO TOTAL-OUT.
           DISPLAY "Total: " TOTAL-OUT.
           STOP RUN.
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Tables and Text

Practice on your own: Online COBOL compiler