sponsored

Be Curious and Learn: Modern Fortran in a Nutshell

istock 502196511
istock

Advances in science and engineering still rely on Fortran more than any other language by many important measures. Surprised? You shouldn’t be, although many people who call themselves programmers have never actually learned Fortran. I’ll teach you Fortran in five minutes or less. It won’t hurt, and you’ll know what is possibly the most important and influential programming language ever.

Computer scientists may behave like Fortran doesn’t exist, but popular culture (The Simpsons, Season 26, Episode 10) did better with the wise observation: “Fortran, the greatest of the programming languages!”In fact, more than half the computation done by the most powerful computers in the world comes from Fortran. In some large systems, Fortran code can be responsible for 80-100% of the computation.

It’s also incredibly easy to learn, as you’ll see with this short introduction to Fortran programming.  I assume you already know how to program in something other than Fortran (if you are completely new to programming, I highly recommend learning Fortran with “A Fortran Coloring Book”).

 Let’s get started

 program hello

    print *, "Hello, World!"

end program hello

Type in (use .f95 or .f90 extension for your file), compile, run this, and it prints “ Hello, World!”.  The “*,” in the print statement essentially means format automatically.

My second example does some math and finishes with ‘if/then/else’ statements:

program mymath

  implicit none

  real :: answer,x,y,sum,prod,cubedx,cubedy

  print  *, 'Type in two numbers (x and y)'

  read  *, x                                          

  read  *, y                                          

  sum = x + y  

  prod = x * y                                       

  cubedx = x ** 3;

  cubedy = y ** 3;

  print  *, 'The sum x+y is ', sum

  print  *, 'The product x*y is ', prod

  print  *, 'Cubed x and y ', cubedx, cubedy

  print  *, 'sine of x and y ', sin(x), sin(y)

  if ( x > y ) then

    print *, 'I noticed that x is larger than y'

  else

    print *, 'It seems that y is not smaller than x'

  end if

end program mymath  

You might notice that I slipped in “implicit none.” That is normal in modern Fortran – it tells the compiler to require variables to be explicitly declared so that unknown variables are flagged as errors.  Without it, Fortran assumes variables starting with ‘i’ through ‘n’ (the first two letters of integer) are integers, and others are floating point numbers (‘real’). That reduced how many cards we had to punch decades ago, but is generally frowned upon in these modern times.

 Running this program (and typing in 3.14 and 1.57) yields the following:

Type in two numbers (x and y)

3.14

1.57

 The sum x+y is    4.71000004   

 The product x*y is    4.92980051   

 Cubed x and y    30.9591484       3.86989355   

 sine of x and y    1.59254798E-03  0.999999702

 I noticed that x is larger than y   

Fortran is designed for math (FORmula TRANslation), note that “sin(…)” is built in without any need to include any header or package. Of course, Fortran supports floating point and integer numbers, but also supports complex numbers as well. No additives needed.

My third example involves loops (called “do” in Fortran):

 program  loop

  implicit none    

  integer  :: i, j, keep(100,2), w

  w = 0

  do  i = 10, 50, 5

    do  j = i, 20, 5

       print  *, i, j

       w = w + 1

       keep(w,1) = i

       keep(w,2) = j

    end  do

  end  do

  do i = 1, w

    print *, 'keep: ', keep(i,1), keep(i,2)

  end do

end  program loop

The print line in my example program only executes if the j do-loop runs. The j loop does not run at all when i exceeds 20.  This example also introduces arrays with the use of an array named ‘keep.’  Fortran starts array numbering at ‘1’ instead of zero, which is the same way Americans number building floors (floor #2 refers to the ground floor which is assumed to be ‘1’). Other places in the world use zero based numbering for their buildings (the “first floor” is what Americans call the “second floor”) like C and C++ do for arrays. The output from that ‘program loop’ example is shown below:

           10          10

          10          15

          10          20

          15          15

          15          20

          20          20

 keep:           10          10

 keep:           10          15

 keep:           10          20

 keep:           15          15

 keep:           15          20

 keep:           20          20

 In my final example, I’ll define a subroutine (function) called ‘ave’ to take the average of three numbers that I statically define.  You’ll notice that I got lazy and just wrote ‘end’ after the program and function. That’s okay, but if you use names, like I did previously, the compiler will help check that the end matches what you wanted it to match.

Program callret

  implicit none

  real a,b,c

  real av, avsq1, avsq2

  real ave

  data a,b,c/5.0,2.0,3.0/

  av = ave(a,b,c)

  print *,'The average of', a, b, c, ' is:',AV

end

real function ave(x,y,z)

  real x,y,z,sum

  sum = x + y + z

  ave = sum / 3.0

  return

end

 This prints:

 The average of   5.00000000       2.00000000       3.00000000      is:   3.33333325 

 Yes, Fortran can format output.  If we replace the “print” above as follows:

  print 8,'The average of ', a, b, c, ' is ',AV

8 format (a,2(f4.2,', '),f4.2,a,f7.5)

The output will become:

The average of 5.00, 2.00, 3.00 is 3.33333

Fortran formatting can also be done on a single line and have the same pretty output:

print "(a,2(f4.2,', '),f4.2,a,f7.5)",'The average of ',a,b,c,' is ',AV

If you’ve been led to believe that Fortran is weird, you were misled. I wrote all these examples in what Fortran programmers call ‘free form.’ That officially became part of Fortran with the ‘Fortran 90’ standard (hence my instructions to use .f90 or .f95 as file extensions; these tell the compiler, without special option switches, that we are using free form).  Keep in mind, in 1956 users wanted something geared for card punches.  I could go on for hours about how Fortran accommodates that with column-oriented formats and continuation characters, etc.  But, as I’ve shown, you really don’t need to learn that to write or read Fortran code.

So why is Fortran shunned by computer scientists? Great question. Fortran is the oldest language (c. 1956). It started in an era of tapes, punched cards, and replacing human computers. It predates block structured programming, parsing theory, and graphics. Along the way, computer scientists explored advances in computing while introducing thousands of programming languages, most of which failed to catch on. Nevertheless, in every field of human endeavor, “out with the old, in with the new” is fashionable. Computer science is no different. 

But when number crunching is important, nothing is better or easier than Fortran. I suggest we should all know lots of tools, and use the best tool for the job.  Just because my trusty hammer has been in my toolbox a long time doesn’t mean I don’t need it for the right job. Fortran is no different.

Fortran is not only the original high-level programming language, it’s alive, important, and widely used.  Who started the rumors to suggest otherwise?

 Resources

Click here to download your free 30-day trial of Intel Parallel Studio XE

Copyright © 2017 IDG Communications, Inc.