Introduction to FORTRAN programming ② : Writing rules

日本語

Fortran has the longest history as high level programming language. Because of that, to continue to meet current demand but at the same time stay backward compatible, there is an old style and a new style of writing in Fortran. This effect is similar to human language where “in the old days this is how we used to say this, but now it is different”.  Of course the compiler and the language continues to support source that has been written long time ago and it is possible to write new code with the old style, but I obviously recommend writing new code in the new style.

2 styles for writing

There are 2 styles of writing in Fortran, fixed format (old format) and free format ( new format ). I personally think that one really needs to remember the rules for both styles. It is true that remembering only the free format writing style is enough, however, if you program in Fortran, you will certainly see lots of source that were writing long time ago in the fixed format or you might have to use/maintain old source code.  I will first outline the rules for the free format style and finally outline the ones for the fixed format.

Free format

  • The language is not case-sensitive. Program, program, PROGRAM are all considered the same.
  • You can start writing from any column number.
  • [!] signify a comment. anything that is placed after [!] to the end of the line is considered a comment.
  • If the statement is to long to fit in one line, then [&] can be placed at the end line and another [&] should be placed at the beginning of the continuing line.
  • To write multiple statements on the same line, use the [;] in between the statements

Free format example 1

    ! you can start to write a statement from any column
         program main
      implicit none
            integer      i    ,    j,   k
      print      *, i,j,k
                                end program main

Free format example 2

! This line is a comment
      result = 100 / c    !Anything written here is a comment

Free format example 3

   ! If one line cannot fit the statement
   ! the & can be used at the end of the statement and
   ! at the beginning of the continuing line
      a = c + 100 &
      &+ 20

 

Free format example 4

    ! you can have multiple statement written on the same line by
    ! separating them using [;]
      a = a + 1; b = 5

Fixed format

punch_card

punch card example

Fortran was originally designed to be written on punch cards. For that reason, you can only write from column 1 to column 80 per line. A column or a collection of columns signify a meaning. This writing style is called fixed format. Here are the main rules for the fixed format style.

 

  • The line can be 72 characters long, only one statement per line is allowed.
  • Column 1 – 5 are reserved for writing line number. If, however, the first column has the character [C] in it then the line is a comment line.
  • If one statement  cannot fit in one line, then you can place a character in column 6 other that 0 or blank to identify that the line is a continuous line .
  • The statement must be written between column 7 and column 72
  • column 73-80 are reserved for sequential number entry. These number has no effect on the statement itself.
  • As with the free format, the format is not case-sensitive
  • An [END] keyword must be placed at the end of the program

Fixed format example

      PROGRAM MAIN
C  This line is a comment ( writing C in the first column makes
C  the line is comment
C  variable declaration and assignment
      REAL A,B
      A=1, B =2
C column 1 through 5 are reserved to place a line number
  100 CONTINUE
C  continues line example and a non case-sensitive
C  example
      A = a +
     &B/2
C  an 「END」 keyword must placed at the end of the program
      END


References
FORTRAN言語の仕様

http://www.edu.cc.uec.ac.jp/mce/c1-3m/fortran/basic/syosiki.htm

http://www.eq.db.shibaura-it.ac.jp/fortran/smpl.f

http://www.nag-j.co.jp/fortran/index.html

 

One thought on “Introduction to FORTRAN programming ② : Writing rules

Leave a comment