An Introduction to programming with COBOL
The Problem(s)
As businesses began relying more heavily on computers for data processing—such as payroll, inventory, banking, and more—in the 1950s, it became clear that change was needed. At the time, each computer had its own proprietary programming language, which was often hardware-specific. As a result, sharing programs between different systems was incredibly difficult. It's also important to note that programming languages at the time were highly mathematical, making them inaccessible to non-technical users.
The Solution
Realizing how much money was being wasted on porting programs to different systems, the U.S. Department of Defense (DoD) began advocating for a standardized programming language that could be easily used across different systems. With this goal in mind, a group of experts gathered at the Conference on Data Systems Languages (CODASYL) in 1959. Among them was Grace Hopper, a pioneering computer scientist who was a driving force behind the development of the first compiler, playing a pivotal role in its creation alongside other contributors.
Out of this conference came the COmmon Business-Oriented Language (COBOL), a revolutionary language designed to meet the needs of business data processing. Among the features that made COBOL groundbreaking was its English-like syntax, which allowed non-technical people to read, understand, and even write programs. This was akin to how modern drag-and-drop website builders have democratized—or disrupted, depending on your perspective—web development today.
The Structure of a COBOL program
Now that we've learned a bit about COBOL and some of the problems it solved, we'll take a look at the structure of a COBOL program.
Columns in COBOL
Since COBOL dates back to the era of punched cards, it’s no surprise that its code is organized into specific areas, each made up of one or more columns with a unique purpose.
The Structure of a COBOL program
Because COBOL is English-like, perhaps it comes as no surprise that a COBOL program is structured much like a document, with divisions, sections, paragraphs, sentences, and statements. Let's explore these a little more.
The Four Divisions of a COBOL program
Every COBOL program consists of four divisions; however, only two are required to write a basic COBOL program: the IDENTIFICATION DIVISION and the PROCEDURE DIVISION.
IDENTIFICATION DIVISION
It is mandatory for every COBOL program. If your program doesn't start with these "magical words", it's not a valid COBOL program. It provides a place to store some valuable metadata, such as the program name, author, and other details. Within this division the the PROGRAM-ID paragraph must appear directly after the IDENTIFICATION DIVISION and is the only required paragraph. Other optional paragraphs include: AUTHOR, DATE-WRITTEN, DATE-COMPILED, and SECURITY.
Recommended by LinkedIn
ENVIRONMENT DIVISION
This division must follow the IDENTIFICATION DIVISION and describes the system the program will run on, including:
The ENVIRONMENT DIVISION is divided into two sections:
DATA DIVISION
This is where your COBOL program defines and stores all the data it will use. It’s like the program’s "storage room" for variables, records, and files.
The DATA DIVISION is divided into the following sections:
PROCEDURE DIVISION
The PROCEDURE DIVISION is mandatory for any COBOL program since it's where the program is brought to life. This is where you define the logic, operations, and all the processing that will occur happens.
A Basic COBOL Program
If you've done any programming before, this program will not shock you. It's the basic 'Hello World' program.
We start by declaring our IDENTIFICATION DIVISION followed by a PROGRAM-ID, which is the only required paragraph in this division.
Next, we declare the PROCEDURE DIVISION, which is where the action happens. Now, because we want to see something, we need to tell COBOL to write something to the output device; these days, it's typically a terminal screen, but back in the day, it was often a printer.
Finally, we instruct COBOL that immediately after it displays Hello World, we want to stop everything, and that's done with the STOP RUN statement.
Summary
In this article, I've described the history and reasoning behind COBOL, the structure of COBOL, and shown you how to write a basic program using COBOL.
As I'm still learning COBOL, if you notice anything that needs correction, please let me know, and I'll do my best to update this article.
As I always wonder why people "code on Windows/Ubuntu/..." and then use a mainframe-centric extension that is best when ... actually connecting to a mainframe instead of a solution for distributed systems... Have you checked out the superbol extension from OCamlPro as well? It also features a COBOL source level debugging option that can come in handy when you get "deep into COBOL programs" (or, you know, just wondering why your "Hello World Extended" does not operate as you wish). If you run on Windows you're likely interesting in their "All-In-One" binary Package offering GnuCOBOL 64bit along with GCSORT and GixSQL (also supported in their vscode extension). https://superbol.eu/en/
good understanding... ready for the ride
See Manas Kumar he has the best COBOL practices and sample code ... I have been COBOL since 1976
My father focused on mainframes and COBOL. It's seriously one of the most important languages businesses still rely on. It should be taught in schools more cause most CS students really don't know how in demand this skill is.