COBOL is one of those languages you keep hearing about, one of the remains of the past.
Turns out it’s the language that makes the world go around, in particular in banks and financial institutions. I read somewhere that over 70% of business transactions are made through programs written in COBOL.
There are various reasons for that. First, the language was designed for that use case.
After all, it’s called COmmon Business-Oriented Language.
Kind of boring, for a name. But it goes straight to the point.
Another reason is that it’s old. Designed in 1959, it’s been used from the start to make those systems, and no one is going to change those programs that run fine.
Those programs are so important that they are just being maintained and improved, but never rewritten from scratch.
Anyway, you can read about the history of COBOL on Wikipedia. The goal here is to make a quick introduction to the language, so the next time you hear COBOL, you know what it looks like.
Install the GNU COBOL compiler
On a Mac, use Homebrew:
brew install gnu-cobol
or use any way you can install GNU commands on your Operating System (hint: Homebrew also works on Win/Linux)
Once this is done, you’ll have access to the
This is the man page for it:
Some instructions I found online involved installing an IDE (Integrated Development Environment) but you don’t need one to test things out.
Write your COBOL programs in a
.cob file, and compile it using
cobc -x <filename>.cob
Write the COBOL Hello, World!
I created a
hello.cob file and opened it in VS Code.
Immediately a popup told me some extensions could help with
.cob files. I’m impressed.
I’m going to install the first and most popular, named COBOL, to provide syntax highlighting.
Now add this code to the
HELLO IDENTIFICATION DIVISION. PROGRAM-ID. HELLO. PROCEDURE DIVISION. DISPLAY "Hello, World!". STOP RUN.
Compile it from the command line:
cobc -x hello.cob
and then run the binary file generated:
This was simple.
Sum two numbers received from the user
Now create a
HELLO IDENTIFICATION DIVISION. PROGRAM-ID. ADDITION. DATA DIVISION. WORKING-STORAGE SECTION. 77 NUM_1 PIC 9(4). 77 NUM_2 PIC 9(4). 77 SOLVE_SUM PIC 9(4). PROCEDURE DIVISION. PARA. DISPLAY "First number: ". ACCEPT NUM_1. DISPLAY "Second number: ". ACCEPT NUM_2. COMPUTE SOLVE_SUM = NUM_1 + NUM_2. DISPLAY "Sum: " SOLVE_SUM. STOP RUN.
cobc -x sum.cob
and you’ll be asked for 2 numbers, then the program calculates the sum:
Note that I have no idea how those programs run, the instructions meanings, but I just wanted to try it out.
I think this is all the COBOL I’ll ever write in my life.