sams_teach_yourself_cobol_in_24_hours_-_hour_23_the_call_interface

Sams Teach Yourself COBOL in 24 Hours - Hour 23 The Call Interface

Return to Teach Yourself COBOL in 24 Hours, COBOL bibliography, COBOL, COBOL DevOps, Awesome COBOL, Awesome IBM Mainframe, IBM Mainframe development, IBM Mainframe bibliography, Fortran

“ (TYCb24H 1998)

Part V

Advanced Topics

Hour

23 The Call Interface

24 The Graphical User Interface

Hour 23 The Call Interface

COBOL programs may execute other COBOL programs or even programs written in a different source language. The COBOL standard defines a simple method for accomplishing this task: the Call statement. In this hour, you learn how to write a program that calls another program and how to write the program that is called. You learn the information necessary to successfully interface and communicate between calling and called programs, such as

• How to Call another program

• Passing data to a called program from a calling program By Reference and By Content

• Coding the Linkage Section and the Procedure Division to allow a program to be called

• Static versus dynamic calls

• The importance of synchronizing the calling parameters and the Linkage Section, and how to use Copybooks to accomplish this task

Calling Other Programs One important necessity of computer programming is the ability to reuse programming logic. If you have a really neat date-validation routine, you don’t want to have to cut the paragraph out of one program and paste it into another. The data items used by the routine might have names that conflict with the data items in the new program. You have to remember to copy not only the routine but also the logic that performs it and the Working-Storage items used by the routine.

Instead of doing all of this work, COBOL allows you to Call other programs, passing and returning data values in the process. A Call is similar to a Perform. The called program is executed and then control returns to the calling program immediately after the Call statement.

The simplest form of calling a program involves the execution of another program without any program-to-program communication. An example of this approach is a menu program.

Image

A menu is a screen or window that is displayed with a list of items from which the user may choose. A menu program typically performs little function and is used merely to allow the user to choose an option to be executed. Normally, making a menu choice causes the menu program to call a program that performs the associated function.

A menu program can be the control center for your application. A normal system is made up of related programs. A menu allows the user to choose the desired function from a list.

Simple Program Calling The menu program being considered is the calling program. The calling program is simply the program that issues the Call statement, causing another program to be executed. No special setup is required for a calling program in general. With Fujitsu COBOL, the very first program that issues a Call must be compiled as a Main program, just as all the programs and examples have been so far.

The menu program in Listing 23.1 calls two of the examples from previous hours. The programs being called require a few simple changes, so they are given new names for this example. The first program being called is the telephone-number-formatting program from Hour 8, “Conditional Statements,” Chapt08a, which appeared in Listing 8.1. It has been renamed Chapt23b. The second program being called is the days-between-dates program from Hour 21, “Date Manipulation,” Chapt21c, which appeared in Listing 21.3. It has been renamed Chapt23c.

Listing 23.1 Menu Program

000001 @OPTIONS MAIN,TEST 000002 Identification Division. 000003 Program-Id. Chapt23a. 000004 Environment Division. 000005 Configuration Section. 000006 Source-Computer. IBM-PC. 000007 Object-Computer. IBM-PC. 000008 Special-Names. 000009 Crt Status Is Keyboard-Status. 000010 Data Division. 000011 Working-Storage Section. 000012 01 Dummy-Field Pic X Value Spaces. 000013 01 Keyboard-Status. 000014 03 Accept-Status Pic 9. 000015 03 Function-Key Pic X. 000016 88 F1-Pressed Value X“01”. 000017 88 F2-Pressed Value X“02”. 000018 88 F3-Pressed Value X“03”. 000019 03 System-Use Pic X. 000020 01 Done-Flag Pic X Value Spaces. 000021 88 All-Done Value “Y”. 000022 Screen Section. 000023 01 Main-Screen 000024 Blank Screen, Auto, Required, 000025 Foreground-Color Is 7, 000026 Background-Color Is 1. 000027 03 Line 1 Column 29 Value “Program Selection Menu”. 000028 03 Line 3 Column 1 Value “F1 Telephone Number Format”. 000029 03 Line 5 Column 1 Value “F2 Days Between Dates”. 000030 03 Line 7 Column 1 Value “F3 Exit”. 000031 03 Line 9 Column 1 Pic X To Dummy-Field Secure. 000032 Procedure Division. 000033 Chapt023a-Start. 000034 Perform Until All-Done 000035 Display Main-Screen 000036 Accept Main-Screen 000037 Evaluate True 000038 When F1-Pressed 000039 Call “Chapt23b” 000040 When F2-Pressed 000041 Call “Chapt23c” 000042 When F3-Pressed 000043 Set All-Done To True 000044 When Other 000045 Continue 000046 End-Evaluate 000047 End-Perform 000048 Stop Run 000049 . Notice the definition of Dummy-Field. The program must have some field to use in conjunction with the Accept statement. Dummy-Field is defined not to actually collect any user information, but simply as a field to Accept so the function key pressed may be captured.

