Sunday, 22 September 2013

Understanding the Difference Between Working and Local Storage Variables.


Working Storage vs Local Storage Variables in COBOL.

In this article, we will explore working storage and local storage variables in COBOL, their characteristics, and how they are used in COBOL programming. By the end of this article, you will have a good understanding of working storage variables in COBOL and how they can be used in your own COBOL programs.

Introduction - Variables in COBOL.

Working storage variables in COBOL are temporary storage locations in a COBOL program. These variables are used to store intermediate values during program execution. They are defined in the "Working Storage" section of a COBOL program and are accessible from any part of the program. Unlike other types of variables, working storage variables are not initialized by default, which means their values can change during program execution. Local variables in COBOL are defined in the Local Storage Section.

Working Storage and Local Storage are two important sections of the COBOL program which is used for defining temporary variables used for different processing and calculation. But interestingly variable defined in these sections has a different scope.

Working Storage for programs is allocated at the start of the run unit. Any data items with VALUE clauses are initialized to the appropriate value at that time. For the duration of the run unit, WORKING-STORAGE items persist in their last-used state. 

Exceptions are:
  • A program with INITIAL specified in the PROGRAM-ID paragraph In this case, WORKING-STORAGE data items is reinitialized each time that the program is entered.
  • A subprogram that is dynamically called and then canceled In this case, WORKING-STORAGE data items are reinitialized on the first reentry into the program following the CANCEL.
Lastly, working storage variables are deallocated at the termination of the run unit.

What are Working Storage Variables in COBOL?

COBOL (Common Business Oriented Language) is a high-level programming language used for business applications and data processing. In COBOL, the "Working Storage" section is used to declare working storage variables (i.e. temporary variables) that are used for intermediate calculations and temporary storage within a program.

Working storage variables in COBOL have the following characteristics:
  • They are defined in the "Working Storage" section of a COBOL program.
  • They are stored in the main memory of a computer.
  • They are accessible within the entire program.
  • They are not initialized by default, so their values can change during program execution.
  • They can be any valid COBOL data type, such as numeric, alphanumeric, or alphabetic. 

What is the difference between Working Storage and Local Storage Variables?

In COBOL, the main difference between working storage variables and local storage variables is their scope and persistence. The following is the precise comparison between them.

Working Storage Variables:
  • Is defined in the "Working Storage" section of a COBOL program.
  • Is accessible from any part of the program.
  • Its values persist between different executions of a program.
  • Is used for intermediate calculations and temporary storage.
Local Storage Variables:
  • Is defined within a procedure division of a COBOL program.
  • Is only accessible within the procedure where it is defined.
  • Its values are not preserved between procedure calls.
  • Is used for temporary storage within a procedure.

In general, working storage is used for data that needs to be accessed throughout the program and persist between program executions, while local storage is used for temporary data within a procedure that does not need to be preserved.

Example:
   
   IDENTIFICATION DIVISION.
   . . .
   . . . 
   WORKING-STORAGE SECTION.
   01 WS-TOTAL PIC 9(5).
   . . .
   LOCAL-STORAGE SECTION.
   01 LS-ITEM-AMOUNT PIC 9(5).

   PROCEDURE DIVISION.
   100-CALCULATE-TOTAL.
   . . .
   . . .
       ADD LS-ITEM-AMOUNT TO WS-TOTAL.

in this example, WS-TOTAL is a working storage variable that is used to keep track of the total amount across multiple procedure calls, while LS-ITEM-AMOUNT is a local storage variable used to store the amount of an individual item within the procedure 100-CALCULATE-TOTAL.


In conclusion, working storage variables are an essential part of COBOL programming and provide a way to store intermediate values and temporary data within a program.



A separate copy of LOCAL-STORAGE data is allocated for each call of a program or invocation of a method, and is freed on return from the program or method. If you specify a VALUE clause on a LOCAL-STORAGE item, the item is initialized to that value on each call or invocation. If a VALUE clause is not specified, the initial value of the item is undefined.
Threading: Each invocation of a program that runs simultaneously on multiple threads shares access to a single copy of WORKING-STORAGE data. Each invocation has a separate copy of LOCAL-STORAGE data.
Created with Artisteer


Subscribe to Topictrick & Don't forget to press THE BELL ICON to never miss any updates. Also, Please visit mention the link below to stay connected with Topictrick and the Mainframe forum on - 

► Youtube
► Facebook 
► Reddit

Thank you for your support. 
Mainframe Forum™

No comments:

Post a Comment

New In-feed ads