ADD and SUBTRACT

The arithmetic of COBOL is written with verbs and prepositions, not with an equals sign: ADD A TO B means B = B + A, and SUBTRACT A FROM B means B = B - A. The receiving field is the one after TO or FROM, and it is the only field the statement changes.

TO and FROM

Two fields, four statements, and the output after each one shows which field moved:

IDENTIFICATION DIVISION. PROGRAM-ID. ADDSUB. DATA DIVISION. WORKING-STORAGE SECTION. 01 A-VAL PIC 9(3) VALUE 100. 01 B-VAL PIC 9(3) VALUE 25. 01 C-VAL PIC 9(4) VALUE 0. PROCEDURE DIVISION. ADD A-VAL TO B-VAL. DISPLAY "A " A-VAL " B " B-VAL " C " C-VAL. ADD A-VAL B-VAL TO C-VAL. DISPLAY "A " A-VAL " B " B-VAL " C " C-VAL. ADD 10 TO A-VAL B-VAL. DISPLAY "A " A-VAL " B " B-VAL " C " C-VAL. SUBTRACT A-VAL FROM C-VAL. DISPLAY "A " A-VAL " B " B-VAL " C " C-VAL. STOP RUN.

cobc -x -free -Wall -o addsub addsub.cob
./addsub

A 100 B 125 C 0000
A 100 B 125 C 0225
A 110 B 135 C 0225
A 110 B 135 C 0115

The first line is the plain form: ADD A TO B leaves A at 100 and makes B 125. The second is the plural form: both addends go to C, which had nothing to do with them. The third shows a literal and two receivers. The fourth is the subtraction, and it takes 110 from 225, not the other way round.

GIVING leaves the sources alone

A statement with GIVING writes the result into a third field and does not touch the operands. Without it, the receiving field is also a source, and the difference matters:

IDENTIFICATION DIVISION. PROGRAM-ID. GIVING. DATA DIVISION. WORKING-STORAGE SECTION. 01 P-VAL PIC 9(3)V99 VALUE 12.50. 01 Q-VAL PIC 9(3)V99 VALUE 0.25. 01 T-VAL PIC 9(4)V99 VALUE 0. PROCEDURE DIVISION. ADD P-VAL Q-VAL GIVING T-VAL. DISPLAY "add [" T-VAL "]". SUBTRACT Q-VAL FROM P-VAL. DISPLAY "sub P [" P-VAL "] T [" T-VAL "]". SUBTRACT Q-VAL FROM P-VAL GIVING T-VAL. DISPLAY "giving [" T-VAL "] P [" P-VAL "]". MULTIPLY 2 BY P-VAL. DISPLAY "mult P [" P-VAL "]". STOP RUN.

cobc -x -free -Wall -o giving giving.cob
./giving

add [0012.75]
sub P [012.25] T [0012.75]
giving [0012.00] P [012.25]
mult P [024.50]

Twelve fifty and twenty-five cents make twelve seventy-five, and P is still 12.50 after the ADD ... GIVING. The next line changes P to 12.25 because there is no GIVING; the line after it computes the same subtraction into T and leaves P at 12.25. The last line is MULTIPLY, which follows the same rule and is taken apart in its own lesson.

Several operands at once

One statement can take a list. On the left of TO the addends are added together, and every receiver after TO gets the whole sum; on the left of FROM the subtrahends are added together and the total is subtracted from each receiver. The two lists are read before anything is written, so a receiver can also appear as an addend without surprising the arithmetic.

In the first program ADD A-VAL B-VAL TO C-VAL gave 225, because C already held nothing, and ADD 10 TO A-VAL B-VAL moved both receivers by ten. Four statements would have written the same values, and the one statement is shorter without hiding anything.

Decimal fields add as they are

The hundredths of P-VAL and Q-VAL lined up by themselves, because both pictures have two decimal places and the compiler aligns them. That is not a coincidence of this program: decimal addition is exact, and the only rounding that can happen is the one the receiving picture forces at the end. A receiving field with two decimals and a sum with three loses the third, which is the subject of ROUNDED.

ADD CORRESPONDING