The Call statements that cause the other programs to be executed are in lines 39 and 41. These called programs are often referred to as subprograms because they are called from a Main program.

The Call statement demonstrated here is the simplest form of the statement. It causes the subprogram specified to be executed. When the subprogram finishes its processing, control returns to the calling program at the statement immediately following the Call.

The subprograms require some special setup also. First, copy the original programs to the new program names. Then you need to make some minor modifications. Remove the @OPTIONS line at the top of the subprograms. You do not want the subprograms to be compiled as Main programs as the directive specifies. Omitting the MAIN causes the programs to be compiled as subprograms.

A new statement is required to return from the called program to the calling program. Replace the Stop Run in the programs with Exit Program. Exit Program causes control to return immediately to the calling program. Any files that are open in the subprogram are automatically closed as if a Close statement were executed. The only difference is that no Declaratives are processed, even if they are coded. Remember also, when changing the programs, to change the Program-Id to reflect the new names. After making the necessary changes, the two subprograms should appear as shown in Listings 23.2 and 23.3.

Listing 23.2 Called Phone Number Format Program

000001 Identification Division. 000002 Program-Id. Chapt23b. 000003* Intelligent Telephone Number Format 000004 Environment Division. 000005 Configuration Section. 000006 Source-Computer. IBM-PC. 000007 Object-Computer. IBM-PC. 000008 Data Division. 000009 Working-Storage Section. 000010 01 Phone-Number Pic 9(10) Value Zeros. 000011 01 Formatted-Number Pic X(14) Value “(XXX) XXX-XXXX”. 000012 01 Formatted-Alternate Pic X(8) Value “XXX-XXXX”. 000013 01 The-Edited-Number Pic X(14) Value Spaces. 000014 Screen Section. 000015 01 Phone-Entry Blank Screen. 000016 03 Line 01 Column 01 Value “ Enter Phone Number: “. 000017 03 Line 01 Column 22 Pic Z(10) Using Phone-Number. 000018 03 Line 03 Column 01 Value “Edited Phone Number: “. 000019 03 Line 03 Column 22 Pic X(14) From The-Edited-Number 000020 Procedure Division. 000021 Chapt23b-Start. 000022 Display Phone-Entry 000023 Accept Phone-Entry 000024 If Phone-Number > 9999999 000025* Number Large Enough To Contain Area Code 000026 Inspect Formatted-Number 000027 Replacing First “XXX” By Phone-Number (1:3) 000028 First “XXX” By Phone-Number (4:3) 000029 First “XXXX” By Phone-Number (7:4) 000030 Move Formatted-Number To The-Edited-Number 000031 Else 000032* Number Not Large Enough To Contain An Area Code 000033 Inspect Formatted-Alternate 000034 Replacing First “XXX” By Phone-Number (4:3) 000035 First “XXXX” By Phone-Number (7:4) 000036 Move Formatted-Alternate To The-Edited-Number 000037 End-If 000038 Display Phone-Entry 000039 Accept Phone-Entry 000040 Exit Program 000041 . Image

In addition to replacing Stop Run with Exit Program, an Accept is added in both programs before the Exit Program statement. If you fail to add this Accept, the program will run, but then return directly to the menu program without pausing to display its output.

Listing 23.3 Called Days Between Dates Program

