Variables in COBOL
Declaring and initializing variables might be simple for programmers. However, if this is your first programming language, think of a variable as a container in which you can fill an unknown quantity – a value of said data type. Based on its use, this variable’s value is then stored at a particular memory address and which can be accessed based on the variable name chosen by the programmer. Now, when we use a variable, we declare and often initialize it with a value that could be of boolean, string, int, or double type. Most importantly, we use descriptive names so that we know what that variable is being used for.
For example, in Java, which is a strongly typed language, we would use the following syntax:
In contrast, in Python, which is a loosely typed language, we use the syntax without the data type to do the same:
In COBOL, this is almost the same. Particularly if we want to declare and initialize variables for computation.
That said, we refer to variables as data items in COBOL and for the sake of following conventionally accepted terms when learning this language, we’ll do the same.
Introduction to COBOL Variables
While in popular languages, we usually declare and initialize a variable prior to its use in the code, this process is more formal in COBOL, as it has to be declared in a specific area of the program.
Given that a COBOL program is divided into four divisions, we have to look for the Working Storage Section which falls under the Data Division. This is where you will add COBOL data items – what we commonly refer to as variables – in the program.
Speaking of which, COBOL works with data such as literals and figurative constants too and which is something that we will look at in the following sections.
Very simply, it is composed of parts that consist of a unique level (01) and name (NumberOne), a data categoryC 9), and an optional value (VALUE 2).
Let’s examine each of these parts in a COBOL declaration in the next section.
Variables in COBOL Syntax
Even if the syntax that COBOL adopts seems to be difficult, it’s really very simple. One must remember that this language was developed at a different time, so it might seem different.
So, let’s break down each of the parts as simple as possible, beginning with the data item level number 01:
Data items are represented by level numbers and which can be used to group lower-level items under higher-level items, as shown below:
Clearly, data items with level number 03 can be grouped under the data item level number 01 labeled StudentInfo.
While you can choose numbers between 1-49 to give your data items a sense of hierarchy, it must be pointed out that level number 01 is the topmost. This is what we will use when declaring each data item in a COBOL program.
What we now identify with as a variable name is what is referred to as a data name in COBOL. As with naming variables in popular languages, here are a few rules to keepind when it comes to COBOL data names:
- A data-name can use alphabets a-z and A-Z as well as numbers from 0-9. A hyphen is also permitted.
- The greatest number of characters that a data name can take is 30 characters.
- Each data name must have at least one alphabet
- Hyphens cannot be used to begin or end a data name.
- A data name cannot be a “reserved word”. These keywords that perform certain functions in COBOL. Examples of reserved words include MULTIPLY, COUNT, FOR, ELSE, DISPLAY and ACCESS.
As you keep these rules in mind, just remember that the data name you select should describe what that variable contains or will be used for.
Next, we look at the Picture clause (PIC 9), and which defines the data type and size of the item. Simply put, the system gets a “picture” of this declaration. It serves as a template or an example. Looking at the clause above, the symbol 9 informs the compiler that it should accept single-digit integers only.
Now, what if you want the system to accept double-digit numbers? Here’s the declaration for just that:
Both these declarations are equally acceptable since they will store the value 22 for both the NumberTwo and NumberThree data names. Similarly, we use the “S” and “V” symbols to indicate sign and decimal places, as shown below:
Lastly, we use the VALUE keyword to assign a value to the data name, as shown below:
Of course, there are other types of data that can be declared other than just the numeric type such as the Alphabetic and Alphanumeric types. Usinearlier example, here are the others:
As we can see above, PIC A is used to declare alphabetic data items while PIC X is used to declare alphanumeric data items. As usual, the value enclosed within each parenthesis indicates the number of digits or characters that need to be allotted for this data item.
Take some time to compare the values assigned to the data names and you’ll understand how the picture clause is used for all three types of data items.
With that said, when declaring and initializing data items in COBOL, please do not forget the full stops after each declaration.
Literals in COBOL
By definition, a literal in COBOL is merely the value that we would associate with the data item. This is the simplest category of data that is used. Based on the example used before and as shown below, the value 2 assigned to NumberOne is literal.
Now, there are two types of literals in COBOL: numeric and alphanumeric literals.
As you guessed, numeric literals are just numbers, much like the value shown above. Other examples of numeric literals include -3 and 34.21. In other words, numeric literals also accommodate both signs and decimals.
On the other hand, alphanumeric literals are values that contain both alphabets and numbers or either. Alphanumeric literals are usually enclosed by quotes. Examples of alphanumeric literals include “-321”, “3.24”, “34”, “ans425”, and “answer”.
One thing that must be remembered about literals: you do not have to declare them in the Data Division of a COBOL program but add them directly to the Procedure Division. This is the reason why the VALUE is consideredbe optional when declaring data items.
Figurative Constants in COBOL
Now, there are times when using literals to initialize a data item might not be pertinent or necessary. For example, you might just want to fill a data item with an arbitrary sequence of zeroes. Here’s what you can do:
Or if a data item needs to be filled with other characters such as quotes, spaces among other ASCII values based on the size of the data item.
This is where figurative constants – being reserved words – might come in handy and which will fill the entire item. A literal assigned by the user will not be able to do this.
Another reason why figurative constants are good is because of the readability they offer. If you compare the two statements of the data items, which reads better?
The form used by the first statement seems better than the second, for obvious reasons.
Example: Run a Sample COBOL Program
Now, muscle memory is very important when learning to code. We cannot learn programming just by reading but have to try a sample COBOL program too. Here’s an example of a COBOL program that displays details of a particular employee:
My Name:Rahul Biswas My Occupation: Programmer Employee ID: MSFT107001 My CGPA: 03.23
One final thing: the environment division, while not necessary in this sample, has been placed just to show you the order of the four divisions of a COBOL program.
Having said that, our introduction to COBOL variables has now concluded. Or as they’re called: data items.