Numeric intrinsic functions
An intrinsic function is a value the compiler knows how to compute. It is written where an operand goes, its name is FUNCTION and a word, and it saves a paragraph for the small arithmetic that every program needs: the larger of two amounts, the absolute value of a difference, the remainder of a division.
MAX, MIN, ABS and MOD
MAX and MIN take any number of arguments, ABS takes one and drops its sign, and MOD takes two and returns the remainder:
IDENTIFICATION DIVISION.
PROGRAM-ID. FN1.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-A PIC S9(5) VALUE -1250.
01 WS-B PIC 9(5) VALUE 250.
01 WS-C PIC 9(5) VALUE 40.
01 WS-R PIC S9(9).
PROCEDURE DIVISION.
COMPUTE WS-R = FUNCTION MAX(WS-B, WS-C, 100)
DISPLAY "max " WS-R.
COMPUTE WS-R = FUNCTION MIN(WS-B, WS-C, 100)
DISPLAY "min " WS-R.
COMPUTE WS-R = FUNCTION ABS(WS-A)
DISPLAY "abs " WS-R.
COMPUTE WS-R = FUNCTION MOD(WS-B, WS-C)
DISPLAY "mod " WS-R.
COMPUTE WS-R = FUNCTION INTEGER(WS-B / WS-C)
DISPLAY "integer " WS-R.
COMPUTE WS-R = FUNCTION FACTORIAL(5)
DISPLAY "factorial " WS-R.
COMPUTE WS-R = FUNCTION SQRT(WS-B * WS-B)
DISPLAY "sqrt " WS-R.
STOP RUN.
cobc -x -free -Wall -o fn1 fn1.cob
./fn1
max +000000250
min +000000040
abs +000001250
mod +000000010
integer +000000006
factorial +000000120
sqrt +000000250
Each line went into a signed nine-digit field, which is why they all carry a sign and their leading zeros. The maximum of 250, 40 and 100 is 250, the minimum is 40, and the remainder of 250 divided by 40 is 10.
A function may stand inside a COMPUTE expression, so FUNCTION MAX(A, B) * 2 is legal. It may also be the whole right-hand side, as above, and it may be used in a DISPLAY or an IF comparison without any COMPUTE around it.
INTEGER, FACTORIAL and SQRT
INTEGER returns the integer part of its argument as a number, FACTORIAL multiplies every number from 1 up to its argument, and SQRT returns a square root. The three are the ones most often used in report arithmetic:
FUNCTION INTEGER(250 / 40) is 6 because the division inside the function is the integer division of the expression, and FUNCTION FACTORIAL(5) is 120. The square root of 250 squared is 250, which is the check that the function is exact for a perfect square in this field width.
Signs, REM and the argument list
MOD and REM differ in the sign of the answer when the arguments have different signs, and SIGN and INTEGER-PART work on a signed value. All of them in one program:
IDENTIFICATION DIVISION.
PROGRAM-ID. FN2.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-A PIC S9(5) VALUE -1250.
01 WS-B PIC 9(5) VALUE 250.
01 WS-C PIC 9(5) VALUE 40.
01 WS-R PIC S9(9).
PROCEDURE DIVISION.
COMPUTE WS-R = FUNCTION REM(WS-B, WS-C)
DISPLAY "rem " WS-R.
COMPUTE WS-R = FUNCTION SIGN(WS-A)
DISPLAY "sign " WS-R.
COMPUTE WS-R = FUNCTION INTEGER-PART(WS-A / 3)
DISPLAY "int-part " WS-R.
COMPUTE WS-R = FUNCTION MAX(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
DISPLAY "max10 " WS-R.
COMPUTE WS-R = FUNCTION MOD(-WS-B, WS-C)
DISPLAY "mod-neg " WS-R.
COMPUTE WS-R = FUNCTION FACTORIAL(0)
DISPLAY "fact0 " WS-R.
STOP RUN.
cobc -x -free -Wall -o fn2 fn2.cob
./fn2
rem +000000010
sign -000000001
int-part -000000416
max10 +000000010
mod-neg +000000030
fact0 +000000001
SIGN returned -1 for the negative field and would return +1 for a positive one and 0 for zero, which is the whole point of a function that answers a question about a sign. INTEGER-PART(-1250 / 3) truncates towards zero: -416.67 becomes -416, not -417.
MOD(-250, 40) returned 30 and not -10, because COBOL gives the result the sign of the divisor: the divisor is the positive 40, so the answer is positive. The negative argument changed the quotient and not the sign of the remainder.
A function with no answer
The square root of a negative number has no real answer, and this compiler does not raise an error or a status for it:
IDENTIFICATION DIVISION.
PROGRAM-ID. FN3.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-R PIC S9(9).
PROCEDURE DIVISION.
COMPUTE WS-R = FUNCTION SQRT(-4)
DISPLAY "sqrt " WS-R.
STOP RUN.
cobc -x -free -Wall -o fn3 fn3.cob
./fn3
sqrt +000000000
Zero, no message, exit status 0. A program that computes a square root of data must test the argument itself and answer with a code of its own, or the zero lands in the report as if the root of a negative number were nil. That is the shape of every function: it computes, and the program still owns the question of whether the answer makes sense.
Exercises
- Compute the largest of three amounts read into the program, using one FUNCTION MAX and no IF.
- Print the remainder of 1250 divided by 7 with both MOD and REM and compare the answers.
- Put FUNCTION SQRT(-4) inside a COMPUTE with a field of three digits and read the result.
- Compute FUNCTION FACTORIAL(12) into a PIC 9(9) field. What does the field hold, and why is that not 479001600?
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
How many arguments does FUNCTION MAX take?
Answer
As many as the program gives it, from one upwards
The page computed the maximum of three arguments and of ten.
-
What does FUNCTION ABS do?
Answer
It returns the value without its sign
The absolute value of -1250 was 1250.
-
Which sign does FUNCTION MOD give its result?
Answer
The sign of the divisor
MOD(-250, 40) returned 30, the sign of the positive divisor.
-
What does FUNCTION INTEGER(250 / 40) return?
Answer
6
The division is the integer division of the expression, and the page printed 6.
-
What does this program print?
IDENTIFICATION DIVISION. PROGRAM-ID. Q5. DATA DIVISION. WORKING-STORAGE SECTION. 01 W-A PIC 9(4) VALUE 300. 01 W-B PIC 9(4) VALUE 500. 01 W-R PIC 9(5). PROCEDURE DIVISION. COMPUTE W-R = FUNCTION MAX(W-A, W-B) DISPLAY W-R. STOP RUN.Answer
00500
The larger of the two amounts is the result.
-
What does this program print?
IDENTIFICATION DIVISION. PROGRAM-ID. Q6. DATA DIVISION. WORKING-STORAGE SECTION. 01 W-R PIC S9(5). PROCEDURE DIVISION. COMPUTE W-R = FUNCTION ABS(-42) DISPLAY W-R. STOP RUN.Answer
+00042
The field is five digits and signed, so the result shows the plus sign and the zeros.
-
What does this program print?
IDENTIFICATION DIVISION. PROGRAM-ID. Q7. DATA DIVISION. WORKING-STORAGE SECTION. 01 W-R PIC 9(5). 01 W-A PIC 9(3) VALUE 17. PROCEDURE DIVISION. COMPUTE W-R = FUNCTION MOD(W-A, 5) DISPLAY W-R. STOP RUN.Answer
00002
Seventeen divided by five leaves a remainder of two.
-
What does this program print?
IDENTIFICATION DIVISION. PROGRAM-ID. Q8. DATA DIVISION. WORKING-STORAGE SECTION. 01 W-R PIC 9(5). PROCEDURE DIVISION. COMPUTE W-R = FUNCTION FACTORIAL(4) DISPLAY W-R. STOP RUN.Answer
00024
Four factorial is four times three times two times one.
-
What does FUNCTION SQRT(-4) give in this compiler?
Answer
Zero, with no message and exit status 0
The run printed sqrt +000000000 and the shell saw 0.
-
Where may an intrinsic function be written?
Answer
Where an operand goes: in COMPUTE, DISPLAY, IF and the other statements
The lessons of this part use functions in COMPUTE and in DISPLAY alike.
The code on this page was built with cobc (GnuCOBOL) 3.2.0 and run on Alpine Linux 3.24.3 (x86_64) in a Docker container.
Article author: Arthur Isaev