Chapter 19 Quiz: Intrinsic Functions Reference
Test your understanding of COBOL intrinsic functions. Each question is followed by a hidden answer -- try to answer before revealing it.
Question 1
What keyword must precede every intrinsic function call in COBOL?
- a) CALL
- b) INVOKE
- c) FUNCTION
- d) COMPUTE
Show Answer
**c) FUNCTION**. Every intrinsic function call in COBOL must be preceded by the FUNCTION keyword. For example: `FUNCTION SQRT(25)`, `FUNCTION UPPER-CASE(WS-NAME)`, `FUNCTION CURRENT-DATE`. This is a syntactic requirement of the COBOL language -- unlike most other programming languages, COBOL does not allow bare function names. The FUNCTION keyword tells the compiler that what follows is an intrinsic function, not a data name.Question 2
How are arguments separated in a COBOL intrinsic function call?
- a) By commas
- b) By semicolons
- c) By spaces
- d) By the AND keyword
Show Answer
**c) By spaces**. COBOL intrinsic function arguments are separated by spaces, not commas. For example: `FUNCTION MAX(WS-A WS-B WS-C)` is correct, while `FUNCTION MAX(WS-A, WS-B, WS-C)` is not standard (though some compilers may accept commas). This is a common source of confusion for programmers coming from languages like Java, Python, or C where commas separate function arguments.Question 3
What does FUNCTION CURRENT-DATE return?
- a) A 6-character date string (YYMMDD)
- b) An 8-character date string (YYYYMMDD)
- c) A 21-character string containing date, time, and GMT offset
- d) A numeric value representing seconds since midnight
Show Answer
**c) A 21-character string containing date, time, and GMT offset**. The format is: YYYYMMDDHHMMSSSS+HHMM, where: - Positions 1-8: Date (YYYYMMDD) - Positions 9-14: Time (HHMMSS) - Positions 15-16: Hundredths of second - Position 17: GMT offset sign ('+', '-', or '0') - Positions 18-21: GMT offset (HHMM) To extract just the date, use reference modification: `FUNCTION CURRENT-DATE(1:8)`.Question 4
Which function converts a YYYYMMDD date to an integer day number?
- a) FUNCTION DATE-TO-INT
- b) FUNCTION INTEGER-OF-DATE
- c) FUNCTION DAY-OF-DATE
- d) FUNCTION CONVERT-DATE
Show Answer
**b) FUNCTION INTEGER-OF-DATE**. This function takes an 8-digit integer in YYYYMMDD format and returns an integer representing the number of days since a base date (the epoch). The inverse function is FUNCTION DATE-OF-INTEGER, which converts a day number back to YYYYMMDD format. These two functions are the foundation for all COBOL date arithmetic -- to calculate the difference between two dates, convert both to integers and subtract.Question 5
What does FUNCTION ANNUITY(rate, periods) return?
- a) The total interest paid over the life of a loan
- b) The periodic payment amount needed to amortize $1 at the given rate over the given periods
- c) The future value of a series of payments
- d) The present value of a single future payment
Show Answer
**b) The periodic payment amount needed to amortize $1 at the given rate over the given periods**. To calculate the actual payment on a loan, multiply the loan amount by the annuity factor:COMPUTE WS-PAYMENT = WS-LOAN-AMOUNT *
FUNCTION ANNUITY(WS-MONTHLY-RATE WS-NUM-PAYMENTS)
For example, to find the monthly payment on a $200,000 loan at 6% annual (0.5% monthly) for 360 months: `200000 * FUNCTION ANNUITY(0.005 360)` = approximately $1,199.10.
Question 6
Which pair of functions converts between uppercase and lowercase?
- a) FUNCTION TO-UPPER and FUNCTION TO-LOWER
- b) FUNCTION UPPER-CASE and FUNCTION LOWER-CASE
- c) FUNCTION CAPITALIZE and FUNCTION DECAPITALIZE
- d) FUNCTION UPSHIFT and FUNCTION DOWNSHIFT
Show Answer
**b) FUNCTION UPPER-CASE and FUNCTION LOWER-CASE**. FUNCTION UPPER-CASE converts all lowercase letters in the argument to uppercase. FUNCTION LOWER-CASE converts all uppercase letters to lowercase. Non-alphabetic characters are not affected. These functions return alphanumeric values of the same length as the input. They are among the most frequently used intrinsic functions in business applications for data normalization and comparison.Question 7
What does FUNCTION LENGTH return?
- a) The number of non-space characters in a string
- b) The defined (allocated) length of a data item in characters
- c) The number of bytes used by a data item
- d) The number of characters up to the first space
Show Answer
**b) The defined (allocated) length of a data item in characters**. FUNCTION LENGTH returns the size of the data item as defined by its PIC clause, regardless of its current content. For example, if WS-NAME is PIC X(30) and contains "JOHN" followed by 26 spaces, FUNCTION LENGTH(WS-NAME) returns 30, not 4. To get the length of the actual content (without trailing spaces), combine with TRIM: `FUNCTION LENGTH(FUNCTION TRIM(WS-NAME))`.Question 8
What does FUNCTION TRIM do?
- a) Removes all spaces from a string
- b) Removes leading and trailing spaces from a string
- c) Truncates a string to a specified length
- d) Removes special characters from a string
Show Answer
**b) Removes leading and trailing spaces from a string**. FUNCTION TRIM removes whitespace from both ends of the argument. For example: `FUNCTION TRIM(" HELLO ")` returns "HELLO". TRIM can also be directed to remove only leading spaces (TRIM with LEADING) or only trailing spaces (TRIM with TRAILING). Introduced in COBOL 2014, TRIM is supported by modern compilers including IBM Enterprise COBOL V6+ and GnuCOBOL 3+.Question 9
Which function returns a pseudo-random number between 0 and 1?
- a) FUNCTION RAND
- b) FUNCTION RANDOM
- c) FUNCTION RNG
- d) FUNCTION GENERATE-RANDOM
Show Answer
**b) FUNCTION RANDOM**. When called with an argument -- `FUNCTION RANDOM(seed)` -- it initializes the random number generator with the given seed and returns the first pseudo-random number. When called without an argument -- `FUNCTION RANDOM` -- it returns the next number in the pseudo-random sequence. The returned value is always between 0 (inclusive) and 1 (exclusive). To generate a random integer in a range, multiply and truncate: `FUNCTION INTEGER(FUNCTION RANDOM * range-size) + lower-bound`. FUNCTION RANDOM is NOT cryptographically secure.Question 10
What is the result of FUNCTION MOD(17 5)?
- a) 3.4
- b) 2
- c) 3
- d) 12
Show Answer
**b) 2**. FUNCTION MOD returns the remainder after integer division. 17 divided by 5 is 3 with a remainder of 2 (because 5 * 3 = 15, and 17 - 15 = 2). The MOD function is useful for determining if a number is even/odd (MOD n 2), for cycling through table indices, for hash calculations, and for determining day-of-week from date calculations.Question 11
What is the difference between FUNCTION INTEGER and FUNCTION INTEGER-PART?
- a) They are identical
- b) INTEGER rounds to the nearest integer; INTEGER-PART truncates toward zero
- c) INTEGER returns the greatest integer not greater than the argument (floor); INTEGER-PART truncates toward zero
- d) INTEGER truncates toward zero; INTEGER-PART rounds up
Show Answer
**c) INTEGER returns the greatest integer not greater than the argument (floor); INTEGER-PART truncates toward zero**. For positive numbers, they produce the same result. The difference appears with negative numbers: - FUNCTION INTEGER(-3.7) = -4 (floor: the greatest integer <= -3.7) - FUNCTION INTEGER-PART(-3.7) = -3 (truncate: remove the decimal part) This distinction matters in financial calculations where rounding direction affects monetary amounts.Question 12
Which function would you use to determine if a date string represents a valid date?
- a) FUNCTION VALIDATE-DATE
- b) FUNCTION TEST-DATE-YYYYMMDD
- c) FUNCTION INTEGER-OF-DATE (check for zero/error return)
- d) Both b and c can be used
Show Answer
**d) Both b and c can be used**. FUNCTION TEST-DATE-YYYYMMDD (COBOL 2002) returns 0 if the argument is a valid date and a non-zero value if invalid. FUNCTION INTEGER-OF-DATE can also serve as a validator -- if given an invalid date, it returns 0 or causes an exception (implementation-dependent). TEST-DATE-YYYYMMDD is the more explicit and portable choice. For Julian dates, use TEST-DAY-YYYYDDD. Not all compilers support TEST-DATE-YYYYMMDD; check your compiler's documentation.Question 13
True or False: Intrinsic functions can modify their arguments (have side effects).
Show Answer
**False**. COBOL intrinsic functions are pure functions -- they return a value without modifying their arguments or any external state. The one exception is FUNCTION RANDOM, which maintains internal state (the seed for the pseudo-random sequence), but even RANDOM does not modify any data items in the program. This purity makes intrinsic functions safe to use in any context without worrying about unintended data modifications.Question 14
True or False: Intrinsic functions can be used directly in an IF condition.
Show Answer
**True**. Intrinsic functions can appear anywhere a value of the appropriate type is expected, including in IF conditions:IF FUNCTION LENGTH(FUNCTION TRIM(WS-INPUT)) > 10
DISPLAY "Input exceeds 10 characters"
END-IF
You can compare function results, use them in compound conditions, and nest them. The function is evaluated when the IF statement executes, and its return value is used in the comparison.
Question 15
True or False: FUNCTION WHEN-COMPILED returns the same value every time the program runs.
Show Answer
**True**. FUNCTION WHEN-COMPILED returns the date and time when the program was compiled, not when it is running. This value is fixed at compile time and embedded in the program's object code. It will be the same every time the program executes, regardless of the current date and time. It only changes when the program is recompiled. This makes it useful for version tracking and audit trails ("this output was produced by a program compiled on...").Question 16
True or False: FUNCTION NUMVAL converts a numeric-edited string like "$1,234.56" to a numeric value.
Show Answer
**False**. FUNCTION NUMVAL handles basic numeric strings with optional signs and decimal points (e.g., "-1234.56", "+1234.56", "1234.56"). For strings with currency symbols, commas, and other editing characters, you need **FUNCTION NUMVAL-C**, which accepts a second argument specifying the currency symbol to strip:COMPUTE WS-AMOUNT = FUNCTION NUMVAL-C("$1,234.56" "$")
NUMVAL would fail or produce incorrect results on "$1,234.56" because it does not know how to handle the dollar sign and commas.
Question 17
True or False: Intrinsic functions can be nested inside other intrinsic functions.
Show Answer
**True**. Intrinsic functions can be nested to any depth. The inner function is evaluated first, and its result becomes the argument to the outer function:COMPUTE WS-RESULT =
FUNCTION SQRT(FUNCTION ABS(WS-VALUE))
MOVE FUNCTION UPPER-CASE(
FUNCTION TRIM(WS-INPUT))
TO WS-OUTPUT
COMPUTE WS-FUTURE =
FUNCTION DATE-OF-INTEGER(
FUNCTION INTEGER-OF-DATE(WS-TODAY) + 30)
This eliminates the need for intermediate working-storage variables and makes code more concise, though deeply nested functions can be hard to read.
Question 18
True or False: FUNCTION REVERSE can be used to reverse the digits of a numeric field.
Show Answer
**True**, but with a caveat. FUNCTION REVERSE operates on alphanumeric strings. To reverse the digits of a numeric field, you must first move it to an alphanumeric field, apply REVERSE, and then convert back:MOVE WS-NUMERIC TO WS-ALPHA-FIELD
MOVE FUNCTION REVERSE(WS-ALPHA-FIELD)
TO WS-REVERSED-ALPHA
If WS-NUMERIC is PIC 9(5) with value 12345, moving to PIC X(5) gives "12345", and REVERSE gives "54321". Note that leading zeros in the numeric field become trailing zeros in the reversed string, which may need handling.
Question 19
What is the output of the following code?
01 WS-DATE PIC 9(8) VALUE 20260101.
01 WS-DAYS PIC 9(8).
01 WS-NEW-DATE PIC 9(8).
PROCEDURE DIVISION.
COMPUTE WS-DAYS =
FUNCTION INTEGER-OF-DATE(WS-DATE)
ADD 59 TO WS-DAYS
COMPUTE WS-NEW-DATE =
FUNCTION DATE-OF-INTEGER(WS-DAYS)
DISPLAY WS-NEW-DATE
STOP RUN.
Show Answer
The output is:20260301
January 1, 2026 plus 59 days = March 1, 2026. The calculation:
- January has 31 days (days 1-31 from Jan 1)
- February 2026 has 28 days (2026 is not a leap year: not divisible by 4... actually, 2026 / 4 = 506.5, so 2026 is NOT a leap year)
- 31 (rest of January) + 28 (February) = 59 days after January 1 = March 1
This demonstrates the power of INTEGER-OF-DATE / DATE-OF-INTEGER for date arithmetic -- the functions automatically handle month lengths, leap years, and year boundaries. No manual calculation is needed.
Question 20
What is the output of the following code?
01 WS-NAME PIC X(20) VALUE " john q. smith ".
01 WS-RESULT PIC X(20).
PROCEDURE DIVISION.
MOVE FUNCTION UPPER-CASE(
FUNCTION TRIM(WS-NAME))
TO WS-RESULT
DISPLAY ">" WS-RESULT "<"
DISPLAY FUNCTION LENGTH(
FUNCTION TRIM(WS-RESULT))
STOP RUN.
Show Answer
The output is:>JOHN Q. SMITH <
13
Here is the evaluation:
1. `FUNCTION TRIM(WS-NAME)` removes leading and trailing spaces: "john q. smith" (13 characters)
2. `FUNCTION UPPER-CASE(...)` converts to uppercase: "JOHN Q. SMITH" (13 characters)
3. MOVE to WS-RESULT (PIC X(20)): "JOHN Q. SMITH" is left-justified and padded with spaces to 20 characters, giving "JOHN Q. SMITH "
4. The first DISPLAY shows the content between > and < markers, revealing the 7 trailing spaces
5. `FUNCTION TRIM(WS-RESULT)` removes trailing spaces: "JOHN Q. SMITH" (13 characters)
6. `FUNCTION LENGTH(...)` returns 13
Question 21
What is wrong with the following code, and what will happen when it executes?
01 WS-AMOUNT PIC X(15) VALUE "$1,234.56".
01 WS-NUMERIC PIC S9(7)V99.
PROCEDURE DIVISION.
COMPUTE WS-NUMERIC =
FUNCTION NUMVAL(WS-AMOUNT)
DISPLAY WS-NUMERIC
STOP RUN.
Show Answer
The code uses **FUNCTION NUMVAL** to convert a string containing a dollar sign and commas. NUMVAL does not handle currency symbols or thousands separators -- it only handles plain numeric strings with optional signs and decimal points. When this code executes, **NUMVAL will raise a runtime error** (or produce garbage results) because it cannot parse "$1,234.56". The fix is to use **FUNCTION NUMVAL-C** (the currency-aware version):COMPUTE WS-NUMERIC =
FUNCTION NUMVAL-C(WS-AMOUNT "$")
NUMVAL-C accepts a second argument specifying the currency symbol to strip. It also handles commas as thousands separators.
Question 22
What is the output of the following code?
01 WS-VAL-A PIC S9(5)V99 VALUE -7.50.
01 WS-VAL-B PIC S9(5)V99 VALUE 3.25.
01 WS-VAL-C PIC S9(5)V99 VALUE 12.80.
PROCEDURE DIVISION.
DISPLAY FUNCTION MAX(WS-VAL-A WS-VAL-B WS-VAL-C)
DISPLAY FUNCTION MIN(WS-VAL-A WS-VAL-B WS-VAL-C)
DISPLAY FUNCTION MEAN(WS-VAL-A WS-VAL-B WS-VAL-C)
DISPLAY FUNCTION MEDIAN(WS-VAL-A WS-VAL-B WS-VAL-C)
DISPLAY FUNCTION ABS(WS-VAL-A)
STOP RUN.
Show Answer
The output is:12.80 (MAX of -7.50, 3.25, 12.80)
-7.50 (MIN of -7.50, 3.25, 12.80)
2.85 (MEAN: (-7.50 + 3.25 + 12.80) / 3 = 8.55 / 3)
3.25 (MEDIAN: middle value when sorted: -7.50, 3.25, 12.80)
7.50 (ABS of -7.50)
Note: The exact display format depends on the compiler's default formatting for numeric values in DISPLAY statements. The MEAN calculation: (-7.50 + 3.25 + 12.80) = 8.55, divided by 3 = 2.85. The MEDIAN is the middle value of the sorted set (3.25), not the average.
Question 23
A programmer wants to convert a 2-digit year to a 4-digit year using intrinsic functions. Which function is designed for this purpose, and what "windowing" technique does it use?
COMPUTE WS-YYYY = FUNCTION YEAR-TO-YYYY(WS-YY ???)
Show Answer
The function is **FUNCTION DATE-OF-INTEGER-PART** -- wait, the correct function is **FUNCTION YEAR-TO-YYYY** (available in some implementations) or the more standard approach using a **windowing** technique with basic arithmetic. The standard COBOL 2002 approach:COMPUTE WS-YYYY = FUNCTION DATE-OF-INTEGER(
FUNCTION INTEGER-OF-DATE(
FUNCTION YEAR-TO-YYYY(WS-YY 1940) * 10000
+ WS-MM * 100 + WS-DD))
But the simpler and more portable approach is the **sliding window**:
IF WS-YY >= 40
COMPUTE WS-YYYY = 1900 + WS-YY
ELSE
COMPUTE WS-YYYY = 2000 + WS-YY
END-IF
This uses a pivot year (40 in this example): years 40-99 are interpreted as 1940-1999, and years 00-39 are interpreted as 2000-2039. The pivot year should be chosen based on the business context (e.g., the oldest possible date in the data).
Some compilers offer `FUNCTION YEAR-TO-YYYY(yy, window-start)` which automates the windowing.
Question 24
What is the output of the following code that uses FUNCTION ORD and FUNCTION CHAR? Assume an ASCII platform.
PROCEDURE DIVISION.
DISPLAY FUNCTION ORD("A")
DISPLAY FUNCTION ORD("Z")
DISPLAY FUNCTION ORD("0")
DISPLAY FUNCTION CHAR(65)
DISPLAY FUNCTION CHAR(90)
DISPLAY FUNCTION CHAR(48)
STOP RUN.
Show Answer
On an ASCII platform:66 (ORD of "A" = 66; note: ORD returns position in collating
sequence starting from 1, so ASCII value 65 + 1 = 66)
91 (ORD of "Z" = ASCII 90 + 1 = 91)
49 (ORD of "0" = ASCII 48 + 1 = 49)
A (CHAR(65) = character at position 65 = ASCII 64 = "@"
... actually, CHAR uses 1-based: CHAR(66) = "A")
Z (CHAR(90) = character at position 90 = ASCII 89 = "Y"
... actually CHAR(90) on 1-based = ASCII 89)
0 (CHAR(48) = character at position 48 = ASCII 47 = "/")
**Important:** ORD and CHAR use 1-based positions, not 0-based. ORD("A") on ASCII returns 66 (ASCII code 65 + 1). CHAR(66) returns "A". This is a common source of confusion. On EBCDIC systems, the ordinal values are completely different (ORD("A") = 194, for example). These functions are platform-dependent and must be used carefully in portable code.
Question 25
What happens when FUNCTION INTEGER-OF-DATE is called with an invalid date like 20261301 (month 13)?
Show Answer
The behavior is **implementation-dependent** and potentially dangerous: - **IBM Enterprise COBOL:** Returns 0 or raises a runtime exception, depending on compiler options and the severity of the error. With DATEPROC(FLAG) compiler option, invalid dates may be flagged. - **GnuCOBOL:** May return 0 or an unpredictable value. - **COBOL Standard:** The result is undefined for invalid dates. Best practice: **Always validate dates before passing them to INTEGER-OF-DATE.** Use FUNCTION TEST-DATE-YYYYMMDD (if available) or manual validation:IF WS-MONTH < 01 OR WS-MONTH > 12
DISPLAY "INVALID MONTH"
ELSE
COMPUTE WS-INT-DATE =
FUNCTION INTEGER-OF-DATE(WS-DATE)
END-IF
Never rely on INTEGER-OF-DATE to validate dates -- validate first, then convert.
Question 26
A bank needs to calculate the number of days between a loan origination date and today. Write the most concise COBOL statement to accomplish this, assuming WS-ORIG-DATE contains the origination date in YYYYMMDD format and WS-TODAY will be set from CURRENT-DATE.
Show Answer
COMPUTE WS-DAYS-OUTSTANDING =
FUNCTION INTEGER-OF-DATE(
FUNCTION CURRENT-DATE(1:8))
- FUNCTION INTEGER-OF-DATE(WS-ORIG-DATE)
This single COMPUTE statement:
1. Gets today's date from CURRENT-DATE (positions 1-8 give YYYYMMDD)
2. Converts today to an integer day number
3. Converts the origination date to an integer day number
4. Subtracts to get the difference in days
No intermediate variables are needed. The nested FUNCTION CURRENT-DATE(1:8) call uses reference modification to extract just the date portion from the 21-character CURRENT-DATE string. If the result is negative, the origination date is in the future (likely an error).
Question 27
Explain the difference between FUNCTION PRESENT-VALUE and calculating present value manually. Given three future cash flows of $10,000, $15,000, and $20,000, and an annual discount rate of 5%, show both approaches.
Show Answer
**FUNCTION PRESENT-VALUE approach:**COMPUTE WS-PV =
FUNCTION PRESENT-VALUE(
0.05 10000 15000 20000)
PRESENT-VALUE takes a rate and a series of future amounts. It calculates: amount1/(1+rate)^1 + amount2/(1+rate)^2 + amount3/(1+rate)^3.
**Manual calculation approach:**
COMPUTE WS-PV =
10000 / (1.05 ** 1) +
15000 / (1.05 ** 2) +
20000 / (1.05 ** 3)
Both produce the same result:
- 10000 / 1.05 = 9,523.81
- 15000 / 1.1025 = 13,605.44
- 20000 / 1.157625 = 17,276.75
- Total PV = 40,406.00 (approximately)
The FUNCTION approach is more concise and handles the exponentiation and summation internally. It is also less error-prone because you do not need to calculate the discount factors manually. However, the manual approach is more transparent and easier to audit.
Note: PRESENT-VALUE assumes the first payment is 1 period in the future, not immediate. For different timing assumptions, adjustments may be needed.
Question 28
What is the result of the following code?
01 WS-TEXT PIC X(20) VALUE "HELLO WORLD".
PROCEDURE DIVISION.
DISPLAY FUNCTION LENGTH(WS-TEXT)
DISPLAY FUNCTION LENGTH(
FUNCTION TRIM(WS-TEXT))
DISPLAY FUNCTION LENGTH("HELLO")
STOP RUN.
Show Answer
The output is:20
11
5
- `FUNCTION LENGTH(WS-TEXT)` returns **20** -- the defined length of the PIC X(20) field, regardless of its content. The field contains "HELLO WORLD" followed by 9 spaces.
- `FUNCTION LENGTH(FUNCTION TRIM(WS-TEXT))` returns **11** -- TRIM removes the 9 trailing spaces, leaving "HELLO WORLD" (11 characters), and LENGTH returns the length of that trimmed result.
- `FUNCTION LENGTH("HELLO")` returns **5** -- the length of the string literal "HELLO".
This illustrates the critical distinction: LENGTH returns the defined size of a data item, not the length of its meaningful content. To get the content length, combine LENGTH with TRIM.
Question 29
Which function would you use to determine if a string can be safely converted to a number using NUMVAL-C?
- a) FUNCTION VALIDATE-NUMERIC
- b) FUNCTION IS-NUMERIC
- c) FUNCTION TEST-NUMVAL-C
- d) The IS NUMERIC class condition
Show Answer
**c) FUNCTION TEST-NUMVAL-C** (COBOL 2014). This function tests whether a string can be successfully converted by NUMVAL-C without causing an error. It returns 0 if the conversion would succeed, or a positive integer indicating the position of the first invalid character.IF FUNCTION TEST-NUMVAL-C(WS-AMOUNT "$") = 0
COMPUTE WS-NUMERIC =
FUNCTION NUMVAL-C(WS-AMOUNT "$")
ELSE
DISPLAY "INVALID AMOUNT: " WS-AMOUNT
END-IF
**d) IS NUMERIC** is not the right choice because IS NUMERIC only checks for unformatted numeric characters (digits, sign, decimal point). It would reject "$1,234.56" even though NUMVAL-C can handle it. TEST-NUMVAL-C checks specifically for NUMVAL-C compatibility, including currency symbols and commas.
Note: TEST-NUMVAL-C may not be available in all compilers. If unavailable, validate the string manually before calling NUMVAL-C.
Question 30
A bank's COBOL program needs to generate a unique 16-character reference number for each transaction, formatted as YYYYMMDD-RRRRRRRR (date dash 8 random digits). Write the code using intrinsic functions.
Show Answer
WORKING-STORAGE SECTION.
01 WS-REF-NUM PIC X(17).
01 WS-DATE-PART PIC X(8).
01 WS-RAND-INT PIC 9(8).
01 WS-RAND-DISPLAY PIC X(8).
01 WS-FIRST-CALL PIC X VALUE 'Y'.
88 FIRST-CALL VALUE 'Y'.
88 NOT-FIRST-CALL VALUE 'N'.
PROCEDURE DIVISION.
* Seed random generator on first call
IF FIRST-CALL
COMPUTE WS-RAND-INT =
FUNCTION RANDOM(
FUNCTION INTEGER-OF-DATE(
FUNCTION CURRENT-DATE(1:8)))
SET NOT-FIRST-CALL TO TRUE
END-IF
* Get today's date
MOVE FUNCTION CURRENT-DATE(1:8)
TO WS-DATE-PART
* Generate 8-digit random number
COMPUTE WS-RAND-INT =
FUNCTION INTEGER(
FUNCTION RANDOM * 90000000)
+ 10000000
MOVE WS-RAND-INT TO WS-RAND-DISPLAY
* Build reference number
STRING WS-DATE-PART DELIMITED BY SIZE
"-" DELIMITED BY SIZE
WS-RAND-DISPLAY DELIMITED BY SIZE
INTO WS-REF-NUM
END-STRING
DISPLAY "REF: " WS-REF-NUM
* Example output: REF: 20260210-47382916
**Caveat:** FUNCTION RANDOM is pseudo-random and NOT suitable for generating cryptographically secure or guaranteed-unique identifiers. In a production system, you would use a database sequence, a UUID generator, or a combination of timestamp + counter to ensure uniqueness. The RANDOM approach can produce duplicates, especially at high transaction volumes.