Sunday, 22 September 2013

Static Call in COBOL: COBOL’s Performance Booster.

COBOL subroutine, Static Call in COBOL

Static Call in COBOL


In COBOL programming, the static call in COBOL is a powerful feature that allows programs to invoke subprograms or perform subprogram calls. The static call statement in COBOL provides a way to execute a specific subprogram without the need for dynamic resolution at runtime. 

This article will provide a clear and concise overview of static calls in COBOL, including their performance considerations, implementation examples, and best practices.

Static Call in COBOL.

The COBOL static call is a mechanism that enables the program to directly invoke a subprogram without any runtime resolution. Unlike dynamic calls, which require the program to determine the subprogram to be called at runtime, static calls are resolved at compile time. This means that the program knows the target subprogram and its location in memory before execution.

The static call statement in COBOL follows the syntax:

CALL 'subprogram-name' USING parameters

Here, 'subprogram-name' is the name of the subprogram to be called, and parameters are the data items passed to the subprogram. The subprogram can be another COBOL program or a COBOL subroutine.

Example: CALL "EMP002" USING RECORD-1 


COBOL Static Call, Static Call in COBOL
COBOL Static Call. 


Static Call Performance Considerations


When using static calls in COBOL, there are several performance considerations to keep in mind:
  • Compile-time resolution: Static calls are resolved at compile time, which means that the program needs to be recompiled whenever the target subprogram changes. This can result in longer development cycles and increased maintenance efforts.
  • Efficiency: Dynamic calls are less efficient than static calls because they require runtime resolution, while the program can directly jump to the exact location of the subprogram in static calls.
  • Memory usage: Static calls require the subprogram to be loaded into memory at compile time. This can result in increased memory usage, especially if the program has multiple static calls to different subprograms.
Considering these performance considerations, it is important to carefully evaluate the use of static calls in COBOL applications. While static calls can improve performance, they may not be suitable for all scenarios.

How to Use Static Call in COBOL.

To use static calls effectively in COBOL, consider the following best practices:

  • Identify appropriate scenarios: Static calls are best suited for situations where the target subprogram is unlikely to change frequently. If the subprogram is expected to change frequently or if dynamic resolution is required, dynamic calls may be more appropriate.
  • Organize subprograms: Group related subprograms together to minimize the number of static calls and reduce memory usage. This can improve the overall performance of the COBOL application.
  • Document dependencies: Clearly document the dependencies between the main program and the subprograms. This will help in understanding the flow of the program and identifying the impact of any changes in the subprograms.
  • Perform thorough testing: Before deploying the COBOL application, thoroughly test the static calls to ensure they are functioning as expected. This includes verifying the correct passing of parameters and validating the behavior of the subprograms.

COBOL Static Call Examples and Best Practices

Let's look at a couple of examples to illustrate the use of static calls in COBOL:

 IDENTIFICATION DIVISION.
 PROGRAM-ID. MAIN-PROGRAM.
*
* Main program that will call two subprograms.
*
 DATA DIVISION.
 WORKING-STORAGE SECTION.
 01 PARAM-1      PIC X(10) VALUE 'Hello'.
 01 PARAM-2      PIC X(10) VALUE 'World'.

 PROCEDURE DIVISION.
 MAIN-PROCEDURE.
     CALL 'SUBPROGRAM-A' USING PARAM-1, PARAM-2.
     CALL 'SUBPROGRAM-B' USING PARAM-1, PARAM-2.
     STOP RUN.


 IDENTIFICATION DIVISION.
 PROGRAM-ID. SUBPROGRAM-A.
*
* Subprogram A called by Main-program.
*
 DATA DIVISION.
 WORKING-STORAGE SECTION.
 01 PARAM-1     PIC X(10).
 01 PARAM-2     PIC X(10).

 PROCEDURE DIVISION USING PARAM-1, PARAM-2.
     DISPLAY 'Subprogram A: ' PARAM-1 ' ' PARAM-2.
     EXIT PROGRAM.


 IDENTIFICATION DIVISION.
 PROGRAM-ID. SUBPROGRAM-B.
*
* Subprogram B called by Main-program.
*
 DATA DIVISION.
 WORKING-STORAGE SECTION.
 01 PARAM-1     PIC X(10).
 01 PARAM-2     PIC X(10).

 PROCEDURE DIVISION USING PARAM-1, PARAM-2.
     DISPLAY 'Subprogram B: ' PARAM-1 ' ' PARAM-2.
     EXIT PROGRAM.


In this example, the main program calls two subprograms, SUBPROGRAM-A and SUBPROGRAM-B, using static calls. The parameters PARAM-1 and PARAM-2 are passed to the subprograms, which display the values received.

By following these examples and best practices, you can effectively implement static calls in your COBOL applications, improving performance and maintaining code organization.

