Classical Programming Languages: The Story of Fortran

0
63
Fortran

Recent research to prepare a list of the top 10 popular programming languages for the year 2021 threw up quite a number of surprises for the author. One was finding out the fact that three programming languages that were thought to be of a bygone era – and hence having historic significance only – are still surviving and making an impact in the programming world. These are: Fortran, COBOL and Pascal. The first article in this three part series discusses Fortran, a programming language still widely used for scientific computing.

The three languages Fortran, COBOL and Pascal are still used professionally, even after being in service for more than half a century. As these programming languages have made quite an impact in the programming world, and have become long-term trend-setters and bearers of standards, I call them classical programming languages.

The first serious question regarding Fortran is: Why is it still relevant? Fortran, which came into existence in 1957, is still ranked 25th and 30th in two of the popular programming language ranking schemes. This makes it better ranked than many programming languages that have captured the public eye recently. Though Fortran was used as a general-purpose programming language in the earlier days, nowadays it can be considered a domain-specific programming language. But what are the reasons behind its success for more than six decades in the field of scientific computing? Of course, there are many specialised tools and programming languages for scientific computing — MATLAB, Scilab, Maple, Mathematica, Julia, etc, are a few examples. But Fortran has a clear advantage over these tools. A Fortran implementation of a solution to a problem is much faster than a similar implementation in any of the tools mentioned earlier. To illustrate this point further, let us consider the programming languages used to write numerical libraries of Scilab. Of course, one of the programming languages is Fortran. Also, the memory consumed by a Fortran solution is much less when compared with similar solutions in many other popular tools. Certainly, using the tools mentioned earlier, in general, is easier. Still, the faster execution in Fortran is critical, especially when the task is complex in nature. Hence, Fortran coding is still used in disciplines such as physics, especially in large scale simulation of physical systems. This includes astrophysical modelling, computational fluid dynamics, climate modelling, etc.

Figure 1: Array processing output in Fortran

So the use of Fortran solutions is justified by their faster execution time when compared with solutions provided by scientific computing tools like Scilab. But if speed alone is the reason for using Fortran, why can’t we switch to C or C++? Both these languages offer solutions that are extremely fast. But another fact in favour of Fortran is its relative ease of use. For example, array processing, very essential in scientific computing, is easier in Fortran when compared with C or C++. In Fortran, mathematics-like processing of arrays is possible. For example, every element of an array can be multiplied by a constant value without using a loop. Consider the Fortran program shown below:

program arr
integer :: A(10), B(10)
do i=1, 10
A(i) = i
end do
B=A*10
do i = 1, 10
Print *, B(i)
end do
end program arr

Figure 1 shows the output of this Fortran program. Notice that the statement ‘B=A*10’ to initialise array B would have been placed inside a loop in C or C++. Hence, because of faster execution and relative ease of use, Fortran is still widely used for scientific computing and high performance computing (HPC).

Figure 2: The logo of Fortran

Finally, Fortran is still relevant because of the large amount of Fortran code still in use. In many fields of science, millions of lines of Fortran code are still in use and giving good performance. So, it is highly unlikely that all this programming code will be translated to code in some other programming language, that too without any real reason other than Fortran being not so popular among the general public.

So, what are the odds of a student learning programming for the first time in 2021 ever coming across a piece of code written in Fortran in his or her professional life? I don’t want to tell any tall stories. The chances of young programmers encountering Fortran code is low unless they are working in the field of scientific computing or high performance computing. But since such computing is on the rise, the use of programming languages like Fortran may also rise in the future.

Now that we are convinced about the relevance of Fortran, let us learn a bit about its history and current status. Fortran was developed by John Backus (also famous for Backus-Naur Form of Context Free Grammars) and was operational in 1957. The language was initially called FORTRAN (abbreviation of Formula Translation). This also tells us that Fortran was always meant to be a scientific computing tool. There were a lot of standards for Fortran over its life time, with each revision bringing in new features. FORTRAN, FORTRAN II, FORTRAN III and FORTRAN IV were the first four noteworthy standards of Fortran. FORTRAN 66 and FORTRAN 77 were two standards of Fortran that were widely followed in the sixties and seventies. Notice that the name FORTRAN was changed to Fortran starting with Fortran 90, yet another very popular Fortran standard. Fortran 90 was very significant since it brought in many fresh changes to the language like allowing free-form source codes, recursive procedures, operator overloading, dynamic memory allocation, etc. Fortran 95, Fortran 2003 and Fortran 2008 are the later standards of Fortran, with Fortran 2018 being the latest. Now, Fortran is truly a general-purpose, compiled and imperative programming language. Though code written in older standards of Fortran like FORTRAN 77 is still surviving, the Fortran programming community today, generally, advises the use of Fortran 90 or a later standard. Such frequent upgradation of Fortran standards also tells us that the language is very much alive and evolving as quickly as any trending programming language. Figure 2 shows the logo of Fortran.

Next comes the important question. How do we execute a Fortran program? An open source compiler called gfortran, part of the GNU Compiler Collections (GCC), allows us to compile and execute Fortran programs in Linux. The gfortran compiler can be installed in Ubuntu Linux with the command sudo apt install gfortran. In other flavours of Linux also the installation of gfortran is very simple and requires the execution of just one command.

Let us execute a small program to understand the syntax and working of the Fortran programming language. Fortran programs can have a number of different extensions based on the particular version of Fortran being used. Some of the extensions include .f, .f90, .f95, .f03, .f08, etc. Fortran compilers, in general, do not require any extensions to program files. But a general suggestion I received was that it is safer to use the .f90 extension for Fortran programs denoting free-form source code. The program called fib.f90 given below reads a number ‘n’ from the keyboard and prints the first ‘n’ Fibonacci numbers on the terminal.

program Fib
integer :: f1,f2,f3,ct,n
PRINT *, ‘Enter the Value of n: ‘
READ *, n
ct = 3
f1 = 0
f2 = 1
! THIS IS A COMMENT IN FORTRAN
PRINT *, ‘Fibonacci Numbers’
do
f3 = f2 + f1
if (ct <= 3) then
if (n > 0) then
print *, f1
end if
if (n > 1) then
print *, f2
end if
end if
if (n > 2) then
print *, f3
end if
if (ct >= n) then
exit
end if
ct = ct + 1
f1 = f2
f2 = f3
end do
end program Fib

The program illustrates many of the important features of a Fortran program. It shows a loop statement (e.g., do statement), a conditional statement (e.g., if statement), arithmetic expressions (e.g., f3=f2+f1), print statements (e.g., print *, f1), as well as a comment in Fortran (e.g., ! THIS IS A COMMENT IN FORTRAN). Since printing of Fibonacci numbers is a classical problem in computer programming, I don’t think any further explanation is required. Figure 3 shows the output of the program fib.f90.

Figure 3: Output of the program fib.f90

I must emphasise once again that the chances of young programmers coming across some Fortran code are relatively low unless they are working in the field of scientific computing. Still, there are many reasons why a programmer should at least hear the name Fortran. Let me give one more reason — the historic significance of Fortran. If you are a professional programmer, it is essential to know about historically significant programming languages and technologies. The existence of the large amount of legacy code and current uses in scientific computing also makes Fortran a good contender as a scientific computing tool. Since Fortran is still relevant, my suggestion to young programmers is that you should at least remember the facts mentioned in this article, i.e., the Fortran standards to be used as well as how to install a compiler and execute programs in Fortran. Hence this article could also be titled ‘The absolute minimum every programmer should know about Fortran’. COBOL will be taken up for discussion in the second article in this series.

LEAVE A REPLY

Please enter your comment!
Please enter your name here