000001 Identification Division. 000002 Program-Id. Chapt23c. 000003* Days Between Dates 000004 Environment Division. 000005 Configuration Section. 000006 Source-Computer. IBM-PC. 000007 Object-Computer. IBM-PC. 000008 Data Division. 000009 Working-Storage Section. 000010 01 First-Date Value Zeros. 000011 03 Date-MM Pic 99. 000012 03 Date-DD Pic 99. 000013 03 Date-YYYY Pic 9(4). 000014 01 Second-Date Value Zeros. 000015 03 Date-MM Pic 99. 000016 03 Date-DD Pic 99. 000017 03 Date-YYYY Pic 9(4). 000018 01 Days-Between Pic S9(12) Value Zeros. 000019 01 Integer-First-Date Pic 9(12). 000020 01 Integer-Second-Date Pic 9(12). 000021 01 Date-Formatting-Items. 000022 03 YYYYMMDD-Format-Date. 000023 05 Date-YYYY Pic 9(4). 000024 05 Date-MM Pic 99. 000025 05 Date-DD Pic 99. 000026 03 YYYYMMDD-Format-Date-N Redefines 000027 YYYYMMDD-Format-Date Pic 9(8). 000028 03 Format-Indicator-F Pic X(8) Value “MMDDYYYY”. 000029 03 Format-Indicator-S Pic X(8) Value “MMDDYYYY”. 000030 Screen Section. 000031 01 Date-Entry Blank Screen Auto. 000032 03 Line 01 Column 01 Value “Enter First Date: “. 000033 03 Line 01 Column 21 Pic X(8) From Format-Indicator-F 000034 To First-Date. 000035 03 Line 03 Column 01 Value “Enter Second Date: “. 000036 03 Line 03 Column 21 Pic X(8) From Format-Indicator-S 000037 To Second-Date. 000038 03 Line 05 Column 01 Value “Days between dates: “. 000039 03 Line 05 Column 21 Pic -Zzz,ZZ9 From Days-Between. 000040 Procedure Division. 000041 Chapt23c-Start. 000042 Display Date-Entry 000043 Accept Date-Entry 000044 Move Corresponding First-Date To YYYYMMDD-Format-Date 000045 Compute Integer-First-Date = 000046 Function Integer-Of-Date (YYYYMMDD-Format-Date-N) 000047 Move First-Date To Format-Indicator-F 000048 Move Corresponding Second-Date To YYYYMMDD-Format-Date 000049 Compute Integer-Second-Date = 000050 Function Integer-Of-Date (YYYYMMDD-Format-Date-N) 000051 Move Second-Date To Format-Indicator-S 000052 Compute Days-Between = Integer-Second-Date - 000053 Integer-First-Date 000054 Display Date-Entry 000055 Accept Date-Entry 000056 Exit Program 000057 . After you alter the programs and have created Chapt23a.Cob (refer to Listing 23.1), you need to compile the programs. The process for compiling and linking a main program that calls subprograms can be complex. Fujitsu provides a simple method—called a Project—to accomplish the task. Follow these steps to create your Project file and compile the programs.

1. Start Programming Staff.

2. Choose the Project Menu option and then click Open.

3. Next to File Name, type \Tycobol\Chapt23a.Prj.

4. Click the Open button. (Under Windows 3.1, click OK.)

5. Click the Yes button when asked whether you want to create the file.

6. A Target Files dialog box is displayed. Click the Add button to add Chapt23a.Exe to the Project.

7. Click the OK button to accept the Project.

The next few steps select the source files that make up the project. These are the dependencies.

8. Under Dependent Files, type \Tycobol\Chapt23a.Cob and click the Add button.

9. The file is shown in the box under Dependent Files. Highlight the program by clicking on it. You need to specify that this file is the Main program. After selecting the program, click the Main Program button. The box to the left of the program name turns from white to red.

10. Change the filename in the Dependent Files field to \Tycobol\Chapt23b.cob and click the Add button.

11. Do the same for Chapt23c.

12. Click the OK button to accept the dependent files. A window appears onscreen with a title of Chapt23a.Prj, and several icons appear in the window. (See Figure 23.1.) Click the Build button to compile and link all the files in the project.

13. A message indicates that the Make has ended. Close that window. (Click OK under Windows 3.1.)

14. If your program has any compile errors, you must fix them and then click the Build button to compile and link the programs again. If the compile is clean, close the Edit window. (Under Windows 3.1, a clean compile does not show the Edit window.)

15. You are now ready to run the program. Click the Execute button to run the program. (Under Windows 3.1, the Execute button starts the debugger, so instead select the Utilities menu, choose Winexec, and type Chapt23a.Exe).

Figure 23.1 The Project window.

Image

Run the program. Try the different menu options. Notice that if you select a program from the menu more than once, your last input is displayed and the program does not start in a fresh state. The telephone number reformat program does not function properly, because Working-Storage is left intact between calls of the subprograms. This condition can cause problems in many programs, especially if any Value clause items that you are counting on for proper program function are specified in Working-Storage.

