next up previous
Next: The first F77 program Up: Introduction to Fortran 77 Previous: Introduction to Fortran 77

F77 indentation rules

Each program written in F77 has to follow a strict column indentation rules which ensure that your source code will be correctly translated by the compiler. It means that you cannot simply write your text anywhere in the Emacs buffer you might desire, but instead the F77 standard tells you where and how a specific information has to be inserted. Here are the rules that you have to adopt when writing your F77 codes.

Column 1 of the source code designates a comment. If you place C (like comment), ! (exclamation mark) or any other character in column 1, the rest of this line is ignored. If you want to comment a line which contains some code, you can place the comment behind the instructions (see the example below). I advise you to always comment more complex parts of your code to explain the operations which follow. It is also important to maintain your comments up-to-date after changing any critical idea originally applied in your code. Remember, you are not the only person who may work with your program. Neatly written comments help tremendously not only you but also other people to understand what the code really does.

Examples of commented lines:

c  Comment... It does not matter if you use small or capital 'c'
!  I prefer to use an exclamation mark to designate a comment
      .....
      do i=2,number   ! this loop calculates the factorial 
         res = res*i
      enddo
      .....

Column 2-5 is reserved for placing a numerical label. The main idea is that once you place a label on a certain line, you can request an unconditional jump to this line from anywhere inside the same program, function or subroutine (more on these structures later).

Example of replacing the do-enddo loop in the example above by labeling:

      do 156 i=2,number   ! this loop calculates the factorial
 156  res = res*i

Column 6 is reserved for placing a character, usually +, which designates a continuation of the previous line. You will often encounter a situation in which you need to break a long line before column 73 and continue on the next line. Each continuation line must then consist in its column 6 the character +.

Example of the line break:

      write(*,'("Factorial of ",I3," is ",I5)') 
     +     number, res

Column 7-73 is the space into which you write your instruction code that has to be translated by your Fortran compiler. Why such limited width? Well, Fortran was born many years ago when the final stage of coding was always punching a card which contained binary instruction code for numerically controlled devices. The standard punched card had 80 columns, numbered 1 to 80 from left to right. Each column could hold one character, encoded as some combination of punches in the 12 rows of the card; more on this at http://www.cs.uiowa.edu/~jones/cards/codes.html. Although many modern compilers can read your code beyond the 73th character, we will strictly cut our source code to appear within column 7 and 73. If your line would extend beyond the 73th character, you have to break it and place the symbol + in column 6 of each continuation line.

Fortran code should be readable not only to you, but also to anyone who might come in contact with it. To write a nice code, we will frequently indent subordinate parts of our codes, e.g. the commands inside loops, to emphasize the whole structure. Bear in mind that F77 does not impose any requirements on the number of empty lines surrounded by the instruction code and also on the number of spaces between commands. Similarly, F77 does not distinguish between upper and lower characters and so you are free to choose your own style of writing Fortran codes. In contrast to older F77 programs which are often written in capital letters, I personally prefer to use lowercase letters for everything except the names of intrinsic functions and user-defined subprograms. Nevertheless, the choice is up to you !


next up previous
Next: The first F77 program Up: Introduction to Fortran 77 Previous: Introduction to Fortran 77
Roman Groger
2004-09-27