Conclusion.

The COBOL static call is a valuable feature that allows programs to directly invoke subprograms without runtime resolution. By understanding the performance considerations, best practices, and examples provided in this article, you can effectively utilize static calls in your COBOL applications. Remember to carefully evaluate the suitability of static calls for each scenario and consider the impact on development cycles, efficiency, and memory usage.

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

► Youtube
► Facebook 
► Reddit

Thank you for your support. 

Mainframe Forum™

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™

Master ARRAY In COBOL In Just A Few Minutes!

ARRAY In COBOL
ARRAY In COBOL

In this tutorial, you will learn ARRAY processing. Array processing is a critical part of any programming language and they are widely used by the programmer to perform various data operations.

An ARRAY in COBOL is used for various data manipulation. For example, a tax table may be used to look up the amount of income tax to be withheld from payroll checks. You can read a file into an array in COBOL or search in COBOL.

The easiest way to work with COBOL array/table is to use subscripts or Index in COBOL. Array in COBOL is also known as the Table in COBOL, these terms are used interchangeably in the tutorial. So, let's start with the array tutorial.


What is an ARRAY in COBOL?


An Array in COBOL or Table in COBOL is a linear data structure that is used to store, access and process data easily and efficiently. It is a collection of homogenous data items (Data items that possess the same size and structure) that can be referred by a single name.

The data items contained in a COBOL Table (i.e. array in COBOL) are called its elements and can be used in arithmetic and logical operations. The elements of a table are stored contiguously in the memory. The elements of a Table can be either elementary items or group items and further a Table can be either of fixed length or variable length.

In COBOL, an array/table can have dimensions up to 7. Though an array/table is a very simple data structure, it is a very powerful tool available to the programmer. It can save many lines of code and simplify the programming logic to a considerable extent.

COBOL has a wealth of logical control structures and statements that allow the programmer to exploit the complete range of Table handling capabilities.

What are subscripts COBOL?


A subscript is a field that contains an occurrence number, which indicates the occurrence of the field you want to refer to. A subscript must be defined as an integer, and it should be defined with computational usage for efficiency.

You can use relative subscripts to increase or decrease a subscript value by a literal value when you refer to a table entry.
You can use the same subscript to refer to entries in two or more arrays/tables in COBOL, and you can use two or more subscripts to refer to the entries in a single COBOL array/ COBOL table.

Note: The COBOL array index starts from 1 (i.e. One). However, in other programming languages such as C, C++, Python its starts from 0 (i.e. Zero).

What is Index in COBOL?


An index represents a displacement value from the start of the table. Due to this fact, indexes are more efficient to use than subscripts, which have to be converted to displacement values.

You define a table that uses an index the same way you define a table that uses a subscript except that you include the Indexed By clause. This clause names one or more indexes that will be used to refer to the table entries. The indexes you name are defined automatically.

How to Define the index in COBOL?


01 TEMP-TABLE         VALUE ZERO.
   05 DAY-GROUP       OCCURS 16 TIMES
                      INDEXED BY PRICE-TABLE-INDEX.
     10 TEMP-DAY      PIC 9(03).
     10 TEMP-IN-DGR   PIC S99V99.
*

How to define an Array in COBOL (i.e. table in COBOL)?


Similar to any other data item, an Array in COBOL is defined in the DATA DIVISION section of a COBOL program. To specify the repeated occurrence of data items with the same format, the OCCURS clause is used. The OCCURS clause specifies the maximum number of elements that can be stored in the COBOL Table/array.

The syntax of a table definition.


level-number data-name OCCURS integer TIMES

Note: It is important to note that the OCCURS clause can be used with only level numbers 02 to 49. It cannot be used with level number 01, for, it must be used to define fields and not records.

Example 1: One-dimensional Array in COBOL.


     DATA DIVISION.
    *
     WORKING-STORAGE SECTION.
    *
     01 WS-DAILY-TEMPERATURE.
        05 WS-HOURLY-TEMPERATURE PIC 9(2)V9 OCCURS 24 TIMES.

Example 2: Two-dimensional in COBOL.


     DATA DIVISION.
    *
     WORKING-STORAGE SECTION.
    *
     01 WS-TEMPERATURE.
         05 WS-DAYS       OCCURSTIMES.
           10 WS-HOURS    OCCURS 24 TIMES.
              15 WS-TEMP  PIC 9(2)V9.

Example 3: (Three) 3 dimensional array in COBOL.

     DATA DIVISION.
   *
     WORKING-STORAGE SECTION.
   *
     01 WS-TEMPERATURE.
        05 WS-DAYS            OCCURS  7 TIMES.
           10 WS-HOURS        OCCURS 24 TIMES.
                                  15 WS-MINUTES   OCCURS 60 TIMES.
                 25 WS-TEMP   PIC 9(2)V9.

