Menu

Do I need to code GOBACK explicitly in a function?

Emmad
2021-08-27
2021-08-27
  • Emmad

    Emmad - 2021-08-27

    What is the difference between the two snippets below when coded in a batch program?
    Thanks all.

    IDENTIFICATION DIVISION.
    Function code
    ...
    EXIT FUNCITON.
    END FUNCTION FUNCX. *> No goback
    

    and

    IDENTIFICATION DIVISION.
    Function code
    ...
    GO BACK.
    EXIT FUNCITON.
    END FUNCTION FUNCX.
    
     
    • Brian Tiffin

      Brian Tiffin - 2021-08-27

      To answer the titular question, no, not really.

      Is there an effective difference, no, not really. Practically, you end up with a few extraneous hops in the intermediate C code.

      It comes down to how the compiler needs to keep track of module entry and exit points. The control flowing out of a function is always assumed to return to an expression. In the case of GOBACK, immediately followed by EXIT FUNCTION there will be two jumps to the module exit handler.

         /* Line: 53        : GOBACK             : exitfun.cob */
        goto exit_function;
        /* Line: 53        : EXIT FUNCTION      : exitfun.cob */
        goto exit_function;
      
        /* Function exit */
      
        exit_function:
        ...
      

      The goback emitted code to jump to the function exit. The exit function also generated code to jump to the function exit.

      If there was neither a goback or exit function, then the compiler control flow handling doesn't even generate code to jump to the exit, and just flows into the function exit code. There won't even be the exit_function: label generated in the C code in simple cases, it will just fall through to the normal, always generated, function exit code.

      Does this make sense, Emmad?

      If you don't mind reading C, you can take a look at the intermediate code with cobc -C filename.cob and then fave-editor filename.c. In 4.0 pre-rels, there will be a /* Function exit */ comment generated that will show how the routine exit is managed.

      But for maintenance and keeping sane, adding a goback in the places you mean to go back from, is still a diligent and reader friendly thing to do. ;-)

      Cheers,
      Blue

       
      • Emmad

        Emmad - 2021-08-27

        Excellent explanation - Thank you very much. I am not clever enough to know about C (I tried) :)

         
  • Simon Sobisch

    Simon Sobisch - 2021-08-27

    EXIT FUNCTION is the same as GOBACK in a function.

    Note: EXIT FUNCTION (and EXIT METHOD, which is not available in GnuCOBOL) should not be used. Both were added with the 2002 Standard and removed in COBOL 202x - because they were not much used and GOBACK works in both places.
    EXIT PROGRAM is heavily used and is archaic, use it only if you want to go on if the program is the first program but return if it was called.

    So: always use GOBACK if you want to return to the caller (which in case of a program can also be the operating system).

    To your original question:
    GOBACK (or EXIT FUNCTION) and END FUNCTION serve different purposes; the first marks the logical end of the function (run-time), the seconds marks the end of the source definition (compile-time scope). Concerning the COBOL standard there's an implicit EXIT PROGRAM at the end of the source definition of a program (which can be at END PROGRAM or, for a single source, at the end of the source file), but interestingly there's no matching rule for functions. I guess it works, but as it is undefined: use GOBACK :-)

     
    • Brian Tiffin

      Brian Tiffin - 2021-08-27

      Simon and I seem to be online at the same time. ;-)

       
      😄
      1
    • Emmad

      Emmad - 2021-08-27

      Excellent explanation - Thank you very much.

       

Anonymous
Anonymous

Add attachments
Cancel