Parameters are values or variable references that are passed to a method. They can be specified in the method header or in the procedure division header. If they are specified in the method header, you must omit the procedure division header from the method.
The actual values of the parameters are specified when the method is invoked.
Typically, when you invoke a method, you specify the parameters in the invocation, and the invoked method receives the parameters in the same order as in the invocation. For example:
invoke type MyClass::MyMethod(a,b) ... method-id MyMethod (x as string y as binary-long). ... end method.
The arguments a and b above are passed to the invoked method, and these correspond to the parameters x and y respectively, in the invoked method. The types of the parameters are specified in the AS phrase.
invoke type MyClass::MyMethod(param y = 3.141592, param x = "Pi") ... method-id MyMethod (value x as string, y as decimal). ... end method.
As long as the parameters x and y are named during invocation, the invoked method receives and uses them appropriately. If you use a mixture of positional and named arguments during invocation, the named arguments must be specified after any positional arguments.
invoke type MyClass::MyMethod(a) ... method-id MyMethod (value x as string, y as decimal = 3.141592). ... end method.
The invocation above only passes one positional argument (which corresponds to parameter x), so the value specified for optional parameter y is used in the method. Optional parameters can only be used as value parameters.
Parameters are passed into a method using one of the following modes, specified after the USING phrase:
If no passing mode is specified, parameters specified in the method header use the VALUE mode by default, and parameters specified in the procedure division header use the REFERENCE mode by default (except in extension methods where they are passed by value). You can change the default by setting the METHODDEFAULT Compiler directive.
When specifying multiple value parameters, the keyword VALUE is assumed for subsequent consecutive parameters. In the following example, v and vv are passed by value, and the others are passed by reference:
procedure division using a as binary-long value v as binary-long vv as binary-long reference r as binary-long rr as binary-long.
A value parameter behaves like a local variable. Its initial value is established when the method is invoked. When the method processes, it can change the value of the local variable. When the method finishes, the original argument in the invoking code remains unchanged. For example:
declare a as binary-long = 3 invoke type MyUtils::CheckByValue(a) display a *> a is still 3 method-id CheckByValue static (x as binary-long). set x to x + 1 *> x is now 4 end method.
In the above example, the parameter a is passed by value. The invoked method increments the value. However, on return to the invoking code, the variable a remains unchanged.
Usage:
Value parameters that are not managed types but are other COBOL data types are exposed as defined in the COBOL Type Compatibility table. For example: PIC X fields and groups are exposed as string. See Type Compatibility of .NET and JVM COBOL with Other Managed Languages.
All optional parameters must be specified following any non-optional parameters.
A reference parameter contains a reference to the required value rather than the value itself.
A reference parameter corresponds to a local variable, whose value is established when the method is invoked, and whose value can change while the method is processing. Unlike a value parameter, when the method finishes, the value of the original argument in the invoking code is changed accordingly.
Note, that a reference parameter does not necessarily create a copy of the variable local to the method. Instead, it might refer to the original argument in the location in use when the method was invoked. While the method is processing, the variable can change locally, but the original argument in the invoking code does not necessarily change. It might change or it might not. The value of the original argument is unknown until the method finishes. When the method finishes and returns to the invoking code, the value of the original argument in the invoking code is updated.
For example:
declare a as binary-long=3 invoke type MyUtils::CheckByReference(a) display a *> a is now 4 method-id CheckByReference static (reference x as binary-long). set x to x + 1 *> x is now 4 end method.
In the above example, the parameter a is passed by reference. The invoked method increments the variable a. On return to the invoking code, the variable a is updated with the incremented value.
Usage:
Reference parameters that do not correspond to managed types (such as PIC X fields, groups, numeric fields other than binary-long and so on) are exposed as COBOL pointers .
Like reference parameters, output parameters don't create a new storage location, but use the storage location of the variable specified on the invocation.
Output parameters are very similar to reference parameters. The main difference is that they must be assigned before returning to the invoking code.
For example:
declare a as binary-long=4 declare b as binary-long invoke type MyUtils::CheckByOutput(a,b) display "Value Param a = " & a *> a is still 4 display "Output Param b = " & b *> b is 16 method-id CheckByOutput static (x as binary-long, output y as binary-long). set x to x + 10 *> x is now 14 set y to x + 2 *> y is now 16 end method.
In the above example, two parameters are passed, where variable b is not initialized and is passed by output. The method CheckByOutput updates the variables x and y. On return, the output variable b is updated but the variable a remains unchanged.
Usage:
Output parameters that do not correspond to managed types (such as PIC X fields, groups, numeric fields other than binary-long and so on) are exposed as COBOL pointers.
Only one parameter may be passed with the PARAMS keyword, and it must be the last parameter. The parameter type must be a single dimensional array.
Programs that invoke this method may pass this parameter in the normal way as an array of the given type. Alternatively the program may pass zero or more parameters corresponding to the PARAMS parameter, where each parameter passed has the same type (or a compatible type) as the element type of the array. In this case the compiler will automatically generate code to create an array from the specified parameters (containing 0 or more elements), and pass this array to the invoked method.
Returning items pass the results of a method back to the invoking code. For example:
method-id AddMins (a as type MyTimer, b as binary-long) returning c as type MyTimer.
Each method can have only one returning item. However you can package a set of return information in an object. For example, the following AddMins() method receives and returns an instance of type myTime :
program-id ParamAsObject. 01 myTime type MyTimer value new MyTimer(5 45). procedure division. set myTime to myTime::AddMins(myTime 70) *> inline invocation display myTime::anHour & ":" myTime::aMin end program. class-id MyTimer. 01 anHour binary-long property. *> property keyword enables get/set 01 aMin binary-long property. ... method-id AddMins (a as type MyTimer, b as binary-long) returning c as type MyTimer. 01 h binary-long value 0. 01 m binary-long value 0. set b to a::aMin + b divide b by 60 giving h remainder m set c to new MyTimer(a::anHour + h , m) end method. end class.