Implicit none

The first programming language I ever learned was Fortran. And before you ask, no, I’m not seventy years old. It just so happened that during my undergraduate degree, I took two courses on numerical methods for physics which were taught in Fortran, and for good reason: Fortran is still one of the best languages to deal with numerical problems.

It’s incredibly fast for the typical numerical tasks that arise in physics such as solving differential equations. It’s also a compiled language rather than an interpreted language, so learning Fortran teaches you about how compilers work, and hence some of the basics of what a computer is actually doing when it reads and executes a program. More than that, it’s a low level language: writing Fortran teaches you how to think like a computer.

You can measure a programmer’s perspective by noting his attitude on the continuing vitality of FORTRAN.

Alan Perlis, Epigrams on Programming

Unfortunately, unlike people, computers are logical creatures, and they like to have all the information they need to solve a problem laid out in front of them before they start trying to solve it. In particular, Fortran needs to know the type of every variable you use in any given program — they cannot be defined on-the-fly like in Python, for example.

What this means is at the start of every program you write, you need to include a list of all the variables you use, and declare their type. A basic Fortran program might look something like this:

program hello_world
implicit none

integer :: i, N
real    :: x

N = 10
x = 3.14

do i = 1, N
    write(*,*) 'hello world!'
end do

write(*,*) 'The square root of pi is about', sqrt(x)

end program hello_world

I defined i and N as integers and x as a real. If you don’t do this, the compiler will complain that your variable has no implicit type.

This highlights another thing I love about Fortran: the implicit none statement. It’s good practice to include this at the start of every program you write. If you don’t, Fortran will assume the types of some variables for you. In particular, any variable whose name begins with i, j, k, l, m or n will be assumed to be an integer. Any other variable will be assumed to be a real. Most of the time this is not a helpful assumption to make, so you tell Fortran implicit none, followed by the explicit declaration of your variable types.

Anyway, why am I telling you all this? The reason is that last week I found a bug in my Fortran code directly related to variable typing.

For the past eighteen months or so, I’ve been working exclusively in Python, and I suppose I got a bit sloppy with my variables due to the luxury of Python’s on-the-fly assignment. The result was that I was working on a piece of Fortran code where I wanted a certain variable to be an integer. If the user selected this variable to be equal to one, another variable would take a particular value (a float, not an integer). If the user selected the first variable to be equal to two, the second variable would take a different value instead (again, also a float).

However, in the end I realised I didn’t want this functionality where the user selected the variable’s value, and I would just hard-code its value instead. Only I forgot to change the declaration of the variable from integer to real. The variable was supposed to have a value of roughly 1.5 x 10^-7, but I was still telling Fortran it should be an integer, so the code went on its merry way, changing 1.5 x 10^-7 to be equal to zero every time it ran. It took me about two months to notice this subtle, yet obvious, bug.

Moral of the story: implicit none!

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Create a website or blog at

%d bloggers like this: