COBOL/Variables

Variables in COBOL
In computer science, a variable is a named memory location where a program can store or retrieve data. The variable name is used to identify the memory location.

In COBOL, variables (known as "data items") must be described in terms of their size and type. Every data item used in a COBOL program needs to have been declared with a description entry in the data division.
 * Data items in a main program are declared in the working-storage section. They may be tables, records, file descriptor records, etc.
 * The name of a data item may use alphanumeric characters, with its first character alphabetic.

The Variables lesson in the Wikiversity course Programming Fundamentals provides additional background, readings, multimedia resources, key terms, and activities related to understanding and using variables.

Please study those materials to better understand and extend the following example program.

The data items in this example program are FAHRENHEIT and CELSIUS. Their size and type are declared by the PICTURE clause, described below.

The PICTURE Clause
While most of the syntax in this example should be apparent, the PICTURE specifications are likely to be cryptic. In COBOL, PICTURE (often shortened to "PIC") describes the size and format of the data item. In doing so it also provides the conversion specifications for interpreting input or displaying output, is analogous to the Fortran Format statement, or the printf format string used in C and other languages.

The PICTURE clause is used to define the following items:
 * Data type can be numeric, alphabetic, or alphanumeric. Numeric type consists of only digits 0 to 9. Alphabetic type consists of letters A to Z and spaces. Alphanumeric type consists of digits, letters, and special characters.
 * Sign can be used with numeric data. It can be either + or –.
 * Decimal point position can be used with numeric data. The assumed decimal position is the position assigned to the decimal point and is not included in the data.
 * Length defines the number of bytes used by the data item.

These symbols are used in a PICTURE clause:


 * 9 Numeric
 * A Alphabetic
 * X Alphanumeric
 * V Implicit Decimal
 * S Sign (+ or -)
 * P Assumed Decimal

Try It
Copy and paste the code above into one of the following free online development environments or use your own compiler / interpreter / IDE.
 * JDoodle
 * paiza.IO
 * TutorialsPoint

Math Words
One of the original goals of COBOL was to make the language easy to read and write. As a result, one design decision was to use English language verbs instead of mathematical symbols in writing assignment statements. For example, this sample program includes the following assignment statement:

COMPUTE CELSIUS = (FAHRENHEIT - 32) * 5 / 9.

This statement uses the mathematical symbols =, -, *, and / to indicate assignment, subtraction, multiplication, and division, respectively. The COMPUTE verb allows use of these mathematical operators.

Alternatively, English language verbs can be used to specify mathematical operations. The correspondence is shown in the table below.

Assignment
Part 1:

Modify the sample program above, replacing the line COMPUTE CELSIUS = (FAHRENHEIT - 32) * 5 / 9. With code that uses the English language verb form of the assignment statement. Make any other modifications to the program needed to accommodate this form of the assignment statement.

Run and test the modified program.

Part 2:

Modify the sample program above to print out a single paycheck. It is OK to use string literals for each field, however the paycheck must include:
 * A check number
 * The date
 * Pay to the order of name
 * Amount in decimal dollars and cents.
 * The above amount spelled out in words

Run and test the modified program.