HP OpenVMS Systems Documentation |
HP COBOL
|
Previous | Contents | Index |
Following is progcob.cob, the COBOL program that is called by the C program:
Example 12-12 COBOL Called Program "PROGCOB" (Alpha, I64) |
---|
identification division. * File progcob.cob ************************************************************** * The C program calls this COBOL program with four arguments: * arg1, arg2, arg3, arg4. * * This program performs: * arg1, myVal get the value of arg1 + arg2 + arg3 + arg4 * * When cobfunc or cobcancel is called the values in * working-storage are reset to their initial values. * * retVal: to demonstrate the value returned by this program. * myVal : to demonstrate cobcancel in the C program * arg1 : to demonstrate cobcall and cobfunc in the C program. ************************************************************** program-id. progcob. data division. working-storage section. 01 retVal pic 9(9) comp value 987654321. 01 myVal pic 9(9) comp value 0. linkage section. 01 arg1 pic 9(9) comp value 0. 01 arg2 pic 9(9) comp value 0. 01 arg3 pic 9(9) comp value 0. 01 arg4 pic 9(9) comp value 0. procedure division using arg1 arg2 arg3 arg4 giving retVal. p0. display " +------------------- From COBOL --------------------". display " | myVal = " myVal with conversion. display " | arg1 = " arg1 with conversion. display " | arg2 = " arg2 with conversion. display " | arg3 = " arg3 with conversion. display " | arg4 = " arg4 with conversion. display " | retVal = " retVal with conversion. add arg1 arg2 arg3 arg4 giving arg1 myVal. display " + After 'add arg1 arg2 arg3 arg4 giving arg1 myVal':". display " | myVal = " myVal with conversion. display " | arg1 = " arg1 with conversion. display " | arg2 = " arg2 with conversion. display " | arg3 = " arg3 with conversion. display " | arg4 = " arg4 with conversion. display " | retVal = " retVal with conversion. display " +---------------------------------------------------". |
Note that the C program progc.c does not have a function called main . The function name "main" has to be renamed, because the COBOL RTL already contains a symbol called main . To resolve this, progc.c is called from a dummy COBOL program called progmain.cob. On Tru64 UNIX, if a COBOL routine is not the main program, you need to call cob_init.
Here is progmain.cob:
identification division. * file progmain.cob program-id. progmain. procedure division. s1. call "mainx". stop run. end program progmain. |
The return value from the COBOL program is an int . Therefore, it is customary to use the int data type for the variables in C and COBOL programs that are passed back and forth. For example, retval , arg1 , arg2, arg3, and arg4 are declared as int and pic(9) in the C and COBOL programs, respectively.
Here are the commands to compile, link, and run on different platforms:
[OpenVMS] $ cobol PROGMAIN.COB, PROGCOB.COB $ cc PROGC.C $ link PROGMAIN.OBJ +PROGCOB.OBJ +PROGC.OBJ (*) $ run PROGMAIN.EXE [UNIX] % cobol progmain.cob progcob.cob progc.c (*) % a.out |
The order of listing at (*) is fundamental. Here is a sample run:
[0] All the initialized values ============================== retval = 0 arg1 = 1 +------------------- From COBOL -------------------- | myVal = 0 | arg1 = 1 | arg2 = 2 | arg3 = 3 | arg4 = 4 | retVal = 987654321 + After 'add arg1 arg2 arg3 arg4 giving arg1 myVal': | myVal = 10 | arg1 = 10 | arg2 = 2 | arg3 = 3 | arg4 = 4 | retVal = 987654321 +--------------------------------------------------- [1] After calling cobcall: ========================== retval = 987654321 arg1 = 10 +------------------- From COBOL -------------------- | myVal = 10 | arg1 = 10 | arg2 = 2 | arg3 = 3 | arg4 = 4 | retVal = 987654321 + After 'add arg1 arg2 arg3 arg4 giving arg1 myVal': | myVal = 19 | arg1 = 19 | arg2 = 2 | arg3 = 3 | arg4 = 4 | retVal = 987654321 +--------------------------------------------------- [2] After calling cobfunc: ========================== retval = 987654321 arg1 = 19 +------------------- From COBOL -------------------- | myVal = 0 | arg1 = 19 | arg2 = 2 | arg3 = 3 | arg4 = 4 | retVal = 987654321 + After 'add arg1 arg2 arg3 arg4 giving arg1 myVal': | myVal = 28 | arg1 = 28 | arg2 = 2 | arg3 = 3 | arg4 = 4 | retVal = 987654321 +--------------------------------------------------- [3] After calling cobcall again: ================================ retval = 987654321 arg1 = 28 [4] After calling cobcancel: ============================ retval = 987654321 arg1 = 28 +------------------- From COBOL -------------------- | myVal = 0 | arg1 = 28 | arg2 = 2 | arg3 = 3 | arg4 = 4 | retVal = 987654321 + After 'add arg1 arg2 arg3 arg4 giving arg1 myVal': | myVal = 37 | arg1 = 37 | arg2 = 2 | arg3 = 3 | arg4 = 4 | retVal = 987654321 +--------------------------------------------------- [5] After calling cobcall again: ================================ retval = 987654321 arg1 = 37 <> |
Because the HP COBOL compiler is part of a common language environment, an HP COBOL program can call a procedure written in another language available in this environment. This communication among high-level languages exists because these languages adhere to the HP OpenVMS Calling Standard or the Tru64 UNIX Calling Standard for Alpha Systems, as applicable, when generating a call to a procedure. Section 13.2 briefly describes the OpenVMS calling standard.
On Alpha and I64, for more information, refer to the material on
calling system routines in the OpenVMS Programming Concepts
Manual, the OpenVMS RTL Library (LIB$) Manual, and the OpenVMS System Services Reference Manual. <>
12.7.1 Calling a Fortran Program
Calling a procedure written in Fortran allows you to take advantage of features of that language. Example 12-13 demonstrates how to call a non-COBOL program in the run unit.
Example 12-13 Calling a Fortran Program from a COBOL Program |
---|
IDENTIFICATION DIVISION. PROGRAM-ID. GETROOT. **************************************************** * This program accepts a value from the terminal, * * calls the Fortran subroutine SQROOT, and passes * * the value as a character string. Program * * SQROOT returns the square root of the value. * **************************************************** DATA DIVISION. WORKING-STORAGE SECTION. 01 INPUT-NUMBER. 03 INTEGER PIC 9(5). 03 DEC-POINT PIC X(1). 03 DECIMAL PIC 9(8). 01 WORK-NUMBER. 03 INTEGER PIC 9(5). 03 DECIMAL PIC 9(8). 01 WORK-NUMBER-A REDEFINES WORK-NUMBER PIC 9(5)V9(8). 01 DISPLAY-NUMBER PIC ZZ,ZZ9.9999. PROCEDURE DIVISION. STARTER SECTION. SBEGIN. MOVE SPACES TO INPUT-NUMBER. DISPLAY "Enter number (with decimal point): " NO ADVANCING. ACCEPT INPUT-NUMBER. IF INPUT-NUMBER = SPACES GO TO ENDJOB. CALL "SQROOT" USING BY DESCRIPTOR INPUT-NUMBER. IF INPUT-NUMBER = ALL "*" DISPLAY "** INVALID ARGUMENT FOR SQUARE ROOT" ELSE DISPLAY "The square root is: " INPUT-NUMBER INSPECT INPUT-NUMBER REPLACING ALL " " BY "0" MOVE CORRESPONDING INPUT-NUMBER TO WORK-NUMBER WORK-NUMBER-A TO DISPLAY-NUMBER DISPLAY DISPLAY-NUMBER. GO TO SBEGIN. ENDJOB. STOP RUN. |
Example 12-14 shows the Fortran program SQROOT called by the program in Example 12-13 and sample output from the programs' execution.
The SQROOT subroutine accepts a 14-character string and decodes it into a real variable (DECODE is analogous to an internal READ). It then calls the SQRT function in the statement that encodes the result into the 14-character argument.
Example 12-14 Fortran Subroutine SQROOT |
---|
SUBROUTINE SQROOT(ARG) CHARACTER*14 ARG DECODE(14,10,ARG,ERR=20)VAL 10 FORMAT(F12.6) IF(VAL.LT.0.)GO TO 20 ENCODE(14,10,ARG)SQRT(VAL) 999 RETURN 20 ARG='**************' GO TO 999 END |
Previous | Next | Contents | Index |