Intrinsic functions arrived with an amendment to COBOL-85 in 1989 (42 of them) and grew with each standard.
A function call is an operand: FUNCTION name or FUNCTION name(arg, arg…), used anywhere a
value of its kind fits. The viewer colours the name after FUNCTION as a function.
Calling and declaring
ENVIRONMENT DIVISION.
CONFIGURATION SECTION.
REPOSITORY.
FUNCTION ALL INTRINSIC.
PROCEDURE DIVISION.
MOVE FUNCTION UPPER-CASE(NAME) TO OUT-NAME
COMPUTE R = FUNCTION SQRT(FUNCTION ABS(X)) + FUNCTION PI
DISPLAY FUNCTION CURRENT-DATE(1:8)
MOVE FUNCTION MAX(A, B, T(ALL)) TO TOP-VALUE
DISPLAY FUNCTION TRIM(S TRAILING) "|"
MOVE FUNCTION WHEN-COMPILED TO STAMP.
- A function with no arguments is written without parentheses (
FUNCTION PI,FUNCTION CURRENT-DATE); the parentheses may be empty. - Arguments may be items, literals, expressions or other functions.
ALLas a subscript passes every element of a table. - The result has a type of its own: numeric, integer, alphanumeric, national. It may be reference-modified:
FUNCTION CURRENT-DATE(1:4). - The
REPOSITORYparagraph (2002) lists names so thatFUNCTIONneed not be written:FUNCTION ALL INTRINSIC,FUNCTION TRIM INTRINSIC, orFUNCTION my-fn AS "MYFN"for a user-defined function. GnuCOBOL then acceptsTRIM(S)alone; IBM does not need or support that shorthand for all names. - User-defined functions are written in a
FUNCTION-IDprogram unit withPROCEDURE DIVISION USING … RETURNING r(Object-oriented COBOL for the unit shapes).
Mathematical
| Function | Result | Std |
|---|---|---|
ABS(n) | Absolute value | 89 |
ACOS(n) ASIN(n) ATAN(n) COS(n) SIN(n) TAN(n) | Trigonometric functions (radians) | 89 |
ANNUITY(rate, periods) | Ratio of an annuity payment to the loan | 89 |
E | Euler’s number | 89 |
EXP(n) EXP10(n) | e to the n; 10 to the n | 89; 2002 |
FACTORIAL(n) | n! for integers 0 to 28 | 89 |
FRACTION-PART(n) INTEGER-PART(n) INTEGER(n) | Fraction; integer part; greatest integer not above n | 89 |
LOG(n) LOG10(n) | Natural and common logarithm | 89 |
MOD(a, b) REM(a, b) | Modulus (sign of b); remainder (sign of a) | 89 |
PI | The constant pi | 89 |
PRESENT-VALUE(rate, v1, v2…) | Present value of a series of amounts | 89 |
RANDOM[(seed)] | Pseudo-random number in [0, 1) | 89 |
SIGN(n) | -1, 0 or 1 | 89 |
SQRT(n) | Square root | 89 |
HIGHEST-ALGEBRAIC(x) LOWEST-ALGEBRAIC(x) | Largest and smallest value the item can hold | 2002 |
Statistical and ordering
| Function | Result |
|---|---|
MAX(a, b…) MIN(a, b…) | Largest and smallest argument (numeric or alphanumeric) |
ORD-MAX(a, b…) ORD-MIN(a, b…) | Position (1-based) of the largest and smallest |
MEAN(…) MEDIAN(…) MIDRANGE(…) RANGE(…) | Arithmetic mean, median, (max + min) / 2, max - min |
STANDARD-DEVIATION(…) VARIANCE(…) SUM(…) | Population standard deviation, variance, total |
Strings and characters
| Function | Result | Std |
|---|---|---|
CHAR(n) ORD(c) | The character at ordinal n (1-based) of the collating sequence; the ordinal of a character | 89 |
CONCATENATE(a, b…) | The arguments joined | 2014; GnuCOBOL earlier |
LENGTH(x) BYTE-LENGTH(x) LENGTH-AN(x) STORED-CHAR-LENGTH(x) | Length in characters; in bytes; alphanumeric length; length without trailing spaces | 89; 2002 |
LOWER-CASE(s) UPPER-CASE(s) | Case conversion | 89 |
REVERSE(s) | Characters in reverse order | 89 |
TRIM(s [LEADING | TRAILING]) | Without leading and trailing spaces | 2002 |
SUBSTITUTE(s, a1, b1…) SUBSTITUTE-CASE | Replace substrings (case-sensitive and not) | 2014 |
NUMVAL(s) NUMVAL-C(s, sym) NUMVAL-F(s) | Number from text: plain, with currency and separators, floating | 89; 2002 |
TEST-NUMVAL(s) TEST-NUMVAL-C TEST-NUMVAL-F | 0 if the text is valid for that NUMVAL, else the position of the first bad character | 2002 |
BOOLEAN-OF-INTEGER(n, len) INTEGER-OF-BOOLEAN(b) | Boolean string from and to an integer | 2002 |
DISPLAY-OF(n) NATIONAL-OF(s) CHAR-NATIONAL(n) | National to alphanumeric and back; national character at ordinal n | 2002; IBM |
STANDARD-COMPARE(a, b, …) | Compare under a standard collating sequence | 2014 |
BASECONVERT, CONVERT and other 2023 additions | Not described here: I could not read the 2023 function list (see the Not described section of the overview) | 2023 |
Dates and times
| Function | Result | Std |
|---|---|---|
CURRENT-DATE | 21 characters: YYYYMMDDhhmmsshh and the offset from GMT (+hhmm) | 89 |
WHEN-COMPILED | Compile time in the same layout | 89 |
DATE-OF-INTEGER(n) INTEGER-OF-DATE(d) | Between the integer day number (1 = 1 Jan 1601) and YYYYMMDD | 89 |
DAY-OF-INTEGER(n) INTEGER-OF-DAY(d) | Same for YYYYDDD | 89 |
DATE-TO-YYYYMMDD(d, win, cur) DAY-TO-YYYYDDD(d, win, cur) YEAR-TO-YYYY(y, win, cur) | Windows a two-digit year | 89 Amend. |
SECONDS-PAST-MIDNIGHT | Seconds since midnight | 2002 |
FORMATTED-CURRENT-DATE(fmt) FORMATTED-DATE(fmt, n) FORMATTED-TIME(fmt, s) FORMATTED-DATETIME(fmt, n, s) | Date or time in an ISO 8601 style format string | 2002 |
INTEGER-OF-FORMATTED-DATE(fmt, s) SECONDS-FROM-FORMATTED-TIME(fmt, s) COMBINED-DATETIME(n, s) | Parse back to numbers; combine | 2002 |
TEST-DATE-YYYYMMDD(d) TEST-DAY-YYYYDDD(d) TEST-FORMATTED-DATETIME(fmt, s) | 0 when valid, else the position of the error | 2002 |
DATEVAL(s, pattern) UNDATE(n, pattern) YEARWINDOW | IBM Enterprise COBOL date helpers (a date to an integer for comparison, back, and the century window) | IBM |
Locale, exceptions, modules
| Function | Result |
|---|---|
CURRENCY-SYMBOL MONETARY-DECIMAL-POINT MONETARY-THOUSANDS-SEPARATOR NUMERIC-DECIMAL-POINT NUMERIC-THOUSANDS-SEPARATOR | Locale symbols (2002) |
LOCALE-COMPARE(a, b) LOCALE-DATE(d) LOCALE-TIME(t) LOCALE-TIME-FROM-SECONDS(n) | Locale-aware comparison and formatting (2002) |
EXCEPTION-FILE EXCEPTION-FILE-N EXCEPTION-LOCATION EXCEPTION-LOCATION-N EXCEPTION-STATEMENT EXCEPTION-STATUS | Describe the last exception (2002) |
MODULE-ID MODULE-PATH MODULE-SOURCE MODULE-CALLER-ID MODULE-DATE MODULE-FORMATTED-DATE MODULE-TIME | About the running program (GnuCOBOL) |
CONTENT-OF(x) CONTENT-LENGTH(x) | Data and length of a pointer target (GnuCOBOL) |
Non-standard routines called with CALL "name" are not functions. GnuCOBOL (CBL_*, C$*, SYSTEM) and Micro Focus
have libraries of them for files, directories, screens and arguments; IBM has callable services (CEE*, ILBO*).