COBOL COBOL 2014, GnuCOBOL 3.x Β·
β verified by execution on 2026-08-16
Modern COBOL includes a comprehensive library of built-in routines known as intrinsic functions. These functions allow you to perform a wide variety of common tasks, such as complex string manipulation, advanced date formatting, and mathematical calculations. By utilizing these built-in routines, you are saved from having to write and maintain extensive low-level code for operations that are standard across many applications. They bring a level of convenience and power to COBOL that developers accustomed to other programming languages might not expect to find in legacy codebases.
Calling Functions
You invoke an intrinsic function by using the FUNCTION keyword, followed immediately by the name of the function you wish to execute. Functions return a computed value. This value is never modifying the original variables passed to the function. Instead, the newly computed value can be displayed directly to the terminal, moved into another variable using the MOVE statement, or used as part of a larger calculation within a COMPUTE statement.
cobolβ verified output
>>SOURCE FORMAT IS FREEIDENTIFICATION DIVISION.PROGRAM-ID. BASIC.DATA DIVISION.WORKING-STORAGE SECTION.01 WS-STR PIC X(5) VALUE "cobol".PROCEDURE DIVISION. DISPLAY FUNCTION UPPER-CASE(WS-STR). STOP RUN.
Output
COBOL
Your output
If you prefer a more modern coding style and wish to omit the FUNCTION keyword throughout your entire program, you can declare FUNCTION ALL INTRINSIC within your REPOSITORY section in the ENVIRONMENT DIVISION. This makes your code slightly cleaner and easier to read, especially in programs that heavily rely on intrinsic functions.
cobolβ verified output
>>SOURCE FORMAT IS FREEIDENTIFICATION DIVISION.PROGRAM-ID. OPT.ENVIRONMENT DIVISION.CONFIGURATION SECTION.REPOSITORY. FUNCTION ALL INTRINSIC.DATA DIVISION.WORKING-STORAGE SECTION.01 WS-STR PIC X(5) VALUE "cobol".PROCEDURE DIVISION. DISPLAY UPPER-CASE(WS-STR). STOP RUN.
Output
COBOL
Your output
System Dates
Retrieving the current date and time is a very common requirement in business applications. The CURRENT-DATE function makes this incredibly simple. It evaluates the system clock at the exact moment the statement is executed. It then returns a 21-character alphanumeric string. This string contains the current calendar date, the precise time of day, and the time differential from Greenwich mean time, all formatted in a standard way.
cobolβ verified output
>>SOURCE FORMAT IS FREEIDENTIFICATION DIVISION.PROGRAM-ID. DATES.PROCEDURE DIVISION. DISPLAY FUNCTION LENGTH(FUNCTION CURRENT-DATE). STOP RUN.
Output
000000021
Your output
Parsing Numeric Strings
Data read from external files, databases, or user input frequently contains leading spaces, trailing signs, or other formatting characters. Attempting direct arithmetic on these alphanumeric fields will typically cause your COBOL program to crash with a data exception. The NUMVAL function provides a safe and reliable way to convert these alphanumeric strings into true numeric values that can be safely used in calculations.
cobolβ verified output
>>SOURCE FORMAT IS FREEIDENTIFICATION DIVISION.PROGRAM-ID. NUMV.DATA DIVISION.WORKING-STORAGE SECTION.01 WS-NUM PIC S9(4).PROCEDURE DIVISION. COMPUTE WS-NUM = FUNCTION NUMVAL(" -42 ") + 10. DISPLAY WS-NUM. STOP RUN.
Output
-0032
Your output
Statistical Functions
While many intrinsic functions expect a fixed number of arguments, statistical functions like MAX and MIN are designed to be flexible. They can accept an arbitrary list of arguments instead of just one or two. This allows you to easily find the largest or smallest value from a dynamic set of variables or literals.
cobolβ verified output
>>SOURCE FORMAT IS FREEIDENTIFICATION DIVISION.PROGRAM-ID. MAXM.PROCEDURE DIVISION. DISPLAY FUNCTION MAX(10, 50, 20). STOP RUN.
Output
50
Your output
Common Misconceptions
When working with string manipulation functions in COBOL, it is critical to remember that COBOL treats fixed-length strings literally. This often leads to unexpected results if you are not careful with how your variables are defined and padded.
Predict the outputcobol
Read the code. What exactly will it print? Commit to an answer before you look.
>>SOURCE FORMAT IS FREEIDENTIFICATION DIVISION.PROGRAM-ID. REV.DATA DIVISION.WORKING-STORAGE SECTION.01 WS-STR PIC X(10) VALUE "CodeLudo".PROCEDURE DIVISION. DISPLAY FUNCTION REVERSE(WS-STR). STOP RUN.
Output
oduLedoC
You predicted
In the example above, WS-STR is explicitly defined as being 10 characters long. The word βCodeLudoβ is only 8 characters. Therefore, the string is automatically padded with two spaces at the end when it is stored in memory. The REVERSE function acts on the full length of the variable, flipping the entire 10-character string. This turns those trailing spaces into leading spaces in the final output. If you only want to reverse the actual text, you must use reference modification to limit the functionβs input to the exact length of the text.
Using Functions in Expressions
Here is a visual breakdown of how a function call fits into a standard COBOL COMPUTE statement:
Function Call Syntax
Check yourself
What does the REVERSE function do when applied to a fixed-length string?
Reveal answer
It reverses the entire string, including spaces, which moves trailing spaces to the front. β REVERSE acts on the full length of the variable. If you reverse "COBOL " (padded to 10 characters), you get " LOBOC".
How many arguments can statistical functions like MAX or MIN accept?
Reveal answer
An arbitrary number of arguments. β Functions like MAX and MIN can accept a variable number of arguments, e.g., FUNCTION MAX(10, 50, 20).
When does CURRENT-DATE determine the system time?
Reveal answer
At the exact moment the statement is executed. β CURRENT-DATE evaluates the system clock at the exact moment the statement is executed, returning a 21-character alphanumeric string.
How do intrinsic functions affect the variables passed to them?
Reveal answer
They return a new computed value without touching the original variable. β Intrinsic functions return a new value which must be moved, displayed, or used in an expression. The original variable is untouched.
Challenges
π Bug Hunt+15 XP
The author tried to reverse the string 'COBOL' stored in a PIC X(10) variable, but got leading spaces in the output. Fix the code to output 'LOBOC' without leading spaces by using reference modification inside the function call.
This code runs. It just does the wrong thing. Read it, find the defect,
fix it β the tests below decide when you are right.
cobolβ solution verified at build time
COBOL has no in-browser runtime, so this one is pen-and-paper: work out what the
fixed code should be, then open the solution and compare. The expected output
below is real β it came from compiling the solution with GnuCOBOL when this page
was built.
Test 1 β expects "LOBOC\n"
Need a hint? (β25% XP)
Limit the input to the REVERSE function to just the first 5 characters using WS-STR(1:5).
Show solution (0 XP)
>>SOURCE FORMAT IS FREEIDENTIFICATION DIVISION.PROGRAM-ID. REVERSE-BUG.DATA DIVISION.WORKING-STORAGE SECTION.01 WS-STR PIC X(10) VALUE "COBOL".PROCEDURE DIVISION. DISPLAY FUNCTION REVERSE(WS-STR(1:5)). STOP RUN.
Challenge 2 +15 XP
Use the NUMVAL function to safely convert the string WS-INPUT (which contains spaces and a sign) into a numeric format, and add 10 to it.
cobolβ solution verified at build time
COBOL has no in-browser runtime, so this one is pen-and-paper: work out what the
fixed code should be, then open the solution and compare. The expected output
below is real β it came from compiling the solution with GnuCOBOL when this page
was built.
Test 1 β expects "-0032\n"
Need a hint? (β25% XP)
Use COMPUTE WS-RESULT = FUNCTION NUMVAL(WS-INPUT) + 10.
Show solution (0 XP)
>>SOURCE FORMAT IS FREEIDENTIFICATION DIVISION.PROGRAM-ID. NUMVAL-TEST.DATA DIVISION.WORKING-STORAGE SECTION.01 WS-INPUT PIC X(10) VALUE " -42 ".01 WS-RESULT PIC S9(4).PROCEDURE DIVISION. COMPUTE WS-RESULT = FUNCTION NUMVAL(WS-INPUT) + 10. DISPLAY WS-RESULT. STOP RUN.