Summary -

In this topic, we described about the Variable with detailed example.

Variable is also called as a data item. Variable is a name used to identify the memory location to where the program writes, retrieves the data while processing.

Variable is a named memory location that is used during the program execution. Every variable using in the COBOL program should declare in the DATA DIVISION of the same program.

A Variable is a pointer to the memory area from starting byte to the ending byte based on the declaration's specified length. Variable declaration always contains the data type in its declaration.

Rules -

Variable name declaration should follow the below rules -

  • It is a combination of alphabets(A-Z), digits(0-9) and hyphens (-).
  • Variable name length can be a minimum of 1 character.
  • Variable name length can be a maximum of 30 characters.
  • The variable name is a combination of COBOL user-defined words.
  • Variable name should not be a COBOL reserved word(i.e., ACCEPT, ADD, MOVE, etc..).
  • The variable name does not allow white spaces( ).
  • Starting character should be an albhabet(A-Z) and should not be a digit(0-9) or hyphen(-) or space( ).
Valid Variable Names Invalid Variable Names
WS-A
VAR
A12
1A2
WS-2
1ABC
WRITE1
MOVE1
WS_A - Doesn't allow hyphen
MOVE, WRITE - Doesn't allow Reserved words
*ABC, A(B, AB) - Doesn't allow special characters
Note! The variable-length is depending on the data type used for declaration. Note that variable name length differs from the variable length.

Example -

Let's take the below example to understand better. A, B & C are variables and declared as shown below -

01 A		PIC X(10).
01 B.
   02 C  	PIC X(03).
   02 FILLER 	PIC X(03).

Assume variable A allocated with 10 bytes of memory from 1000 to 1010, and variable B with 6 bytes of memory from 1100 to 1106. The below diagram representing memory bytes for the above declaration.

Variable Diagram

A is assigned with the memory location from 1000 to 1010. So, referring to the variable A accesses the 10 bytes data from memory location 1000 to 1010. C is assigned with the memory location from 1100 to 1103. So, referring to the variable C accesses the 3 bytes data from memory location 1100 to 1103.

The memory allocated for FILLER can't be accessed because FILLER is not a variable. However, B is declared as a group variable for the entire 6 bytes, includes C and FILLER. So, by referring to variable B, we can access the 6 bytes of data from memory.

Practical Example -

Problem - Below example describes about how the variable assignment works in COBOL.

Code -

Variable COBOL program code
----+----1----+----2----+----3----+----4----+----5----+----6----+----7--
***************************** Top of Data ******************************
       IDENTIFICATION DIVISION.                                         
       PROGRAM-ID. VARIABLE.                                            
       AUTHOR. MTH.                                                     
                                                                        
       DATA DIVISION.                                                   
       WORKING-STORAGE SECTION.                                         
                                                                        
       01 WS-A        PIC X(40)                                         
                      VALUE 'MAINFRAME SYSTEMS ARE LEGENDARY SYSTEMS'.  
       01 WS-B.                                                         
          05 WS-C     PIC X(20).                                        
          05 FILLER   PIC X(20).                                        
                                                                        
       PROCEDURE DIVISION.                                              
                                                                        
           MOVE WS-A      TO WS-B.                                      
           DISPLAY 'DATA IN WS-A: ' WS-A.                               
           DISPLAY 'DATA IN WS-B: ' WS-B.                               
           DISPLAY 'DATA IN WS-C: ' WS-C.                               
           STOP RUN.                                                    
**************************** Bottom of Data ****************************

Output -

Variable COBOL program output

Explaining Example -

In the above example, the WS-A variable is declared as length 40 and initialized with the string "MAINFRAME SYSTEMS ARE LEGENDARY SYSTEMS". WS-B variable declared as a group variable with elementary variables WS-C and FILLER of each 20 characters long.

When moving WS-A to WS-B, entire data from WS-A assigned to WS-B. i.e., first 20 characters set to WS-C and following 20 characters to FILLER. Displaying the variables WS-A and WS-B, produces the same result. Displaying variable WS-C, shows the first 20 characters moved from WS-A.

Note! Variables WS-A and WS-B refer to different memory locations. However, WS-A and WS-B have the same content.