Two records with the same field names take a whole statement of additions at once:

IDENTIFICATION DIVISION. PROGRAM-ID. CORR. DATA DIVISION. WORKING-STORAGE SECTION. 01 SRC-REC. 05 QTY-F PIC 9(3) VALUE 4. 05 PRICE-F PIC 9(4)V99 VALUE 2.50. 01 DST-REC. 05 QTY-F PIC 9(3) VALUE 1. 05 PRICE-F PIC 9(4)V99 VALUE 10.00. PROCEDURE DIVISION. ADD CORRESPONDING SRC-REC TO DST-REC. DISPLAY "qty [" QTY-F IN DST-REC "]". DISPLAY "price [" PRICE-F IN DST-REC "]". STOP RUN.

cobc -x -free -Wall -o corr corr.cob
./corr

qty [005]
price [0012.50]

One and four is five, ten and two fifty is twelve fifty, and the source record is unchanged. The pairing rule is the one MOVE CORRESPONDING uses: elementary items with the same name, at any depth, and nothing else. A field that is named differently, or that has no numeric picture, is skipped without a word.

Two mistakes

The receiving field has to be a field, and it has to be numeric. A literal on the receiving side is a statement with nowhere to write:

IDENTIFICATION DIVISION. PROGRAM-ID. RULELIT. DATA DIVISION. WORKING-STORAGE SECTION. 01 X-FLD PIC 9(3) VALUE 10. PROCEDURE DIVISION. SUBTRACT 5 FROM 3. DISPLAY X-FLD. STOP RUN.

cobc -x -free -Wall -o rulelit rulelit.cob

rulelit.cob:9: error: '3' is not a numeric name
7 |
8 | PROCEDURE DIVISION.
9 > SUBTRACT 5 FROM 3.
10 | DISPLAY X-FLD.
11 | STOP RUN.

The message calls the literal a name, which is the compiler saying that the place after FROM is a field position. The same message comes for a text field:

IDENTIFICATION DIVISION. PROGRAM-ID. ALPHAADD. DATA DIVISION. WORKING-STORAGE SECTION. 01 N-FLD PIC 9(3) VALUE 5. 01 T-FLD PIC X(3) VALUE "010". PROCEDURE DIVISION. ADD N-FLD TO T-FLD. DISPLAY "[" T-FLD "]". STOP RUN.

cobc -x -free -Wall -o alphaadd alphaadd.cob

alphaadd.cob:10: error: 'T-FLD' is not a numeric name
8 |
9 | PROCEDURE DIVISION.
10 > ADD N-FLD TO T-FLD.
11 | DISPLAY "[" T-FLD "]".
12 | STOP RUN.

A PIC X field that happens to hold digits is still text, and arithmetic refuses it whether or not the value would have parsed. The conversion belongs in a MOVE or a function, as NUMVAL shows later in the course.

The reading mistake is not an error the compiler reports. SUBTRACT A FROM B is B - A, which is the opposite of the order of the words; the same holds for DIVIDE A INTO B, where B is the dividend. Reading every statement aloud as "B gets B minus A" is the habit that keeps the signs right.

