The COBOL course that I am starting from provides an oddly large Hello World program. I am going to reduce that over several steps, to construct a more traditional (minimal) Hello World Program.
Here is the Hello World program from the intro course I am using:
000001 IDENTIFICATION DIVISION.
000002 PROGRAM-ID. "HELLOWORLD".
000003 AUTHOR. PEGGY FISHER.
000004*This is a comment in COBOL
000005*Columns 8-11 A Margin
000006*Columns 12-72 B Margin
000007 ENVIRONMENT DIVISION.
000008
000009 DATA DIVISION.
000010
000011 PROCEDURE DIVISION.
000012 0100-START-HERE.
000013 DISPLAY "Hello World!".
000014 STOP RUN.
000015 END PROGRAM HELLOWORLD.
Compile and run:
user@host:~/Dev/My_First_COBOL/1_HelloWorlds$ cobc -x HelloWorld.cbl
user@host:~/Dev/My_First_COBOL/1_HelloWorlds$ ./HelloWorld
Hello World!
user@host:~/Dev/My_First_COBOL/1_HelloWorlds$
This has several elements not needed for demonstrating that we can write and compile COBOL code. I’m just stripping those out one-by-one to make sure I actually understand what is needed and what isn’t.
First, we’ll remove the traditional line numbers. Note that several elements must begin in a certain column (like the asterisk being the seventh character in order to make a comment) or in a certain range of columns (called “A Margin” and “B Margin,” which you can look up), so I am replacing the line numbers, not simply removing them.
IDENTIFICATION DIVISION.
PROGRAM-ID. "HELLOWORLD".
AUTHOR. PEGGY FISHER.
*This is a comment in COBOL
*Columns 8-11 A Margin
*Columns 12-72 B Margin
ENVIRONMENT DIVISION.
DATA DIVISION.
PROCEDURE DIVISION.
0100-START-HERE.
DISPLAY "Hello World!".
STOP RUN.
END PROGRAM HELLOWORLD.
We’ll take out the non-mandatory elements of the Identification Division.
IDENTIFICATION DIVISION.
PROGRAM-ID. "HELLOWORLD".
ENVIRONMENT DIVISION.
DATA DIVISION.
PROCEDURE DIVISION.
0100-START-HERE.
DISPLAY "Hello World!".
STOP RUN.
END PROGRAM HELLOWORLD.
I’ll also go ahead and drop the unused Environment Division and Data Division as well.
IDENTIFICATION DIVISION.
PROGRAM-ID. "HELLOWORLD".
PROCEDURE DIVISION.
0100-START-HERE.
DISPLAY "Hello World!".
STOP RUN.
END PROGRAM HELLOWORLD.
Last but not least, we don’t actually need to tell the program when to end. It can handle running out of code and exiting on its own.
IDENTIFICATION DIVISION.
PROGRAM-ID. "HELLOWORLD".
PROCEDURE DIVISION.
0100-START-HERE.
DISPLAY "Hello World!".
user@host:~/Dev/My_First_COBOL/1_HelloWorlds$ cobc -x HelloWorld3.cbl
user@host:~/Dev/My_First_COBOL/1_HelloWorlds$ ./HelloWorld3
Hello World!
user@host:~/Dev/My_First_COBOL/1_HelloWorlds$
Okay, that gives the same result as before. Success.
Well, not exactly. If you already have a working COBOL program, there’s not any reason to use a minimal Hello World program. Still, it is important to know the minimum needed for a valid demonstration program. That’s the point of “Hello World,” after all.
Maybe someday someone will ask “Wait, what do I actually need in my COBOL file?,” and this page can answer the question.
Maybe that someone will be me. . .