A simple method to correct this problem is to code the clause Is Initial after the name of the program on the Program-Id line of the called program. Is Initial causes any Working-Storage items with a Value clause to be reinitialized to that value when the program is called. The utility provided by the ability to code Is Initial is one reason that you should always code a Value clause on Working-Storage items. Add the Is Initial clause after the name of the program on the Program-Id line of Chapt23b and Chapt23c.

000002 Program-Id. Chapt23b Is Initial. Then rebuild your project and try the programs again. Notice that they operate properly.

Image

To rebuild a project after you have closed the project, select the Project menu item from Programming Staff. Then choose Open and open the desired project. Then you can use the previously explained procedure to build and rebuild the open project. Rebuilding forces a recompile on all programs. Build recompiles only those programs that changed since the last build.

Passing Data Between Programs In addition to simply calling a subprogram, you can pass data to and from the subprogram. A good example of this technique appears in the date-validation program coded in Hour 21. This program is an ideal candidate for a called subprogram. You can pass the date to be validated and a status flag that the called program can set to indicate the validity of the date.

When passing data to a subprogram, the Call statement is altered slightly with the addition of the Using clause. The various Call parameters are specified after the Using clause. COBOL passes the memory address of these data items to the subprogram, which then has access to those data items. The Call parameters may be any literals or data items. The program in Listing 23.4 accepts a date and then calls a date-validation program based on the validation program coded in Hour 21 to validate the date.

Listing 23.4 Date Entry, Calling Validation Program

000001 @OPTIONS MAIN,TEST 000002 Identification Division. 000003 Program-Id. Chapt23d. 000004* Enter A Date For Validation 000005 Environment Division. 000006 Configuration Section. 000007 Source-Computer. IBM-PC. 000008 Object-Computer. IBM-PC. 000009 Data Division. 000010 Working-Storage Section. 000011 01 Passed-Date. 000012 03 Date-To-Validate Pic 9(8). 000013 03 Date-To-Validate-X Redefines Date-To-Validate. 000014 05 Date-MM Pic 99. 000015 05 Date-DD Pic 99. 000016 05 Date-YYYY Pic 9(4). 000017 01 Valid-Status Pic X(40) Value Spaces. 000018 Screen Section. 000019 01 Date-Entry Blank Screen Auto. 000020 03 Line 01 Column 01 Value “Enter Date:”. 000021 03 Line 01 Column 13 Pic 99/99/9999 Using Date-To-Validate. 000022 03 Line 01 Column 24 Pic X(40) From Valid-Status. 000023 Procedure Division. 000024 Chapt23d-Start. 000025 Initialize Date-To-Validate 000026 Display Date-Entry 000027 Accept Date-Entry 000028 Call “Chapt23e” Using Passed-Date Valid-Status 000029 Display Date-Entry 000030 . Notice the addition in line 28 of the Using clause with two Call parameters being passed to the called program, Chapt23e (see Listing 23.5). Nothing else is necessary for a calling program to pass data to a called program.

The Linkage Section The called program must have some way to find the data being passed by the calling program. Remember that the data itself is not passed, but the location or address in memory of that data is. The data is located in the called program by using what is known as the Linkage Section. The Linkage Section appears immediately before the Procedure Division of the called program. Under the Linkage Section, the data description of the same information that is passed is coded. Each item passed, however, must have a Group Level definition in the Linkage Section. Each item must match exactly what is passed in the Call statement of the calling program. The simplest way to meet this requirement is to ensure that the items defined in Working-Storage of the calling program and passed to the called program are used exactly the same way in the Linkage Section of the called program. The Linkage Section of Chapt23e (Listing 23.5) is coded as follows:

000023 Linkage Section. 000024 01 Passed-Date. 000025 03 Date-To-Validate Pic 9(8). 000026 03 Date-To-Validate-X Redefines Date-To-Validate. 000027 05 Date-MM Pic 99. 000028 05 Date-DD Pic 99. 000029 05 Date-YYYY Pic 9(4). 000030 01 Valid-Status Pic X(40).

The Procedure Division of the Called Program In addition to the Linkage Section, the called program must identify the data items being passed to it on the Procedure Division line. This step is accomplished in a method that is very similar to the Call statement in the calling program. The Procedure Division is coded with a Using clause, which references the Call parameters as named in the Linkage Section. This combination of Linkage Section and Procedure Division setup allows the called program to reference the passed data in its exact memory location. The called program may modify this data, and when control is passed back to the calling program, the modified data will be available. The Procedure Division line for Chapt23e (Listing 23.5) is coded as follows:

