Boolean functions


I would like to know whether it is possible to write a COBOL function that returns a true or false value so that one can write:

    if myfunc() then <true-branch> else <false-branch> end-if.

I can write functions that return values "t" or "f" (or 1/0, or "TRUE"/"FALSE", or whatever) and then write:

    if myfunc() = "t" then <true-branch> else <false-branch> end-if.

That "= ""t""" seems like something I should not have to do.

I read somewhere abouit using PIC 1, where B"1" is true and B"0" is false. I wrote a function using that but the callers do not compile.

To expand that:

    function-id. myfunc prototype.

    linkage section.

    01  l-result pic 1.

    procedure division returning l-result.

    end function myfunc.


    if myfunc() then


I see a compiler error on the ')' of the function call saying "Error    1    COBCH0001 : Undefined error. Inform Technical Support   ..."

Any insights gratefully received.


       function-id. numeric-1 prototype.
       linkage section.
       01      l-data      pic x any length.
      *01      l-result    pic x.
       01      l-result    pic 1(16).
       procedure division using l-data returning l-result.
       end function numeric-1.


  • COBOL has a facility for this - it's called the level 88 condition-name - maybe not exactly how you are visualizing it, however.

    Google "COBOL level 88" to read all about it.

  • It looks like it isn't currently possible to have a user-defined function return a pic 1 item. I've reproduced the "Undefined error. Inform Technical Support" error message with my own test program. I'll see if there's an open RPI for this, and raise one if not.

    Austin1 suggested you use a level-88. That's not at all the same thing as a function that returns a true or false result, but it might suit your purposes. Level-88 items are constant values that can be used to abbreviate testing conditions. For example:

    working-storage section.
    01 ws-some-state pic x value 'a'.
       88 state-a value 'a'.
       88 state-b value 'b'.
    procedure division.
    set state-a to true
    if state-b

    (Man, the editor for MF Community is terrible. Could we please get an editor that has decent support for code blocks?)

    You set the parent item of the level-88 to a particular value by setting the corresponding level-88 item to true. You can test if the parent item has a particular value by testing the corresponding level-88.

    This is for native COBOL, by the way. Managed COBOL is much nicer - inline method invocation means you can invoke methods in the conditional phrase of the if statement, or refer to properties, etc. And methods can return boolean values, and properties can evaluate to boolean values.

  • Indeed that is not what I'm looking for. See my (upcoming) reply to Michael.


  • Indeed that is not what I'm looking for. See my (upcoming) reply to Michael.


  • Michael,

    Thanks for that.

    I know about 88 levels.

    To use an 88 level I have to do:

       01  w-flag pic x.

         88  wc-ok value "y" false "n".

       call "validformat" using w-data w-flag.

       if wc-ok then ...


       01  w-flag pic x.

         88  wc-ok value "y" false "n".

       move  validformat(w-data) to w-flag.

       if wc-ok then ...

    I have to: 1) create a data item for the purpose; 2) type 2 lines.

    That's both more work and less clear than:

       if validformat(w-data) then ...

    Preaching to the converted I guess.


  • Yes, I agree - level-88s are definitely not the same thing. I did raise an RPI for the issue with functions that return a pic-1 item, but for now the only solution I know of is the one you had in your original post, where you have the function return a pic-x item and test for it explicitly in the condition.

    I have to say - most of the COBOL I write these days is Managed OO COBOL with the new simplified syntax, and when I go back to old procedural COBOL the difference is stark. But of course most projects can't simply switch to using managed code.