Why Fortran is easy to learn

Some may think Fortran is an archaic language, and certainly not one that should be taught, but in any respects modern Fortran is an exceptionally good language to teach programming in. Certainly better than C and its idiosyncrasies. Better than Python or Julia you say? In many cases yes. Certainly things like I/O are still problematic for the novice programmer in these languages. Partially it is because modern Fortran is easy to read and comprehend, which is an important factor for the individual learning to program for the first time. The idea of introductory languages is not necessarily that they are suited to commercial endeavours, but rather that they are (i) easy to implement algorithm in, (ii) readable, and (iii) do not contain things that will confuse and bewilder.

Let’s perform the simple task of calculating the distance to the horizon. The calculation is quite simple, all that is required is the height of your eye, i.e. the distance that your eyes are off the surface of the water. The formula for calculating the distance, d, is d=√(2rh), where r is the radius of the earth (6367.45km), and h is the height of the object in metres (divided by 1000 to convert to km). So for an observer standing in a tower 42m above sea level, the distance to the horizon is d=√(2×6367.45×(42/1000)) = 23.13km. So a program to calculate this in Fortran would look like this:

program horizon
   real :: height, dist
   write(*,*) 'Height above sea level(m): '
   read(*,*) height
   dist = sqrt(2*6367.45*(height/1000.0))
   write(*,*) 'Distance to horizon = ', dist, 'km'
end program horizon

There is very little the novice programmer could not understand in this program – even without having any real notion of the syntax of Fortran. The terms read and write imply something is being input and output, the distance calculation is effectively just an equation. The only real sticking point maybe the variable declaration on the second line. The equivalent C program is somewhat more challenging.

#include <stdio.h>
#include <math.h>
int main(void)
{
   float height, dist;
   printf("Height above sea level(m): ");
   scanf("%f", &height);
   dist = sqrt(2*6367.45*(height/1000.0));
   printf("Distance to horizon = %.2fkm\n", dist);
   return 0;
}

Firstly it is four lines longer, and the only thing the same between the two programs is the equation. Two of the lines in the C program are taken up including libraries for I/O and math functions – which Fortran doesn’t require. The printf function deals with output, and is quasi-intuitive… the scanf function is not at all intuitive. What about int main(void) – muddy. What about the Python program, surely it must be better than both Fortran and C?

import math
height = float(input('Height above sea level(m): '))
dist = math.sqrt(2*6367.45*(height/1000.0))
print('Distance to horizon = ', dist, 'km')

Python dispenses with the notion of defining the “program”, allowing the algorithm to take place with little fanfare… I mean it is only 4 lines. But the math library still has to be imported. The prompt for the height is combined together with reading in the value, which is nice and compact, but the user will still question what float() is doing. And Julia?

println("Height above sea level(m): ")
height = parse(Float64, chomp(readline()))
dist = sqrt(2*6367.45*(height/1000.0))
println("Distance to horizon = ", dist, "km")

It suffers from input issues as well, chomping and parsing things – things novice programmers don’t care to know about. The commonality among all these programs is of course the calculation. The usability of a programming language is likely associated with how well it uses appropriate words to describe functions and actions, and uses symbols appropriately.

12 thoughts on “Why Fortran is easy to learn

  1. In C, the format specifications for printf() and scanf() are their own little language. When writing (or reading) these statements, a programmer must mentally shift from the C language to the specification language (and then back). This mental shifting imposes a cognitive load on the programmer.

    What about other languages, such as Python or Java?

    1. Honestly I’ve written about a dozen programs in Java. I never liked it, and its object-orientedness detracts from
      making it an easy language to learn. Like C derivatives, it takes a lot of effort to write Hello World. Python is quite
      easy to learn, but comes with its own baggage… namely being slow, forcing indents, and using some strange syntax
      when it comes to things like loop ranges… and frankly although there are a lot of libraries, managing them is a bit of a nightmare.

  2. For Python, you do not even need to import the math library: sqrt(x) can be written as x**0.5

    1. Yep it can, but to compare languages its best to keep them consistent. Surprisingly not everyone would ever think of using x**0.5 anyways.

      1. Yes, but there is a difference, so those experienced Python *would* know the difference:

        “`
        >>> import math
        >>> math.sqrt(-1)
        Traceback (most recent call last):
        File “”, line 1, in
        math.sqrt(-1)
        ValueError: math domain error
        >>> (-1) ** (1 / 2)
        (6.123233995736766e-17+1j)
        “`

        `**` will return complex numbers, math.sqrt will not.

      2. But the point is, that this is not about experienced programmers, it is about people learning to program.
        Again, my example has nothing to do with calculating the sqrt of -1.

  3. This was a fun article. Here’s an APL version of the program:
    0.5*⍨×/2 6367.45,⎕÷1000

    I also wrote a simpler version by transforming the division into a multiplication:
    0.5*⍨×/2 6367.45 0.001 ⎕

    The box collects the input. For the former, this is then divided by one thousand. In both cases, I then use reduction of multiplication. Lastly, the result is raised to the power of one half, which is square root. The tilde and diaeresis simply turns A f B into B f A, and I use it to avoid parentheses.

    Going a bit further, if I add the following two characters, it more conveniently permits multiple answers at once; it works without them, but this improves the shape of the result:
    ,⊃0.5*⍨×/2 6367.45 0.001 ⎕

    Isn’t that fun?

  4. Some comments on the python version:
    You don’t actually need the call to float() you are complaining about. The division by the floating point value 1000.0 will automagically convert “height” to a float.
    Also you don’t need the I/O. Python has an interactive toplevel. So you could just do:
    “`
    from math import sqrt

    def height(h):
    return sqrt(2*6367.45*(h/1000.0))

    height(42)
    height(46)

    “`

    1. Sure, but the code you wrote is not the same as the code I wrote. input() returns a string that has to be converted. You just called height() with two numbers… mine got user input… slightly different.

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.