Exercises

  1. Add a fourth statement to the first program that subtracts A-VAL and B-VAL from C-VAL at once. What is the result, and why is it not negative?
  2. Change T-VAL to PIC 9(1)V99 in the second program and run it. Which result is cut, and from which end?
  3. Add a field to the source record of the CORRESPONDING program that the destination does not have. What happens, and what does the build say?
  4. Write the same addition once with ADD ... TO and once with ADD ... GIVING, and print the fields in between. Which fields differ, and why?

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. What does ADD A TO B change?

    Answer

    B only: B becomes B + A

    The field after TO is the receiver and is the only operand the statement writes to.

  2. What does SUBTRACT A FROM B compute?

    Answer

    B - A, stored in B

    FROM names the field that is reduced, so the value after FROM is the receiver and the value before it is the subtrahend.

  3. What does GIVING add to an arithmetic statement?

    Answer

    A third field receives the result and the operands are left alone

    Without GIVING the receiver is also an operand; with it, the result goes to a field that the computation does not read.

  4. Which field of ADD A TO B must have a numeric picture?

    Answer

    B, the receiver; A may be numeric or a literal

    The receiver has to hold a number, and the compiler refuses a PIC X field there; the addend may be a literal.

  5. What does this program print? (A starts at 100, B at 25, C at 0.) IDENTIFICATION DIVISION. PROGRAM-ID. Q1. DATA DIVISION. WORKING-STORAGE SECTION. 01 A-VAL PIC 9(3) VALUE 100. 01 B-VAL PIC 9(3) VALUE 25. 01 C-VAL PIC 9(4) VALUE 0. PROCEDURE DIVISION. ADD A-VAL TO B-VAL. ADD A-VAL B-VAL TO C-VAL. DISPLAY "[" C-VAL "]". STOP RUN.

    Answer

    [0225]

    The first statement makes B 125, and the second adds 100 and 125 to C, which was zero.

  6. What does this program print? IDENTIFICATION DIVISION. PROGRAM-ID. Q2. DATA DIVISION. WORKING-STORAGE SECTION. 01 P-VAL PIC 9(3)V99 VALUE 12.50. 01 Q-VAL PIC 9(3)V99 VALUE 0.25. 01 T-VAL PIC 9(4)V99 VALUE 0. PROCEDURE DIVISION. ADD P-VAL Q-VAL GIVING T-VAL. DISPLAY "[" T-VAL "] P [" P-VAL "]". STOP RUN.

    Answer

    [0012.75] P [012.50]

    GIVING writes the sum into T and leaves P as it was, so the hundredths add up without changing either source.

  7. What does this program print? IDENTIFICATION DIVISION. PROGRAM-ID. Q3. DATA DIVISION. WORKING-STORAGE SECTION. 01 P-VAL PIC 9(3)V99 VALUE 12.50. 01 Q-VAL PIC 9(3)V99 VALUE 0.25. PROCEDURE DIVISION. SUBTRACT Q-VAL FROM P-VAL. DISPLAY "[" P-VAL "]". STOP RUN.

    Answer

    [012.25]

    FROM makes P the receiver, so P becomes 12.50 minus 0.25 and keeps its two decimal places.

  8. What does this program print? IDENTIFICATION DIVISION. PROGRAM-ID. Q4. DATA DIVISION. WORKING-STORAGE SECTION. 01 SRC-REC. 05 QTY-F PIC 9(3) VALUE 4. 05 PRICE-F PIC 9(4)V99 VALUE 2.50. 01 DST-REC. 05 QTY-F PIC 9(3) VALUE 1. 05 PRICE-F PIC 9(4)V99 VALUE 10.00. PROCEDURE DIVISION. ADD CORRESPONDING SRC-REC TO DST-REC. DISPLAY "[" QTY-F IN DST-REC "][" PRICE-F IN DST-REC "]". STOP RUN.

    Answer

    [005][0012.50]

    Each same-named numeric field is added to its partner in the destination: 1 + 4 and 10.00 + 2.50.

  9. What does this program print? IDENTIFICATION DIVISION. PROGRAM-ID. Q5. DATA DIVISION. WORKING-STORAGE SECTION. 01 A-VAL PIC 9(3) VALUE 10. 01 B-VAL PIC 9(3) VALUE 4. PROCEDURE DIVISION. SUBTRACT A-VAL FROM B-VAL. DISPLAY "[" A-VAL "][" B-VAL "]". STOP RUN.

    Answer

    [010][006]

    B is the receiver, so 4 minus 10 is minus six, and an unsigned picture keeps the magnitude 006 without a word from the compiler.

  10. What is the first line of the compiler output for this program? IDENTIFICATION DIVISION. PROGRAM-ID. Q6. DATA DIVISION. WORKING-STORAGE SECTION. 01 X-FLD PIC 9(3) VALUE 10. PROCEDURE DIVISION. SUBTRACT 5 FROM 3. DISPLAY X-FLD. STOP RUN.

    Answer

    error: '3' is not a numeric name

    The place after FROM must be a field, and the compiler names the literal it found there instead.

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

Banner image