Procedure Division Using Passed-Date Valid-Status. The full version of Chapt23e, which validates the passed date appears in Listing 23.5.

Listing 23.5 Date Validation Subprogram

000001 Identification Division. 000002 Program-Id. Chapt23e Is Initial. 000003* Validate A Date 000004 Environment Division. 000005 Configuration Section. 000006 Source-Computer. IBM-PC. 000007 Object-Computer. IBM-PC. 000008 Data Division. 000009 Working-Storage Section. 000010 01 Work-Number Pic 9(5) Value Zeros. 000011 01 Work-Remainder Pic 9(5) Value Zeros. 000012 01 Work-Remainder-100 Pic 9(5) Value Zeros. 000013 01 Work-Remainder-400 Pic 9(5) Value Zeros. 000014 01 Today-Date Pic 9(8) Value Zeros. 000015 01 Today-Integer Pic 9(7) Value Zeros. 000016 01 Test-Integer Pic 9(7) Value Zeros. 000017 01 Test-Range Pic 9(7) Value Zeros. 000018 01 Day-Table-Area. 000019 03 Day-Table-Values Pic X(24) Value 000020 “312831303130313130313031”. 000021 03 Day-Table Redefines Day-Table-Values. 000022 05 Days-In-Month Pic 99 Occurs 12 Times 000023 Linkage Section. 000024 01 Passed-Date. 000025 03 Date-To-Validate Pic 9(8). 000026 03 Date-To-Validate-X Redefines Date-To-Validate 000027 05 Date-MM Pic 99. 000028 05 Date-DD Pic 99. 000029 05 Date-YYYY Pic 9(4). 000030 01 Valid-Status Pic X(40). 000031 Procedure Division Using Passed-Date Valid-Status. 000032 Chapt23e-Start. 000033 Divide Date-YYYY Of Date-To-Validate-X By 4 000034 Giving Work-Number Remainder 000035 Work-Remainder 000036 Divide Date-YYYY Of Date-To-Validate-X By 100 000037 Giving Work-Number Remainder 000038 Work-Remainder-100 000039 Divide Date-YYYY Of Date-To-Validate-X By 400 000040 Giving Work-Number Remainder 000041 Work-Remainder-400 000042 If Work-Remainder = Zeros And 000043 (Work-Remainder-100 Not = Zeros Or 000044 Work-Remainder-400 = Zeros) 000045 Move 29 To Days-In-Month (2) 000046 Else 000047 Move 28 To Days-In-Month (2) 000048 End-If 000049 If Date-MM Of Date-To-Validate-X > 12 Or 000050 Date-MM Of Date-To-Validate-X < 01 Or 000051 Date-YYYY Of Date-To-Validate-X < 1601 Or 000052 Date-DD Of Date-To-Validate-X Not > Zero Or 000053 Date-DD Of Date-To-Validate-X > 000054 Days-In-Month (Date-MM Of Date-To-Validate-X) 000055 Move “Invalid Date” To Valid-Status 000056 Else 000057 Move “Valid Date” To Valid-Status 000058 End-If 000059 Exit Program 000060 . The program is coded with the Is Initial clause on the Program-Id line. This clause allows you to use the routine multiple times, without worrying about leftover values in the Working-Storage fields.

Create a new Project named Chapt23d.Prj. Use Chapt23d.cob as your Main program and Chapt23e.cob as the subprogram. Build the project and run the program. Notice how the date-validation functions seamlessly?

Image

The COBOL standard provides no method for a COBOL program to Call itself or another program that calls a program that in turn issues a Call to the original program. This type of operation is defined as recursion. Standard COBOL does not support recursion; however, many COBOL vendors provide a method for accomplishing recursion. Check your COBOL documentation if you want to use recursion.

Call By Reference and By Content The method of calling demonstrated thus far, where data values may be changed in the called program, is referred to as calling By Reference. When a parameter is passed By Reference, its address is passed to the called program. Another option is to call the subprogram specifying By Content before the data item being passed. You may mix By Reference and By Content items in the same Call statement. Calling By Content causes the program to copy the data being passed to a temporary area, passing the address of that temporary area to the called program instead of the address of the actual data item. This method allows the called program to modify this data, but upon return to the calling program, the original data is left intact, thus protecting it.

