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.
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
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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 :-)
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
What is the difference between the two snippets below when coded in a batch program?
Thanks all.
and
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 byEXIT FUNCTIONthere will be two jumps to the module exit handler.The
gobackemitted code to jump to the function exit. Theexit functionalso generated code to jump to the function exit.If there was neither a
gobackorexit 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 theexit_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.coband thenfave-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
gobackin the places you mean to go back from, is still a diligent and reader friendly thing to do. ;-)Cheers,
Blue
Excellent explanation - Thank you very much. I am not clever enough to know about C (I tried) :)
EXIT FUNCTIONis the same asGOBACKin a function.Note:
EXIT FUNCTION(andEXIT 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 andGOBACKworks in both places.EXIT PROGRAMis 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
GOBACKif you want to return to the caller (which in case of a program can also be the operating system).To your original question:
GOBACK(orEXIT FUNCTION) andEND FUNCTIONserve 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 implicitEXIT PROGRAMat the end of the source definition of a program (which can be atEND PROGRAMor, 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: useGOBACK:-)Simon and I seem to be online at the same time. ;-)
Excellent explanation - Thank you very much.