FORTRAN Programming Intro
FORTRAN is a language that has been in use for a long time (since cards were used to store program code and card readers were used to get the program into the computer). Thus, it has some very specific and somewhat odd format requirements and style guidelines as compared with more recently developed programming languages.
Rules (that must be followed):
- Only UPPERCASE letters may be used. (This no longer applies as FORTRAN is now case-insensitive)
- Each line is exactly 80 characters (or columns) long.
- Columns 1-5 are for the line label. And, a 'c' or 'C' in column 1 indicates that this line is a comment.
- Column 6 is blank unless this line is a continuation of the previous code line.
- Column 7 is the start of the code for this line.
- Column 72 is the end of the current line of code.
- Characters 73-80 can be used to sequentially number the line (card). This was for use by card sorters and is no longer used.
Thankfully, modern editors and compilers help programmers follow this rule and later versions (after F77) no longer have some of these restrictions. In addition to the rules that must be followed, it is common to have style guidelines that should be followed to facilitate the maintenance of your program.
Style Guidelines (that should be followed):
(taken from recommendations from Atmospherice and Oceanic Studies at UW-Madison)
- Use
implicit none
to require that all variables are explicitly declared. - Start integer variable names with one of these letters:
I,J,K,L,M,N
. For example, call a counter variableicount
instead ofcount
. - Start floating point variable name with a different letter.
- Follow naming conventions that are already in use in the existing code.
For example, in fluid dynamics they use u,v,w for wind components, and they have leap frog time differencing . Hence they name the components UP, VP, WP for u,v,w at the Past time level.
- It is unacceptable to name a variable after one's girlfriend or other none related word.
- Develop and document your coding style and stick to is so others can follow the flow. This is often more efficient and useful than verbose comments which quickly become invalid and even misleading as the program evolves.
Of course, FORTRAN like other languages has evolved over time. This means that there are several different versions of FORTRAN in use today. Two of the most common are FORTRAN 77 and FORTRAN 90/95. The numbers loosely refer to the years 1977 and 1990-1995 that these language standards were released to the public.
Since we can not possibily teach you an entire programming language like FORTRAN in one week, we will focus on some simple examples in an attempt to illustrate the main program structure, simple I/O, file I/O, and some common control structures. The main structure is similar for all FORTRAN programs, but different compilers may have specific instructions that may not be standard across platforms.
FORTRAN programs start with the word "PROGRAM" and end with the word "END". That's pretty easy so far. However, both statements must start at position 7 of their respective lines. Here's a FORTRAN program that reads in "age" as an integer and computes the number of dog years for that age. The lines that start with "c" are comment lines used to provide information about the program. The last two comment lines help the programmer keep track of the character count so that statements start at position 7 and do not exceed 72 characters. The next line is the first line of the program and it indicates the start of the program code and that this program is named "myProgram". The last line indicates the "end" of this program. FORTRAN is not case-sensitive and actually treats all commands as if they were uppercase.
c c myProgram: A FORTRAN program that doesn't do much (yet) c Author(s): Becky Badger, Bucky Badger c Date: February 1, 2009 c c 1 2 3 4 5 6 7 c23456789012345678901234567890123456789012345678901234567890123456789012 PROGRAM myProgram INTEGER age WRITE (*,*) 'How old are you? ' READ (*,*) age WRITE (*,*) 'Hello Programmer! You are ', age*7, ' in dog years!' END
Be sure to see this web site, http://www.stanford.edu/class/me200c/tutorial_77/, for lots more detail about FORTRAN 77.