Modify Chapt23d.Cob (refer to Listing 23.4) to call the date-validation program By Content. The Call statement becomes

000028 Call “Chapt23e” Using By Content Passed-Date Valid-Status Rebuild the project and run the program. Notice that the message about the validity of the date is not returned! This is because By Content is specified. If you need a value returned, you must always issue the call using By Reference or by not specifying By Content or By Reference and thus defaulting to By Reference.

Under certain circumstances, such as calling a program that needs to manipulate the input field as part of the validation process, you should specify By Content. This step allows the called program to manipulate the passed data as necessary without disturbing the original data. In this case, you still need to be able to return a value. You now have Call parameters that require different methods of being passed—some By Reference and some By Content. Modify the Call statement in Chapt23d.Cob (Listing 23.4) once again, this time adding By Reference before the Valid-Status data item.

000028 Call “Chapt23e” Using By Content Passed-Date 000029 By Reference Valid-Status Rebuild the project and run the program again. Notice that it has started working properly again. The Valid-Status field is being properly passed both to and from the called program.

Dynamic Versus Static Calls The programs called so far in these examples have been static calls. These programs are actually linked into and become part of the program that issues the Call. If the subprogram is changed, then the calling program must be recompiled or at least relinked so that the new called program can be linked with the calling program.

Most compilers also support a feature called dynamic calls. Dynamically called programs are loaded into memory when the Call is issued. Therefore, these programs can be changed and recompiled independently of the calling program. The use of dynamic calls is specified mainly by the method in which the programs are linked.

Image

When using dynamic calls, you can very easily change the name of the program being called. Instead of coding the Call statement using a literal for the program to be called, refer to a data item defined in Working-Storage. For example, you can define an item in Working-Storage as 01 Program-To-Call Pic X(8) Value “CHAPT23E”. Coding Call Program-To-Call, issues a dynamic call for CHAPT23E. To call a different program, simply move its name into the Program-To-Call field and issue the Call.

Dynamically called programs can be removed from memory and reinitialized upon the next Call by coding a Cancel statement. The Cancel statement is followed by the name of the program being canceled or the data field containing the name of the program to be canceled. For example:

000103 Cancel Program-To-Call When a Cancel statement is encountered, all files opened by the program are closed as if a Close statement were issued for each one. No Declaratives that might be coded for the file are performed with this implied Close.

If for some reason the Call is not successful, either because of a memory problem or because the called program is not found, an exception occurs. You may capture this exception by coding the On Exception clause with the Call statement. Similarly, the Not On Exception clause is also supported. If you choose to use On Exception or Not On Exception, I suggest that you use the End-Call explicit scope terminator.

000104 Call Program-To-Call 000105 On Exception Display “The Call Failed” 000106 Not On Exception Display “The Call was Successful” 000107 End-Call Copy the programs Chapt23d.Cob (refer to Listing 23.4) and Chapt23e.Cob (refer to Listing 23.5). Change their names to Chapt23f and Chapt23g. Modify Chapt23f to Call Chapt23g with a dynamic call. The modified program follows.

000001 @OPTIONS MAIN,TEST 000002 Identification Division. 000003 Program-Id. Chapt23f. 000004* Enter a date for Validation - Dynamic Call 000005 Environment Division. 000006 Configuration Section. 000007 Source-Computer. IBM-PC. 000008 Object-Computer. IBM-PC. 000009 Data Division. 000010 Working-Storage Section. 000011 01 Passed-Date. 000012 03 Date-To-Validate Pic 9(8). 000013 03 Date-To-Validate-X redefines Date-To-Validate. 000014 05 Date-MM Pic 99. 000015 05 Date-DD Pic 99. 000016 05 Date-YYYY Pic 9(4). 000017 01 Valid-Status Pic X(40) Value Spaces. 000018 01 Program-To-Call Pic X(8) Value “CHAPT23G”. 000019 Screen Section. 000020 01 Date-Entry Blank Screen Auto. 000021 03 Line 01 Column 01 Value “Enter Date:”. 000022 03 Line 01 Column 13 Pic 99/99/9999 Using Date-To-Validate 000023 03 Line 01 Column 24 Pic X(40) From Valid-Status. 000024 Procedure Division. 000025 Chapt23f-Start. 000026 Initialize Date-To-Validate 000027 Display Date-Entry 000028 Accept Date-Entry 000029 Call Program-To-Call Using Passed-Date Valid-Status 000030 Display Date-Entry 000031 . Fujitsu COBOL handles dynamic calls by creating DLL files, or dynamic link libraries, for the programs being called. To do so, some new options must be set within the project file.

