"BY VALUE LENGTH OF directory-name SIZE 4" construct

The Micro Focus Developer doc related to CBL_GET-CURRENT-DIR suggests this:   "Use the "BY VALUE LENGTH OF directory-name SIZE 4" construct to pass the name-length parameter."  Based on the explanation in the doc, if I have a MY-DIR-NAME PIC X(80 and a MY-DIR-NAME-LENGTH PIC 9(4) then I can MOVE LENGTH OF MY-DIR-NAME TO MY-DIR-NAME-LENGTH in order to initialize it to a value of 80 in this example.  My question is about the terminology and concept of the "Use the "BY VALUE LENGTH OF directory-name SIZE 4" construct.  I would like to see an example of this and/or any links to doc explaining this concept behind this construct.


OK, Update.

I now can see how it might be used.  Instead of setting up an actual MY-DIR-NAME-LENGTH variable in working storage to be initialized with the length of MY-DIR-NAME, you can just use the expression "BY VALUE LENGTH OF MY-DIR-NAME SIZE 4" as a phrase at the appropriate point in the CBL_GET_CURRENT_DIR library routine/command, thereby eliminating an extra variable and a separate statement to initialize it.  However, the "SIZE 4" part... where is this explained?


  • Verified Answer

    Hi Austin,

    Certain dialects of COBOL (including the Micro Focus dialect)  support the use of a special register named "LENGTH OF". This is used in the form:


    and returns the size in bytes of the storage used by the variable MY-VARIABLE-NAME. So, instead of having to define a separate variable to contain the length value to be passed, you can use this register to determine the value on-the-fly. This also has the advantage that if you change the size of MY-VARIABLE-NAME, you don't have the housekeeping of also fixing the value of the associated variable where you've stored the length of MY-VARIABLE-NAME.

    You can find the LENGTH OF special register documented here:

    The SIZE keyword is actually an optional part of the CALL statement, when the BY VALUE phrase is used. The CALL BY VALUE essentially says  "let the called subprogram have its own copy of this value, rather than allowing it to access (and modify) the value here in the calling program". Since we are asking that the called routine be given its own copy, the SIZE option allows us to specify the size of the field that is dynamically created in that routine to hold this value. This is documented under General Rule 16 for the CALL statement here. Since the CBL_GET_CURRENT_DIR routine wants a 4 byte value passed that contains the size of the target field that will receive the directory name, we can use the SIZE option to make this dynamically created field that size.

    Please let us know if you have any additional questions about this.