Dynamic array in COBOL with example.


Array in COBOL (i.e. Tables in COBOL) can also have a variable number of elements. This does not imply that the size of the table is dynamically adjusted depending on the number of elements. The variable size is useful to avoid out of range accesses. For example, if a table has been declared to have between 10 and 200 entries and it currently has 30 entries, access to the 55th element is illegal.

 Example 4: Dynamic array in COBOL example.


      01 WS-EMP-TABLE.
         05 WS-NUMBER-OF-EMPLOYEES PIC 9(6).
         05 WS-EMP-REC   OCCURS 1 TO 500 
                         TIMES DEPENDING ON WS-NUMBER-OF-EMPLOYEES.
            10 WS-EMP-NAME PIC A(30).
            10 WS-EMP-DEPT PIC X(04).

Accessing the elements of an array in COBOL (i.e. Table handling in COBOL).


The elements of a table/array can be accessed with the data-item that is lowest in the hierarchy with either a subscript or an index. The subscript/index must be enclosed within a pair of parentheses.

If the elements are to be accessed using the INDEXED BY phrase, then it should be declared in the DATA DIVISION using the INDEXED BY clause as shown in the following examples.

Example 1: Two-dimensional Table defined using INDEXED BY clause (i.e. usage is index in COBOL)


      DATA DIVISION.
  *
   WORKING-STORAGE SECTION.
  *
   01 WS-TEMPERATURE.
      05 WS-DAYS        OCCURS  7 TIMES INDEXED BY I.
         10 WS-HOURS    OCCURS 24 TIMES INDEXED BY J.
            15 WS-TEMP  PIC 9(02)V9.

Example 2: Three-dimensional Table defined using INDEXED BY clause (i.e. usage is index in COBOL)


      DATA DIVISION.
     *
      WORKING-STORAGE SECTION.

      01 WS-TEMPERATURE.
         05 WS-MONTHS        OCCURS 12 TIMES INDEXED BY I.
            10 WS-DAYS       OCCURS 31 TIMES INDEXED BY J.
               15 WS-HOURS   OCCURS 24 TIMES INDEXED BY K.
                  20 WS-TEMP PIC 9(02)V9.

How to initialize an Array in COBOL?


In certain cases, you might need to initialize the values in a table before you use the table. However, the best practice is to initialize the COBOL array before using it in the program. To do that, you can include Value clauses in the table definition as shown in the example below. Here, the VALUE CLAUSE is coded on the elementary items in the table/array. Then, each occurrence of those items is initialized with the indicated value, in this case, zero.

Although you can initialize each field in a table separately, you can also initialize the entire table at once by coding a Value clause at the table level or at the group level (the level that contains the Occurs clause). However, that works only if all of the fields in the table are defined with the same usage and you want to initialize each field to the same value. Otherwise, you’ll want to define each field separately.

Example 1: One-dimensional table initialized using the VALUE clause.


    DATA DIVISION.
   *
    WORKING-STORAGE SECTION.
   *
     01 WS-MONTHS   VALUE “JANFEBMARAPRJUNJULAUGSEPOCTNOVDEC”.
        05 WS-MONTH OCCURS 12 TIMES PIC A(03).

Subscript in COBOL with an example:


The easiest way to work with tables is to use subscripts. To refer to a specific table entry using subscripts, you indicate the occurrence number of the entry. The first entry in a table has an occurrence number of one, the second entry has an occurrence number of two, and so on. The subscript can be a positive integer or a numeric data item or an integer type data item + or – an integer or another integer type data item. If a Table with name WS-EMP-TABLE has 1000 elements, then they are WS-EMP-TABLE(1), WS-EMP-TABLE(2), . . . , WS-EMP-TABLE(1000).

Then the largest value that the subscript can take is the integral value specified in the OCCURS clause. To deal with multi-dimensional arrays, there is a flavor of a PERFORM statement that can be used.

The definition of a subscript.

WORKING-STORAGE SECTION.
*
* Define a subscript.
  05 YR-MNTH-SUB    PIC S99   COMP. 
*
PROCEDURE DIVISION.
*
*Access data from an array using subscript in COBOL.
     MOVE WS-MONTH(YR-MNTH-SUB) To HDR-DTE-MNTH

The index in COBOL with an example:


Although subscripts are easy to use, they’re not as efficient as using indexes. That’s because an index represents a displacement value that points directly to the location of a table entry.

The index in COBOL syntax. 


level-number data-name OCCURS integer TIMES
             INDEXED BY {index-name-1} ...

How to code the SET statement.


Because an index contains a displacement value and not an occurrence number, you can’t use the standard COBOL statements to assign a value to an index or to change the value of an index. Instead, you have to use the Set statement to do that. 

