Highlighted
New Member.
1201 views

Variable number of arguments to a cobol program

Jump to solution

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

 

0 Likes
1 Solution

Accepted Solutions
Highlighted
Micro Focus Expert
Micro Focus Expert

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

View solution in original post

0 Likes
5 Replies
Highlighted
Micro Focus Expert
Micro Focus Expert
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.
0 Likes
Highlighted
New Member.
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.
0 Likes
Highlighted
Micro Focus Expert
Micro Focus Expert

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
       		   
0 Likes
Highlighted
Absent Member.
Absent Member.
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
.
0 Likes
Highlighted
Micro Focus Expert
Micro Focus Expert

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

View solution in original post

0 Likes
The opinions expressed above are the personal opinions of the authors, not of Micro Focus. By using this site, you accept the Terms of Use and Rules of Participation. Certain versions of content ("Material") accessible here may contain branding from Hewlett-Packard Company (now HP Inc.) and Hewlett Packard Enterprise Company. As of September 1, 2017, the Material is now offered by Micro Focus, a separately owned and operated company. Any reference to the HP and Hewlett Packard Enterprise/HPE marks is historical in nature, and the HP and Hewlett Packard Enterprise/HPE marks are the property of their respective owners.