Search code examples
cobol

COBOL function output as string


123456*
       IDENTIFICATION DIVISION.
       PROGRAM-ID. "EVEN-OR-ODD".
       DATA DIVISION.
       WORKING-STORAGE SECTION.
       01 Num-1    PIC 9(02).
       02 Answer   PIC XXXX.
       PROCEDURE DIVISION.
           GOBACK.
       EVEN-OR-ODD.
          IF FUNCTION REM(NUM-1, 2) = 0
              COMPUTE ANSWER =  "Even"
          ELSE
              COMPUTE ANSWER = "Odd"
          END-IF
       END PROGRAM EVEN-OR-ODD.

Its a simple even odd function. It should check if number is even return "even" else return "odd" Can someone explain what's wrong ?


Solution

  • So much things a COBOL compiler would have told you...

    • GOBACK as first statement, so the rest would not be executed
    • the program misses a final period and a necessary/reasonable (that depends on the compiler) statement to end the program (END PROGRAM is only parsed for the compilation phase) - you likely want to move your GOBACK. to the end
    • COMPUTE does not set anything to alphanumeric, you likely want MOVE
    • there is no way to know what the program would have done, so possibly want DISPLAY instead of MOVE
    • NUM-1 is never set and has no initial VALUE - so it could theoretically even abend