Variable number of arguments to a cobol program

Is there a way to create a COBOL sub-program (procedural) that accepts a variable number of arguments similar to the "C" va_start or "Java" ... or "C#" params keyword.

 

Thanks

 

  • You can do this in a procedural COBOL program by using the OPTIONAL phrase preceding the data-name. In the corresponding calling program you could then use the OMITTED phrase in the call statement if you did not wish to pass an actual parameter.

    In a method you may specify optional default parameters for which you assign a default value. You may also pass in parameters using the PARAMS keyword as the last parameter in the list and this will group all parameters passed into a one-dimensional array and pass this into the method. This works like the C# params keyword.
  • I think what I want is what is called the "PARAMS" option

    documentation.microfocus.com/.../index.jsp


    do you know of any examples of how to do this?

    I have tried, but I get this error and I don't know what to do

    *1263-S****************************************************** **
    ** PARAMS may only be specified for a single dimensional array as the last parameter


    the item in question is defined as

    01 data-fld-nn pic x occurs 1 to 128 times
    depending on nr-of-cols.
  • I believe that PARAMS can only be specified in the procedure division header of a method and not in a procedural program but I am awaiting confirmation on this.

    The parameter array must be that of a managed type so to pass in a variable number of pic x fields you would receive this as an array of strings. 

     

           working-storage section.
    	   01 param1   pic x  value "1".
    	   01 param2   pic x  value "2".
    	   01 param3   pic x  value "3".
           procedure division.
    
    		   declare myobj = new MyClass
    		   invoke myobj::mymethod(param1, param2, param3)
    	...	   
           class-id MyClass.
           method-id mymethod.
           procedure division using params data-fld-nms as string occurs any.
               perform varying s1 as string thru data-fld-nms
    				  display s1
    		   end-perform
           		   
    
  • Are you using managed or native COBOL?
    PARAMS only applies to managed - if you want to make some arguments optional in native COBOL you can use the PARAMCOUNTCHECK directive:

    a.cbl:
    working-storage section.
    01 s1 pic x(32) value "sausages".
    01 s2 pic x(32) value "mashed potato".
    01 s3 pic x(32) value "baked beans".

    procedure division.

    call "b" using "sausages" "mash"

    call "b" using "baked beans"

    stop run.

    b.cbl:
    $set PARAMCOUNTCHECK
    linkage section.
    01 s1 pic x(32).
    01 s2 pic x(32).

    procedure division using s1 s2.
    display s1

    if address of s2 not = null
    display s2
    end-if

    goback
    .
  • Verified Answer

    This is managed JVM code.

    You can use PARAMS in the procedure division header of a procedural COBOL program but the parameters in the calling program must be passed by value.

     

           working-storage section.
    	   01 param1   pic x  value "1".
    	   01 param2   pic x  value "2".
    	   01 param3   pic x  value "3".
           procedure division.
    
    		   call "prog2" using by value param1, param2 param3
           ...
           
           program-id. prog2.
           procedure division using params myparams as string occurs any.
    
    	   perform varying s1 as string thru myparams
    	      display s1
    	   end-perform