ACCEPT FROM ARGUMENT-VALUE
A COBOL program has no parameter list of its own: the words after its name arrive in the runtime, and three statements read them. This lesson shows the three, then a small calculator that uses them, validates them and reports its own failure through the exit status.
ARGUMENT-NUMBER and ARGUMENT-VALUE
ACCEPT ... FROM ARGUMENT-NUMBER puts the number of arguments after the program name into a field, and each ACCEPT ... FROM ARGUMENT-VALUE takes the next argument, as a string of the length of the receiving field. There is no subscript and no array: the runtime keeps the position and moves it on.
IDENTIFICATION DIVISION.
PROGRAM-ID. ARGDUMP.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-COUNT PIC 9(3).
01 WS-A PIC X(20).
01 WS-B PIC X(20).
01 WS-C PIC X(20).
01 WS-LINE PIC X(80).
PROCEDURE DIVISION.
ACCEPT WS-COUNT FROM ARGUMENT-NUMBER
DISPLAY "arguments " WS-COUNT.
ACCEPT WS-A FROM ARGUMENT-VALUE
ACCEPT WS-B FROM ARGUMENT-VALUE
ACCEPT WS-C FROM ARGUMENT-VALUE
DISPLAY "1 [" FUNCTION TRIM(WS-A) "]"
DISPLAY "2 [" FUNCTION TRIM(WS-B) "]"
DISPLAY "3 [" FUNCTION TRIM(WS-C) "]"
ACCEPT WS-LINE FROM COMMAND-LINE
DISPLAY "line [" FUNCTION TRIM(WS-LINE) "]"
STOP RUN.
Three runs, with no argument, with three and with one:
cobc -x -free -Wall -o args args.cob
./args
./args alpha 42 "two words"
./args only
arguments 000
1 []
2 []
3 []
line []
arguments 003
1 [alpha]
2 [42]
3 [two words]
line [alpha 42 two words]
arguments 001
1 [only]
2 []
3 []
line [only]
Two things in that output are the language and not an accident. The count is a numeric field printed as three digits, because PIC 9(3) prints its leading zeros. And an ACCEPT FROM ARGUMENT-VALUE past the last argument leaves the field as it was: the field starts as spaces, so the bracket is empty, and no error is raised. A program that reads five arguments and is given two gets three empty strings and has to notice on its own.
The whole command line
ACCEPT ... FROM COMMAND-LINE gives the arguments as one string, in the order they were typed. The program name itself is not part of it, and the words keep their spacing:
line [alpha 42 two words]
That is the form to log in a run header, because it shows how the job was started, quotes and all. It is the wrong form to parse: a value with a space in it is indistinguishable from two arguments there, while ARGUMENT-VALUE already has the split the shell made. The rule is the one every language has: read arguments one by one, and read the line only to record it.
A calculator that takes arguments
The program takes an operation and two numbers and prints the result. Every argument is checked before it is used: the count first, then the operation, then both numbers through FUNCTION TEST-NUMVAL, which answers zero for a string that is a number and a non-zero value for one that is not.
IDENTIFICATION DIVISION.
PROGRAM-ID. CALC.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-COUNT PIC 9(3).
01 WS-OP PIC X(10).
01 WS-LEFT PIC X(20).
01 WS-RIGHT PIC X(20).
01 WS-L PIC S9(9)V99 COMP-3.
01 WS-R PIC S9(9)V99 COMP-3.
01 WS-RESULT PIC S9(11)V99 COMP-3.
01 WS-OUT PIC -(11)9.99.
PROCEDURE DIVISION.
ACCEPT WS-COUNT FROM ARGUMENT-NUMBER
IF WS-COUNT NOT = 3
DISPLAY "usage: calc OP NUMBER NUMBER" UPON STDERR
MOVE 2 TO RETURN-CODE
STOP RUN
END-IF.
ACCEPT WS-OP FROM ARGUMENT-VALUE
ACCEPT WS-LEFT FROM ARGUMENT-VALUE
ACCEPT WS-RIGHT FROM ARGUMENT-VALUE.
IF FUNCTION TEST-NUMVAL(WS-LEFT) NOT = 0
DISPLAY "bad number: " FUNCTION TRIM(WS-LEFT) UPON STDERR
MOVE 4 TO RETURN-CODE
STOP RUN
END-IF
IF FUNCTION TEST-NUMVAL(WS-RIGHT) NOT = 0
DISPLAY "bad number: " FUNCTION TRIM(WS-RIGHT) UPON STDERR
MOVE 4 TO RETURN-CODE
STOP RUN
END-IF.
COMPUTE WS-L = FUNCTION NUMVAL(WS-LEFT)
COMPUTE WS-R = FUNCTION NUMVAL(WS-RIGHT).
EVALUATE WS-OP
WHEN "add"
COMPUTE WS-RESULT = WS-L + WS-R
WHEN "sub"
COMPUTE WS-RESULT = WS-L - WS-R
WHEN "mul"
COMPUTE WS-RESULT = WS-L * WS-R
WHEN "div"
IF WS-R = 0
DISPLAY "cannot divide by zero" UPON STDERR
MOVE 3 TO RETURN-CODE
STOP RUN
END-IF
COMPUTE WS-RESULT ROUNDED = WS-L / WS-R
WHEN OTHER
DISPLAY "unknown op: " FUNCTION TRIM(WS-OP) UPON STDERR
MOVE 2 TO RETURN-CODE
STOP RUN
END-EVALUATE.
MOVE WS-RESULT TO WS-OUT
DISPLAY FUNCTION TRIM(WS-LEFT) " " FUNCTION TRIM(WS-OP) " "
FUNCTION TRIM(WS-RIGHT) " = " FUNCTION TRIM(WS-OUT).
STOP RUN.
The receiving fields for the numbers are COMP-3 because the calculator is about money, which the lesson on decimal money explains. WS-OUT is an edited field, so the result prints with a sign position, no leading zeros and two decimal places.
cobc -x -free -Wall -o calc calc.cob
./calc add 1.25 2.50
echo $?
./calc div 10 3
echo $?
./calc mul 2 3
echo $?
1.25 add 2.50 = 3.75
0
10 div 3 = 3.33
0
2 mul 3 = 6.00
0
Usage, a bad number and an exit status
A tool has four kinds of wrong input here, and each gets its own message and its own status. The messages go to standard error, so they do not end up in a result file that another step reads.
./calc pow 2 3
echo $?
./calc add 1
echo $?
./calc add x 1
echo $?
./calc div 1 0
echo $?
unknown op: pow
2
usage: calc OP NUMBER NUMBER
2
bad number: x
4
cannot divide by zero
3
The statuses are a small contract with the shell: 2 for a wrong command line, 3 for a division by zero, 4 for a value that is not a number, and 0 for success. A batch step that reads RETURN-CODE can tell a caller error from a data error without parsing the message. The next lesson reads the other half of the contract, the environment variables a step sets, and looks at RETURN-CODE from the shell side.
Exercises
- Add an operation mod that prints the remainder of a division with FUNCTION MOD. Which status does a zero divisor get now?
- Change WS-COUNT to PIC 9 and run the calculator with no arguments. What number is printed by the usage path?
- Run ./calc add "1 2" 3. How many arguments does the program see, and what does TEST-NUMVAL say about the first one?
- Print the whole command line in the usage message. Which part of the line does it add?
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
What does ACCEPT FROM ARGUMENT-NUMBER give the program?
Answer
The number of arguments after the program name
The page shows arguments 003 for three words and 000 for none.
-
What does an ACCEPT FROM ARGUMENT-VALUE past the last argument do?
Answer
Leaves the receiving field as it was and raises nothing
The run with one argument printed empty brackets for the second and third.
-
Which statement gives the arguments as one string?
Answer
ACCEPT FROM COMMAND-LINE
The line block printed alpha 42 two words as one value.
-
What is the exit status of ./calc div 1 0?
Answer
3
The division by zero path moves 3 to RETURN-CODE.
-
What does this program print?
IDENTIFICATION DIVISION. PROGRAM-ID. Q5. DATA DIVISION. WORKING-STORAGE SECTION. 01 S PIC X(6) VALUE "42.50". 01 N PIC 9(4)V99. PROCEDURE DIVISION. COMPUTE N = FUNCTION NUMVAL(S) DISPLAY N. STOP RUN.Answer
0042.50
The value goes into four integer digits and two decimals, which prints as 0042.50.
-
What does this program print?
IDENTIFICATION DIVISION. PROGRAM-ID. Q6. DATA DIVISION. WORKING-STORAGE SECTION. 01 W PIC X(6) VALUE "AB CD ". PROCEDURE DIVISION. DISPLAY "[" FUNCTION TRIM(W) "]". STOP RUN.Answer
[AB CD]
TRIM removes the trailing space only.
-
What does this program print?
IDENTIFICATION DIVISION. PROGRAM-ID. Q7. DATA DIVISION. WORKING-STORAGE SECTION. 01 S PIC X(6) VALUE "12.5x". 01 N PIC 9(4). PROCEDURE DIVISION. IF FUNCTION TEST-NUMVAL(S) = 0 MOVE FUNCTION NUMVAL(S) TO N DISPLAY N ELSE DISPLAY "bad" END-IF. STOP RUN.Answer
bad
The x makes TEST-NUMVAL non-zero, so the ELSE branch runs.
-
What does this program print?
IDENTIFICATION DIVISION. PROGRAM-ID. Q8. DATA DIVISION. WORKING-STORAGE SECTION. 01 A PIC 9(2) VALUE 5. 01 B PIC 9(2) VALUE 3. 01 C PIC 9(3). PROCEDURE DIVISION. COMPUTE C = A * B + 1 DISPLAY C. STOP RUN.Answer
016
The multiplication happens before the addition: 15 + 1.
-
Why are the messages of the calculator sent UPON STDERR?
Answer
So a result file written by the step stays free of error text
Messages go to the error stream and the result to the normal one.
-
What does the status 2 of the usage path mean to the shell?
Answer
The program was called with a wrong command line
The calculator uses 2 for a command-line error, 3 and 4 for data errors.
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