Fortran Code Guessing Game

A thinking-out-loud analysis of Fortran code. What could a programmer accustomed to various modern languages figure out about a Fortran code snippet?

In one of my recent articles, I wrote a snippet of Fortran code to argue that the programming languages of old are not that different from the ones we use today. One could guess a lot of what is written there if one has some exposure to some different languages. I am writing this for the benefit of one of my readers, Matthew, but I thought it might be of general interest. So here is the Fortran code:

Without looking up anything about Fortran, I will try to guess what everything means here based on other languages I use. I promise you that I have never written a line of Fortran code in my life or ever been taught Fortran.

Start and End of Function

That the keywords for marking the beginning and end of the function is and seems pretty obvious. Here I rely on the fact that the name is self-explanatory as well as the fact that functions are marked with and in Julia, Lua and Matlab as well.

Variable Definitions and Type Annotations

I scan the code to find the most easy stuff to figure out first and work from there. For example, the first variable definitions are a bit tricky, so I read on and I see this line:

I can see in the loop-looking structure that the is used, and given that and are usually used as indices in both math and code, it seems safe to assume they are meant to be two integers. This tells me the variable names must come last and the type first. Hence, is the name of a type.

That type and variable names are separated with some characters seems reasonable. For example, in the modern languages Kotlin and Swift, we have type and variables separated by a single colon:

In Haskell and Julia, you would write type annotations for function arguments like this:

Thus, we have various language that use double colons to separate variable name and its type. Hence, it is very likely that Fortran is indeed using it for the same purpose.

Array Definitions

The next tricky part is the code for defining arrays. This looks unusual to modern programmers, but with exposure to some variety of languages, it does not look too crazy.

The challenge here is to understand how this keyword is used. Can we even know if this is used to define arrays? The variable name gives one hint. But we can get more clues by looking at how the variables are used:

To a modern programmer, this looks like a function call. Are we sorting ? That makes no sense; it is a single integer. However, if you have used Matlab or Basic, you should know that this is how array elements are accessed in those languages. They don’t use square brackets like C-like languages.

In fact, in Microsoft Visual Basic you would define an array of four integers like this:

You would access the third element of this array like this:

The in Basic is short for dimension. Thus it seems reasonable that an older language like Fortran may, like Basic, have used the word to set the dimensions of an array. The meaning of is, however, hard to guess. However, colons are often used in ranges. Without a start and end number for the range, it may mean a variable length array.

Array Usage

Both in Matlab and Basic, you could call functions with parentheses, even if this is how you accessed array elements as well. Thus could be an array, but most likely it is a function.

From languages I use such as Go, Python and Julia, a colon with no end such as this

is used to indicate a range of of elements. Here is an example from the Python REPL:


By having some knowledge of languages outside the dominating curly braces style of languages such as C/C++, Java and C#, it is perfectly possible to decipher a lot of a Fortran program even if you have never actually learned Fortran.

Older languages may have some odd quirks seen with our modern eyes, but they are not completely alien. Established conventions get reused a lot. Syntax is seldom entirely arbitrary.

Geek dad, living in Oslo, Norway with passion for UX, Julia programming, science, teaching, reading and writing.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store