Open Source for you

Classical Programmin­g Languages: The Story of Fortran

-

The three languages Fortran, COBOL and Pascal are still used profession­ally, even after being in service for more than half a century. As these programmin­g languages have made quite an impact in the programmin­g world, and have become longterm trend-setters and bearers of standards, I call them classical programmin­g 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 programmin­g language ranking schemes. This makes it better ranked than many programmin­g languages that have captured the public eye recently. Though Fortran was used as a general-purpose programmin­g language in the earlier days, nowadays it can be considered a domain-specific programmin­g 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 specialise­d tools and programmin­g languages for scientific computing — MATLAB, Scilab, Maple, Mathematic­a, Julia, etc, are a few examples. But Fortran has a clear advantage over these tools. A Fortran implementa­tion of a solution to a problem is much faster than a similar implementa­tion in any of the tools mentioned earlier. To illustrate this point further, let us consider the programmin­g languages used to write numerical libraries of Scilab. Of course, one of the programmin­g 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 discipline­s such as physics, especially in large scale simulation of physical systems. This includes astrophysi­cal modelling, computatio­nal fluid dynamics, climate modelling, etc.

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

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, mathematic­s-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:

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 performanc­e computing (HPC).

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 performanc­e. So, it is highly unlikely that all this programmin­g code will be translated to code in some other programmin­g 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 programmin­g for the first time in 2021 ever coming across a piece of code written in Fortran in his or her profession­al life? I don’t want to tell any tall stories. The chances of young programmer­s encounteri­ng Fortran code is low unless they are working in the field of scientific computing or high performanc­e computing. But since such computing is on the rise, the use of programmin­g 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 operationa­l in 1957. The language was initially called FORTRAN (abbreviati­on of Formula Translatio­n). 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 significan­t since it brought in many fresh changes to the language like allowing free-form source codes, recursive procedures, operator overloadin­g, 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 programmin­g language. Though code written in older standards of Fortran like FORTRAN 77 is still surviving, the Fortran programmin­g community today, generally, advises the use of Fortran 90 or a later standard. Such frequent upgradatio­n of Fortran standards also tells us that the language is very much alive and evolving as quickly as any trending programmin­g 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 Collection­s (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 installati­on 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 programmin­g 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.

The program illustrate­s many of the important features of a Fortran program. It shows a loop statement (e.g., do statement), a conditiona­l statement (e.g., if statement), arithmetic expression­s (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 programmin­g, I don’t think any further explanatio­n is required. Figure 3 shows the output of the program fib.f90.

I must emphasise once again that the chances of young programmer­s 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 significan­ce of Fortran. If you are a profession­al programmer, it is essential to know about historical­ly significan­t programmin­g languages and technologi­es. 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 programmer­s 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.

 ??  ??
 ??  ?? Figure 1: Array processing output in Fortran
Figure 1: Array processing output in Fortran
 ??  ??
 ??  ?? Figure 2: The logo of Fortran
Figure 2: The logo of Fortran
 ??  ??
 ??  ?? Figure 3: Output of the program fib.f90
Figure 3: Output of the program fib.f90

Newspapers in English

Newspapers from India