Friday, July 13, 2018

Static and Dynamic call in COBOL and their compile JCL's

Much information on this topic is available in Google, but I personally felt that they are all scattered here and there. In this blog post, I've tried my best to explain the difference between Static and Dynamic call in COBOL and how to compile the COBOL programs that involve CALL statements.

We use CALL statements in COBOL to call sub-program(s).


What happens during a call?
Control from the Main program/Calling program is passed to the sub-program/Called program. The control however is returned back to the Main program, once the sub-program is done. EXIT/GO BACK statements are coded in the sub program to return the control back to the main program. 

Main program: Calling program.
Sub program: Called program. 


CALL statement in COBOL is usually coded inside the PROCEDURE DIVISION and in AREA B, as like the other statements. If you wonder what I just wrote in the previous line, then I strongly recommend you to check this link. If you wish to check the link later, PROCEDURE DIVISION in COBOL is where all the statements that does the processing, resides. AREA B is nothing but a rule that you should follow while writing COBOL programs. Certain entries must begin in AREA A (Columns 8 - 11) and others like CALL, must begin in  AREA B (Columns 12 - 72).


Right. Here we go!


What are the advantages in calling sub programs? 
Modularity: Anything which is modular is cool! Take a Desktop computer as example. There are various modules like RAM, Graphics Card, hard drive etc. If something doesn't work the way you want, you can just change that module. The sub programs that are being called from the Main program are the modules here. We can do changes to the sub program without modifying the main program. 

Reusability: Avoids duplication of effort.

Let's do a deep dive into COBOL's static and dynamic calls:
To explain calls, I'll be using a sample COBOL program to calculate the Tax amount (CGST+SGST) and the total amount payable while taking Amount and Tax percentage (GST) as Input to the main program. The tax amount and total amount payable will be calculated inside the Sub program. 

Static call and dynamic call both does the same stuff - to call the sub program. But, there are slight differences between the two and I've listed them below.



Static Call:
  • The called sub program is link-edited along with main program. 
  • The sub program's name will be enclosed within single quotes in the CALL statement. 
      • Ex: CALL 'SUBPGM' USING A B C. SUBPGM is the sub program's name. A, B and C are the arguments. 
  • Any changes made to the sub program will require the main program to be compiled along with the sub program. 
The main program is shown in Picture 3.1. Amount and GST percentage are passed as Input to the main program from the run JCL. 

Notice the CALL statement in line #17. This is CALL literal, where literal is the explicit name of the sub program, in this case, PGMB. 3 arguments are passed to the sub program; Amount, GST percentage and Total amount (for holding the total amount value i.e., Amount + Tax).

Note: Click on the picture to get an enlarged view.

Picture 3.1: Main program


The sub program is shown in Picture 3.2. Linkage section is defined to handle the data items that are passed as arguments to the sub program from the main program. In the PROCEDURE DIVISION, tax amount is calculated using Amount and GST percentage. GST is split into 2 components, CGST and SGST. Tax percentage is calculated for each component. Then, tax percentage is applied to the amount to derive total tax amount. Total amount payable is computed by adding amount and tax amount. LS-TOT-AMT data item in the sub program holds the final value. The corresponding data item of LS-TOT-AMT is used in the main program to display the Total amount payable. 
Picture 3.2: Sub program

GST helped me write a memory efficient program!
The Goods and Service Tax (GST) is a value-added tax levied on most goods and services sold for domestic consumption. The GST  shall have two components: one levied by the Centre (referred to as Central GST or CGST), and the other levied by the States (referred to as State GST or SGST). Since tax will be shared equally between the Central and State Government, I thought of using REDEFINES in COBOL for one of the tax data item. In the Picture 3.2, WS-SGST-P redefines WS-CGST-P, so it is enough to calculate the tax percentage for one data item i.e., WS-CGST-P. The other one (WS-SGST-P) will hold the same tax percentage as it shares the same memory as that of WS-CGST-P data item. 

Compile and link-edit the sub program(PGMB) as shown in Picture 3.3.
Picture 3.3: Compile and Link edit the Sub program


In COBOL's Static Call, Main program and Sub program are tied together in a same Load module:
When compiling the main program(PGMA), do a composite link by adding INCLUDE statements in the SYSIN of IEWL program, as shown in Picture 3.4. We are instructing the Link-edit program (in lines 35 thru 37) to include the load of PGMB from a private library (PRIVLIB) while creating the load module for PGMA. 
Picture 3.4: Do a Composite link of multiple COBOL programs and create an executable. 

Compile and Link-editing are done. It's time to run the main program.
Picture 3.5: Run JCL

The result, Picture 3.6. So much of tax!
Picture 3.6: Result!


Dynamic call:
  • The main program and the called program are part of different load modules i.e.sub program is not link-edited with the main program. 
  • If you make any changes to the sub program, you will only compile and link-edit the sub program. 
Notice the CALL statement in Picture 3.7, line #17. This is CALL identifier, where identifier is the data item, in this case, it is WS-PGM which contains the name of the sub program. 
Picture 3.7: Changes in the Main program for Dynamic call



Main program and sub program are compiled and link-edited separately. While compiling the main program, Compiler option DYNAM is provided in the PARM list, as shown in Picture 3.8. 
Picture 3.8: Compile Main program with compiler option as DYNAM. 

I obtained same results as shown in Picture 3.6, when calling the sub program dynamically. 

Key points to be noted: 
  • Default compiler option is NODYNAM. All calls made are static.
  • With DYNAM as compiler option, CALL literal statement calls the sub program dynamically. 
  • CALL identifier type calls are always dynamic.  

Same Stuff using Python:
I'm learning Python and I've just coded the example program in Python too. Feel free to use the interactive console on the right side of Trinket's tool to run Python. You have to call the main function and pass two arguments, Amount & GST %, as input. 
Ex: >>> main(3000,12)



Hope you found this post useful. Should you have any questions or suggestions, please share it in the Comments section below. thx.