Introduction to Fortran

Simple Program Construction in Fortran » History and Introduction to Fortran

Amateur and professional programmers alike have many languages at their disposal for use in coding something like our previous “what to wear” program. In many cases one might choose an object-oriented language such as Java to write said program, however here we will focus on the lesser-known Fortran, still a favorite among scientists and engineers for good reason.

IBM machine with man and woman

Fortran (Formula Translation) was developed in 1954 by a diverse team at IBM, in an effort to simplify the programming process so that others aside from computer programming experts could perform complex calculations without needing to directly modify assembly or machine language (0s and 1s). Or, as its founder John Backus would say, “I was lazy, and didn’t like writing programs, so I started work on a system to make it easier to write programs.”

Fortran achieved this first-ever, high-level programming interface through the creation of a compiler - a program, or set of programs, that turns algebra and logical expression-based, high-level code into assembly language. Fortran effectively reduced the number of necessary programming statements by a factor of 20 by having the compiler do the work of laborious tasks such as allocating memory to specific program operations. Prior machine language programs were written to a specific computer, but a Fortran program could be run on any computer with the Fortran compiler installed - and thus, an industry heavyweight was born.

Titan Supercomputer

Fortran still reigns as the language of choice in computationally-expensive fields such as computational fluid dynamics (weather and climate models!), finite element analysis, economic modeling and DNA sequencing. It is used to benchmark the speed and computing power of the world’s fastest supercomputers.

In Fortran, your code will be in a logical algorithmic format just like your pseudocode. It just needs to be translated from pseudocode to Fortran code proper.

 1. Use app to gather forecast high temperature for today. 2. Use calendar to find whether it is a workday or weekend. 3. Catalog your entire wardrobe into short-sleeved, long-sleeved dress, t-shirt,
and long-sleeved tops, and shorts, pants, athletic shorts, and pajama pants. 4. If today is a weekend a. If the forecast temperature is at or above 70°F, select a t-shirt,
else select a long-sleeved top. b. If the forecast temperature is above 60°F, select athletic shorts,
else select pajama pants. else (today is a workday) a. If the forecast temperature is at or above 70°F, select a short-sleeved top,
else select a long-sleeved dress top. b. If the forecast temperature is above 60°F, select shorts,
else select pants. 5. Subtract today’s outfit from your clean wardrobe. 6. Look through clean clothes a. If no more clean clothes left i. Print out an alert to do laundry.

We have been using specific language to describe some of our pseudocode from the beginning of this lesson. You may not have noticed it, but the words will become a clear reminder of what the Fortran code will actually look like as we move forward. Pseudocode is meant to be an interface between human-readable language and computer-readable language. Because of this, we often intermix the code and the pseudocode. As you learn more about coding in your chosen language, your pseudocode will start to look more like code, until eventually, you may not need to pseudocode anymore.

All Fortran programs must start and end the same way, using “program”, “implicit none” and “end”:

 program pick_outfit implicit none  !this line is a comment, and will be ignored by the compiler  end 

The “program” line indicates to the compiler that the code starts here and the name of the program is: “pick_outfit”. “implicit none” won’t mean anything to you right now, but it is suggested best practice that avoids some implied syntax from early versions of Fortran. The “end” line is the last line of the code that will be executed. Everything after the “end” line will be ignored.

In Fortran 90 and later, code can start anywhere on the line, with no indentation required. Note that in Fortran 77 and earlier, you will always indent code by 7 spaces before writing any executable code. There are only a few reasons to write anything in the first 6 columns of a Fortran 77 or earlier code, comments, continuations, and labels, but only comments will be covered herein. Comments start with a “!”. If you write a “!” in column one of the code, it is a comment and will be ignored by the compiler. It helps other programmers and users to read the code better and gives more detail when complex code may be too hard to understand on initial pass.

Starting just like we did with the pseudocode, let’s show you how to gather some weather data for use in the Fortran code.