Find out the number of parameters passed to a program

Unfortunately in COBOL there isn’t an “elegant” way to find out the number of parameters passed into a called program. Unless your COBOL compiler provides a proprietary function (I think isCOBOL has one) the only way I’m aware of is testing the address of the parameters declared in the LINKAGE SECTION.
The following two programs show how it is possible determining in a called program how many parameters are passed by the caller program.


CALLPTR is the caller program:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
      *
      * Author: Santix, http://www.italianscool.com
      *  
      * Write a text file using fopen, fwrite and fclose. 
      * 
       IDENTIFICATION DIVISION.
      *
       PROGRAM-ID. CALLPTR.
      *
       ENVIRONMENT DIVISION.  
      *
       DATA DIVISION.
      *
       WORKING-STORAGE SECTION.                                 
      *
       01 WS-PARM-COUNT         PIC S9(4) COMP.
      *
       01 WS-PARM-1             PIC X.
       01 WS-PARM-2             PIC X.
       01 WS-PARM-3             PIC X.
      *
       PROCEDURE DIVISION.
      *
       MAIN-SECTION.
      *    CREATE A FILE NAME
           CALL "RECVPTR" USING WS-PARM-COUNT
                                WS-PARM-1
                                WS-PARM-2
                                WS-PARM-3
      *
           DISPLAY "CALLPTR, WS-PARM-COUNT=" WS-PARM-COUNT
      *
           STOP RUN
      *
           .
       MAIN-EX.
           EXIT.



RECVPTR is the called program:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
      *
      * Author: Santix, http://www.italianscool.com
      *  
      * Write a text file using fopen, fwrite and fclose. 
      * 
       IDENTIFICATION DIVISION.
      *
       PROGRAM-ID. RECVPTR.
      *
       ENVIRONMENT DIVISION.  
      *
       DATA DIVISION.
      *
       WORKING-STORAGE SECTION.                                 
      *
       LINKAGE SECTION.                                           
      *
       01 WS-PARM-COUNT         PIC S9(4) COMP.
      *
       01 WS-PARM-1             PIC X.
       01 WS-PARM-2             PIC X.
       01 WS-PARM-3             PIC X.
       01 WS-PARM-4             PIC X.
       01 WS-PARM-5             PIC X.
       01 WS-PARM-6             PIC X.
       01 WS-PARM-7             PIC X.
       01 WS-PARM-8             PIC X.
       01 WS-PARM-9             PIC X.
       01 WS-PARM-10            PIC X.
      *
       PROCEDURE DIVISION USING WS-PARM-COUNT
                                WS-PARM-1
                                WS-PARM-2
                                WS-PARM-3
                                WS-PARM-4
                                WS-PARM-5
                                WS-PARM-6
                                .
      *
       MAIN-SECTION.
      *    
           COMPUTE WS-PARM-COUNT = 0
           IF ADDRESS OF WS-PARM-1 = NULL
             GOBACK
           END-IF
           ADD 1 TO WS-PARM-COUNT
           IF ADDRESS OF WS-PARM-2 = NULL
             GOBACK
           END-IF
           ADD 1 TO WS-PARM-COUNT
           IF ADDRESS OF WS-PARM-3 = NULL
             GOBACK
           END-IF
           ADD 1 TO WS-PARM-COUNT
           IF ADDRESS OF WS-PARM-4 = NULL
             GOBACK
           END-IF
           ADD 1 TO WS-PARM-COUNT
           IF ADDRESS OF WS-PARM-5 = NULL
             GOBACK
           END-IF
           ADD 1 TO WS-PARM-COUNT
           IF ADDRESS OF WS-PARM-6 = NULL
             GOBACK
           END-IF
           DISPLAY "WARNING! RECVPTR, THERE MIGHT BE MORE PARAMETERS."
           GOBACK
      *
           .
       MAIN-EX.
           EXIT.




Execution output:

CALLPTR, WS-PARM-COUNT=3

4 thoughts on “Find out the number of parameters passed to a program

  1. Metin says:

    Unfortunately, COBOL does not guarantee non-passed argument address to be NULL. So this does not work in all situations!

  2. Pratheep Vaz says:

    Hi Santix,

    I found this is not working if there are multiple calls to the same program with different number of parameters. If there is a call with 3 parameters and the subsequent call is with 2 parameters then the count is displayed as 3 in both cases.

    Please help if you have a solution for this.

    Thanks
    Pratheep

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>