Image

The calling and called Program-Id are critical. This name is contained internally within the DLL that is the called program. Fujitsu COBOL creates this program name in all uppercase when the DLL is compiled and linked. If you are issuing a dynamic Call and you do not specify the program name being called in all uppercase, an error message tells you that the program is unable to make the Call. You can correct this problem by using all upper-case letters when you code the called program name in the calling program.

Use the previously discussed steps to create a new project for Chapt23f and then do the following:

1. Add Chapt23f.Exe.

2. Before proceeding to the Dependencies selection, change the filename displayed from Chapt23f.Exe to Chapt23g.Dll and click Add.

3. Click OK to proceed to the Dependencies selection. The first target is Chapt23f.Exe. Add Chapt23f.Cob as a dependent file.

4. Select the program and click the Main button to make it a main program.

5. Click the field down arrow in the target field and select Chapt23g.Dll.

6. Under the Dependent File, type the name Chapt23g.Cob and then click Add.

7. Click OK to proceed to the Project Manager. Click Build to compile and link the programs that are part of the project.

Image

If you fail to change the Program-Id in Chapt23g.Cob from Chapt23e to Chapt23g, the build issues an Unresolved External message. In this case, the build routine cannot find the program that you are attempting to Call. The Program-Id is very important.

Using Copybooks Another common problem relating to calling subprograms is failure to ensure that the parameters specified for the Call in the calling program match the parameters coded in the Linkage Section of the called program.

You can alleviate this problem by ensuring that source code that is included in the calling program is exactly the same as that coded in the Linkage Section of the called program. COBOL provides a simple method of handling this situation. It uses the Copy statement.

The Copy statement simply inserts another file containing source code into your program. When compiled, the compiler assembles the full program by expanding the copy members into the source of the program. These copy members are referred to as Copybooks. The following Copybook (see Listing 23.6) and modification of the called program, Chapt23g, (see Listing 23.7) illustrate the concept.

Listing 23.6 Dateval.Cpy, Date Validation Copybook

000001 01 Passed-Date. 000002 03 Date-To-Validate Pic 9(8). 000003 03 Date-To-Validate-X Redefines Date-To-Validate. 000004 05 Date-MM Pic 99. 000005 05 Date-DD Pic 99. 000006 05 Date-YYYY Pic 9(4). 000007 01 Valid-Status Pic X(40).

Listing 23.7 Date Validation Program Using Dateval.Cpy Copybook

000001 Identification Division. 000002 Program-Id. Chapt23h. 000003* Validate A Date 000004 Environment Division. 000005 Configuration Section. 000006 Source-Computer. IBM-PC. 000007 Object-Computer. IBM-PC. 000008 Data Division. 000009 Working-Storage Section. 000010 01 Work-Number Pic 9(5) Value Zeros. 000011 01 Work-Remainder Pic 9(5) Value Zeros. 000012 01 Work-Remainder-100 Pic 9(5) Value Zeros. 000013 01 Work-Remainder-400 Pic 9(5) Value Zeros. 000014 01 Today-Date Pic 9(8) Value Zeros. 000015 01 Today-Integer Pic 9(7) Value Zeros. 000016 01 Test-Integer Pic 9(7) Value Zeros. 000017 01 Test-Range Pic 9(7) Value Zeros. 000018 01 Day-Table-Area. 000019 03 Day-Table-Values Pic X(24) Value 000020 “312831303130313130313031”. 000021 03 Day-Table Redefines Day-Table-Values. 000022 05 Days-In-Month Pic 99 Occurs 12 Times. 000023 Linkage Section. 000024 Copy “Dateval.Cpy”. 000025 Procedure Division Using Passed-Date Valid-Status. 000026 Chapt23h-Start. 000027 Divide Date-YYYY Of Date-To-Validate-X By 4 000028 Giving Work-Number Remainder 000029 Work-Remainder 000030 Divide Date-YYYY Of Date-To-Validate-X By 100 000031 Giving Work-Number Remainder 000032 Work-Remainder-100 000033 Divide Date-YYYY Of Date-To-Validate-X By 400 000034 Giving Work-Number Remainder 000035 Work-Remainder-400 000036 If Work-Remainder = Zeros And 000037 (Work-Remainder-100 Not = Zeros Or 000038 Work-Remainder-400 = Zeros) 000039 Move 29 To Days-In-Month (2) 000040 Else 000041 Move 28 To Days-In-Month (2) 000042 End-If 000043 If Date-MM Of Date-To-Validate-X > 12 Or 000044 Date-MM Of Date-To-Validate-X < 01 Or 000045 Date-YYYY Of Date-To-Validate-X < 1601 Or 000046 Date-DD Of Date-To-Validate-X Not > Zero Or 000047 Date-DD Of Date-To-Validate-X > 000048 Days-In-Month (Date-MM Of Date-To-Validate-X) 000049 Move “Invalid Date” To Valid-Status 000050 Else 000051 Move “Valid Date” To Valid-Status 000052 End-If 000053 Exit Program 000054 . The use of the Copy statement is not limited to the Linkage Section. You can use the Copy statement anywhere in a program except within Copybooks.

