6

What is the difference between BY CONTENT and BY VALUE in a CALL statement in COBOL?

  • Question should mention OS and compiler. Answers currently include details that are not always true for different compilers. E.g., BY VALUE isn't always limited to {an "integer" or a one-byte alphanumeric value) and literals, and any valid data type can be passed BY VALUE in at least some COBOL compilers. – user2338816 Oct 09 '15 at 02:39

2 Answers2

7

BY CONTENT on a CALL will copy the content of the identifier to a compiler-managed area of storage, which will then be passed to the CALLed program "by reference" implicitly.

This means the CALLed program can change the data, but no change made in the CALLed program will affect the original data in the CALLing program.

Any identifier, of any size valid for the compiler, can be used BY CONTENT (subject to any limits, if existing, which are documented for the specific compiler - you never know).

Although you can change the value in a CALLed program, it would seem a bit obscure to do so, at best.

BY VALUE is an entirely different beast. It is very limited, in that the value "passed" can be either an "integer" or a one-byte alphanumeric value. It can also be a literal.

The PROCEDURE DIVISION USING ... has to know, in the case of BY VALUE, that is is so, by specifying it in an equivalent manner to the CALL. BY REFERENCE and BY CONTENT on the CALL are both BY REFERENCE on the PROCEDURE DIVISION USING.

How this is implemented is down to the specific compiler. IBM Enterprise COBOL puts the value itself in the "parameter list".

Ven
  • 19,015
  • 2
  • 41
  • 61
Bill Woodger
  • 12,968
  • 4
  • 38
  • 47
1

Passing parameters by content is the same as passing by reference, except that the data is not copied back into the COBOL memory when the call has completed. Meaning that the original variable cannot be edited by the Called unit.

So the difference between BY CONTENT and BY VALUE is that in the case of BY VALUEonly the value gets passed and therefor not all kind of variables can be passed this way while BY CONTENT the pointer of a copied variable gets passed and in this way every type of variable can be passed.

See: http://documentation.microfocus.com/help/index.jsp?topic=%2Fcom.microfocus.eclipse.infocenter.visualcobol.eclipseux%2FGUID-EB09203C-3873-4DBE-9298-0C353BC0701A.html

By Reference:

When a parameter is passed by reference, a copy of the item in the JVM COBOL is passed to the native code. When the call to the native has finished, any changes to the information made in the native code are copied back to the JVM COBOL. However, this does mean that memory is shared between the JVM and native environments. In fact, what is actually passed to the native code is a pointer to the copied data. This is useful if you are calling non-COBOL programs. The implications of this are very important, particularly in multi-threaded environments. Any changes to reference parameters are not visible to the JVM COBOL calling program until the call has completed. Arbitrarily complex group items (within the memory limitations) can be passed by reference. The group definition must be identical in the native and JVM COBOL source code and it must not contain USAGE POINTER items. Strings (java.lang.Strings) and tables (java.lang.byte arrays) can be passed by reference. All other objects (types that inherit from java.lang.Object, including valuetypes) cannot be passed by reference (or by value - see below).

By Content

Passing parameters by content is the same as passing by reference, except that the data is not copied back into the JVM COBOL memory when the call has completed. Any item that can be passed by reference can be passed by content.

By Value

In JVM COBOL, the only items that can be passed by value are as follows: binary-long - the recommended type for passing by value pic x(4) comp-x pic (9)9 comp pic s(9)9 comp pic (9)9 comp-5 pic s(9)9 comp-5

Niki van Stein
  • 10,564
  • 3
  • 29
  • 62