Though the value of a subscript can be modified using MOVE and ARITHMETIC statements, the value of an index can be modified using only the SET statement. There are two basic formats of the SET statement.


SET index name 1 [, index name 2] . . . TO integer

Examples:

      1. SET I TO 4.
      2. SET I, J TO K. Here I, J and K are all indexes.
      3. SET index name 1 [index - name - 2 UP BY integer

Subscript in COBOL Vs Index in COBOL.


Subscript in COBOL: Represents an occurrence of a Table element. Is defined explicitly in the WORKING-STORAGE SECTION. To change the value of subscript, use a PERFORM . . . VARYING or a MOVE statement or an Arithmetic statement for example:

MOVE WS-HOURLY-TEMPERATURE (HRS-SUB) TO HDG3-TEMP-RNGE.

The index in COBOL: Represents the displacement from the address of the first element in the Table. An index is a special subscript created and maintained by the operating system. To change the value of subscript, use a PERFORM . . . VARYING or a SET statement.

Array in COBOL Coding rules.


To refer to an entry in a one-level table, you code the name of the field you want to refer to followed by a space and the subscript name, an occurrence number, or a relative subscript enclosed in parentheses.

If you code a relative subscript, the plus or minus sign must be preceded and followed by a space.


Summary.


A quick recap of the array in the COBOL tutorial. 
  1. You define an array in COBOL using an Occur clause. You can use either indexes or subscripts. An index represents a displacement value, and a subscript represents an occurrence number that must be converted to a displacement value. 
  2. You can use the initialized clause to initialize the table before using the table in program logic.  
  3. To load data in the table, you use a Perform Varying statement that varies the value of the subscript or index for the table. To search a table, you can use a Perform Varying statement for each level of the table.
  4. When you define a table with indexes, you can use the Search statement to perform a sequential search on the table. If the entries are in sequence by a key field, you can also use the Search All statement to perform a binary search.
  5. You use the Set statement to work with indexes in COBOL or index data items. You can use this statement to assign an index value or to increase or decrease an index value.

Created with Artisteer

COBOL Application.

An application is a collection of programs design to achive specific task or objective. For example Employee Management System which is broadly categories into 4 different categories based on business requirment.

  • Employee Registration: consist of module which will be design to handle employee registration.
  • Employee Salary Calc: consist of modules which will be desing to calcualte salary of employees. 
  • Employee Daily Req: consist of module which will be desing to handle daily request from employees.
  • Employee Reporting: consist of module which will be desing to handle monthly or daily reporting.
Many applications consist of several separately compiled programs linked together. A RUN UNIT (the COBOL term synonymous with enclave in Language Environment) includes one or more object programs and can include object programs written in other Language Environment member languages.
All program which is include in applicaition were broadly divied into two category :
  • Main Program : If a COBOL program is the first program in a run unit, that COBOL program is the main program.
  • Sub Program :  All other COBOL programs in the run unit are called subprograms.  
No specific source-code statements or options identify a COBOL program as a main program or subprogram.


Created with Artisteer

COBOL Ending and Reentering.


COBOL is one of the robust language, which include lot of prominent feature. COBOL provide three different termination statement that can be used terminate processing of application program. Refer below figure and table for more details.
Termination StatementMainprogram Subprogram




EXIT PROGRAM




No action taken
Return to calling program without ending the run unit.
An implicit EXIT PROGRAM statement is generated if the called program has no next executable statement.
In a threaded environment, the thread is not terminated unless the program is the first (oldest) one in the thread.








STOP RUN
Return to calling program.1 (Might be the operating system, and application will end.)

STOP RUN terminates the run unit, and deletes all dynamically called programs in the run unit and all programs link-edited with them. (It does not delete the main program.)

In a threaded environment, the entire Language Environment enclave is terminated, including all threads running within the enclave.
Return directly to the program that called the main program.
1 (Might be the operating system, and application will end.)

STOP RUN terminates the run unit, and deletes all dynamically called programs in the run unit and all programs link-edited with them. (It does not delete the main program.)

In a threaded environment, the entire Language Environment enclave is terminated, including all threads running within the enclave.



GOBACK
Return to calling program.

1 (Might be the operating system, and application will end.)

GOBACK terminates the run unit, and deletes all dynamically called programs in the run unit and all programs link-edited with them. (It does not delete the main program.)

In a threaded environment, the thread is terminated.2





Return to calling program. In a threaded environment, if the program is the first program in a thread, the thread is terminated.2

  1. If the main program is called by a program written in another language that does not follow Language Environment linkage conventions, return is to this calling program.
  2. If the thread is the initial thread of execution in an enclave, the enclave is terminated.



Created with Artisteer

New In-feed ads