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
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 access
This is the man page:
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.cobfile and opened it in VS Code. Immediately a popup told me some extensions could help with
.cobfiles. 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.
More computers tutorials:
- Finite State Machines
- The Decimal Number System
- The Binary Number System
- Converting Numbers from Decimal to Binary
- Printable ASCII characters list
- Non-printable ASCII characters list
- How to connect to a Raspberry Pi using a Mac
- How to make sure the Raspberry Pi has always the same IP address
- A very short introduction to COBOL