Summary In this hour, you learned the following:

• The Call statement executes other programs from within your program.

• The term that describes these called programs is subprogram.

• The Is Initial clause of the Program Id reinitializes any Working-Storage entries with a Value clause every time the subprogram is called.

• Programs can be called statically or dynamically.

• Dynamically called programs may be canceled, thus unloading them from memory and resetting their values for the next Call. The Cancel statement accomplishes this job.

• The On Exception clause captures errors that occur while making a Call.

• You can use the Copy statement to include other files containing source code within your COBOL programs.

Q&A Q Can the Call statement be used to Call programs written in a language other than COBOL?

A Yes. The various COBOL vendors might use a different syntax for the Call statement to accomplish the task. Check your compiler documentation to be sure.

Q What is one advantage of using a dynamic Call over a static Call?

A If you need to change the called program, you can and you don’t have to recompile the calling program.

Q Why would I want to ever Cancel a called program?

A One reason is good housekeeping. At the end of your program, you should Cancel any dynamically called programs. Another reason to Cancel a program is to initialize its Working-Storage to a fresh state upon the next Call of the program. Remember that the Cancel statement closes any open files that the called program was using.

Q Can I use the Copy statement to include FD information for a file?

A Yes. This popular use of the Copy statement ensures consistency of file definitions.

Workshop To help reinforce your understanding of the material presented in this hour, refer to the section “Quiz and Exercise Questions and Answers” that can be found on the CD. This section contains quiz questions and exercises for you to complete, as well as the corresponding answers.

Fair Use Sources

COBOL: COBOL Fundamentals, COBOL Inventor - COBOL Language Designer: 1959 by Howard Bromberg, Norman Discount, Vernon Reeves, Jean E. Sammet, William Selden, Gertrude Tierney, with indirect influence from Grace Hopper, CODASYL, ANSI COBOL, ISO/IEC COBOL; Modern COBOL - Legacy COBOL, IBM COBOL, COBOL keywords, COBOL data structures - COBOL algorithms, COBOL syntax, Visual COBOL, COBOL on Windows, COBOL on Linux, COBOL on UNIX, COBOL on macOS, Mainframe COBOL, IBM i COBOL, IBM Mainframe DevOps, COBOL Standards, COBOL Paradigms (Imperative COBOL, Procedural COBOL, Object-Oriented COBOL - COBOL OOP, Functional COBOL), COBOL syntax, COBOL installation, COBOL containerization, COBOL configuration, COBOL compilers, COBOL IDEs, COBOL development tools, COBOL DevOps - COBOL SRE, COBOL data science - COBOL DataOps, COBOL machine learning, COBOL deep learning, COBOL concurrency, COBOL history, COBOL bibliography, COBOL glossary, COBOL topics, COBOL courses, COBOL Standard Library, COBOL libraries, COBOL frameworks, COBOL research, Grace Hopper, COBOL GitHub, Written in COBOL, COBOL popularity, COBOL Awesome list, COBOL Versions. (navbar_cobol)


© 1994 - 2024 Cloud Monk Losang Jinpa or Fair Use. Disclaimers

SYI LU SENG E MU CHYWE YE. NAN. WEI LA YE. WEI LA YE. SA WA HE.


sams_teach_yourself_cobol_in_24_hours_-_hour_23_the_call_interface.txt · Last modified: 2024/04/28 03:37 (external edit)