You are on page 1of 4

Static and Dynamic Subroutine CALLs

Static CALLs In the static CALL statement, the COBOL program and all called programs are part of the same load module. When control is transferred, the called program already resides in storage, and a branch to it takes place. Subsequent executions of the CALL statement make the called program available in its last-used state, unless the called program has the INITIAL attribute. In that case, the called program and each program directly or indirectly contained within it are placed into its initial state every time the called program is called within a run unit In COBOL, you normally call a subroutine like this: CALL 'A' USING arguments The static form of the CALL statement specifies the name of the subroutine as a literal; e.g., it is in quotes. This is the static form of a subroutine call. The compiler generates object code for this which will cause the linker to copy the object module a.obj into your executable when it is linked. So, if you modify "A" and recompile it, you must also relink all of the executables that call "A", because the each of the executables contains its own copy of "A".

Dynamic CALLs

In this form of the CALL statement, the called COBOL subprogram is not link-edited with the main program, but is instead link-edited into a separate load module, and is loaded at run time only when it is required (that is, when called). Each subprogram that you call with a dynamic CALL statement can be part of a different load module that is a member of either the system link library or a private library that you supply. In either case it must be in an MVS load library; it cannot reside in the hierarchical file system. When a dynamic CALL statement calls a subprogram that is not resident in storage, the subprogram is loaded from secondary storage into the region or partition containing the main program and a branch to the subprogram is performed. The first dynamic call to a subprogram within a run unit obtains a fresh copy of the subprogram. Subsequent calls to the same subprogram (by either the original caller or any other subprogram within the same run unit) result in a branch to the same copy of the subprogram in its last-used state, provided the subprogram does not possess the INITIAL attribute.

In COBOL, the dynamic form of a subroutine call is coded like this: 01 SUBROUTINE-A PIC X(8) VALUE 'A'. CALL SUBROUTINE-A USING arguments The dynamic form of the CALL statement specifies the name of the subroutine using a variable; the variable contains the name of the subroutine to be invoked. The difference is that the name of the subroutine is found in the variable SUBROUTINE-A. The compiled code will cause the operating system to load the subroutine when it is required instead of incorporating it into the executable..

some compilers let you set options that will override the calling mechanisms shown above. Therefore, even if your program is coded to call a program statically, the compiler can convert it to the dynamic form of CALL if you set (or don't set) the correct compiler options(i.e thru DYNAM option in jcl)

Examples: static and dynamic CALL statements


This example has three parts:

Code that uses a static call to call a subprogram Code that uses a dynamic call to call the same subprogram The subprogram that is called by the two types of calls

The following example shows how you would code static calls: PROCESS NODYNAM NODLL IDENTIFICATION DIVISION. DATA DIVISION. WORKING-STORAGE SECTION. 01 RECORD-2 PIC X. 01 RECORD-1. 05 PAY PICTURE S9(5)V99. 05 HOURLY-RATE PICTURE S9V99. 05 HOURS PICTURE S99V9. . . . PROCEDURE DIVISION. CALL "SUBPROG" USING RECORD-1. CALL "PAYMASTR" USING RECORD-1 RECORD-2. STOP RUN. The following example shows how you would code dynamic calls: DATA DIVISION. WORKING-STORAGE SECTION. 77 PGM-NAME PICTURE X(8). 01 RECORD-2 PIC x. 01 RECORD-1. 05 PAY PICTURE S9(5)V99. 05 HOURLY-RATE PICTURE S9V99. 05 HOURS PICTURE S99V9. . . . PROCEDURE DIVISION. . . . MOVE "SUBPROG" TO PGM-NAME. CALL PGM-NAME USING RECORD-1. CANCEL PGM-NAME. MOVE "PAYMASTR" TO PGM-NAME. CALL PGM-NAME USING RECORD-1 RECORD-2. STOP RUN.

(6) (2)

(1) (5)

(6) (2)

(1) (4) (5)

The following example shows a called subprogram that is called by each of the two preceding calling programs: IDENTIFICATION DIVISION. PROGRAM-ID. SUBPROG. DATA DIVISION. LINKAGE SECTION. 01 PAYREC. 10 PAY PICTURE S9(5)V99. 10 HOURLY-RATE PICTURE S9V99. 10 HOURS PICTURE S99V9. 77 PAY-CODE PICTURE 9. PROCEDURE DIVISION USING PAYREC. . . . EXIT PROGRAM. ENTRY "PAYMASTR" USING PAYREC PAY-CODE. . . . GOBACK. (1) Processing begins in the calling program. When the first CALL statement is executed, control is transferred to the first statement of the PROCEDURE DIVISION in SUBPROG, which is the called program. In each of the CALL statements, the operand of the first USING option is identified as RECORD-1. (2) When SUBPROG receives control, the values within RECORD-1 are made available to SUBPROG; however, in SUBPROG they are referred to as PAYREC. The PICTURE character-strings within PAYREC and PAY-CODE contain the same number of characters as RECORD-1 and RECORD-2, although the descriptions are not identical. (3) When processing within SUBPROG reaches the EXIT PROGRAM statement, control is returned to the calling program. Processing continues in that program until the second CALL statement is issued. (4) In the example of a dynamically called program, because the second CALL statement refers to another entry point within SUBPROG, a CANCEL statement is issued before the second CALL statement. (5) With the second CALL statement in the calling program, control is again transferred to SUBPROG, but this time processing begins at the statement following the ENTRY statement in SUBPROG. (6) The values within RECORD-1 are again made available to PAYREC. In addition, the value in RECORD-2 is now made available to SUBPROG through the corresponding USING operand PAY-CODE. When control is transferred the second time from the statically linked program, SUBPROG is made available in its last-used state (that is, if any values in SUBPROG storage were changed during the first execution, those changed values are still in effect). When control is transferred

(2)

(6) (1) (3) (5) (7)

from the dynamically linked program, however, SUBPROG is made available in its initial state, because of the CANCEL statement that has been executed. (7) When processing reaches the GOBACK statement, control is returned to the calling program at the statement immediately following the second CALL statement. In any given execution of the called program and either of the two calling programs, if the values within RECORD-1 are changed between the time of the first CALL and the second, the values passed at the time of the second CALL statement will be the changed, not the original, values. If you want to use the original values, you must save them.

Performance considerations of static and dynamic calls


If storage is of main concern, then we can go for "Dynamic call" as module will be loaded to main memory only when it is called/needed. If speed is of main concern, then we can go for "Static Call" as all modules will be link-edited together into the calling module during compilation process only. Which one is better under what circumstances ? 1) Suppose I have a main program which calls 100 other programs (each one only once) which one would be better i.e Static or Dynamic Call ? 2) Similarly if I have a main program which calls a single program 100 times then in that case which one would be better i.e Static or Dynamic Call ? In the first case since the number of called program is too high so it will be better to go for dynamic call, as if you go for static call and if any one of the 100 programs get changed then u need to compile all the 100 programs in correct sequence In second case , it is better to go for static call, as main program is calling 100 times the subroutine program. As in dynamic call, control need to passed to different object module, so it will take more time, where as in static call there will be only one load module, so time consumption will be less in that case

Making both static and dynamic calls


You can specify both static and dynamic CALL statements in the same program if you compile the program with the NODYNAM compiler option. In this case, with the CALL literal statement the called subprogram will be link-edited with the main program into one load module. The CALL identifier statement results in the dynamic invocation of a separate load module. When a dynamic CALL statement and a static CALL statement to the same subprogram are issued within one program, a second copy of the subprogram is loaded into storage. Because this arrangement does not guarantee that the subprogram will be left in its last-used state, results can be unpredictable

You might also like