A brief introduction about COBOL

COBOL is one of those languages that you keep hearing, and one of the legacy of the past.

It turns out that this is the language that makes the world work, especially in banks and financial institutions. I read somewhere that more than 70% of business transactions are conducted through programs written in COBOL.

There are multiple reasons. First, the language is designed for this use case.

After all, it is called the COMmon language for business.

A boring name. But this goes straight to the point.

Another reason is that it is very old. It was designed in 1959 and has been used to manufacture these systems from the very beginning, and no one will change those programs that work well.

These programs are so important that they have only been maintained and improved, and have never been rewritten from scratch.

In any case, you can learn about the history of COBOL on Wikipedia. The goal here is to introduce the language quickly, so the next time you hear COBOL, you will know how it looks.

Install GNU COBOL compiler

installationgnu-cobol.

On Mac, useHouse wine:

brew install gnu-cobol

Or use any method to install GNU commands on the operating system (hint: Homebrew can also be used on Win/Linux)

Once this is done, you will be able to accesscobccommand.

This is the man page:

man cobc

Some instructions I found on the Internet involve installing an IDE (Integrated Development Environment), but you don't need to do any testing.

Write your COBOL program with the following code.cobFile and use to compile

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.cob file:

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:

./hello

This was simple.

Sum two numbers received from the user

Now create a sum.cob file:

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.

Compile it:

cobc -x sum.cob

Run it:

./sum

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.


More computers tutorials: