[Santa Clara University]
Department of Mathematics
and Computer Science
[Return to CSCI 169 Homepage]

CSCI 169 Notes -- Fortran-90/95/2003/2008

Link given at bottom of this page to a Serbo-Croation translation of the text.


A. "Hello World!" in Fortran

The well-known sample program in Fortran-90/95/2003 can be written as follows:
	     WRITE(6,*) "Hello World!"

B. History

FORTRAN (an acronym for FORmula TRANslation) was developed at IBM facility in New York lead by a development team including John W. Backus (whose name is attached to a form now used to describe computer languages). For many years, Backus worked in the south San Jose IBM facility.
(A history of FORTRAN as given by John Backus may be found at http://www.softwarepreservation.org/projects/FORTRAN/paper/p25-backus.pdf . See pg 29 for the reference to the initial development of FORTRAN in New York.)
FORTRAN "0" report - 1954
FORTRAN I compiler - 1957
---> added subroutine definition and invocation
FORTRAN IV - developed 1960-62, definitive report in 1966
---> added logical IF, type declaration
special versions:
---> Univ. of Waterloo, Ontario -- WATFOR
---------> WATFIV (WATerloo Fortran IV)
---------> WATFIV-S (Structured -- Pascal influenced)
FORTRAN 77 (or FORTRAN 5) - 1977
---> Planned FORTRAN 88 eventually led to:
FORTRAN 90 - 1990
---> added recursion, pointers, modules, local routines
---> and numerous matrix features
FORTRAN 95 - 1995
---> added FORALL statement and other features
---> for parallel machines
FORTRAN 2003 - 2004
---> interoperability with C,
---> allocatable components of derived types,
---> object oriented programming features,
- - - > e.g., "class" variables (polymorphic variables)
---> function pointers,
---> command line arguments,
---> and numerous other improvements.
(For more detailed information on Fortran 2003, also see
John Reid, "An Overview of Fortran 2003," ACM Sigplan, Vol. 39:8 [Aug 2004], pp 31-38.
John Reid, "The New Features of Fortran 2003" [38 page PDF file].)
---> "Coarray Fortran"
---> DO CONCURRENT and BLOCK constructs.
---> some other additions and corrections.
(For more detailed information on Fortran 2008, see: John Reid, "The New Features of Fortran 2008" [26 page PDF file].)

See http://math.scu.edu/~dsmolars/ma60/notesfor2003.html for some additional information on Fortran 2003 and Chapter 19 of The Essentials of Fortran 90/95/2003 (http://math.scu.edu/~dsmolars/ma60/briefcont.html) (which also lists features of earlier versions of Fortran no longer part of Fortran-95 or Fortran-2003.

Fortran-90/95/2003/2008 is usually written without all the letter in UPPERCASE.

C. Language Development

Fortran is (probably) the only example of a widely-used, scientifically-oriented, computer language that has developed over almost 50 years. As issues related to programming languages have recommended new features for languages, new versions of Fortran have included them, while at the same time retaining the ability to run programs written in earlier versions.

For scientific work, Fortran compilers still regularly produce faster code and have a more extensive library of numerical routines than code written in other scientific languages.

See the Wikipedia entry for a good summary and indication of the current uses of Fortran at: http://en.wikipedia.org/wiki/Fortran

D. Preliminaries

There are currently two different styles used for Fortran source (program) code. In a given program, both styles may be used, but in any one electronic data file, the code should be written either in one style or the other. At SCU, on the Math/CS Department Linux server, math, and on many other systems/compilers, the distinction between styles is indicated by the FILE EXTENSION.
  1. Free Source Code Format:
    For FREE SOURCE CODE FORMAT programs ("new style" or Fortran-90/95/2003 only), there are no restrictions regarding placement of code on a line, except that a line cannot be more than 132 characters long.

  2. Fixed Source Code Format:
    For FIXED SOURCE CODE FORMAT (i.e., "old style") programs, each line has the following restrictions:
    (A) If there was a C or * in column 1, the card/line is considered a comment. The rest of the line is ignored. (One can also use the exclamation point as in Free Source Code Format if one compiles using a Fortran-90/95/2003 compiler.)


    Columns 1-5 are used for statement label number (usually blank).
    Column 6 is used ONLY to indicate this line is a continuation of the previous line (if a non-blank character is inserted).
    Columns 7-72 are for the FORTRAN statement.
    Columns 73-80 are ignored.
    IMPORTANT NOTE: In Fixed Source Code format, a common problem is using very long statements (and expressions) and forgetting about the column 72 "limit." ANYTHING which appears past column "72" on a line is ignored, even if it is the middle and end of a variable identifier! Thus it is possible that the end part of long statements will not be read by the compiler.

    I do not recommend using Fixed Source Code Format when one has the possibility of using Fortran-90/95/2003 Free Source Code Format. However, one should know the rules governing Fixed Source Code Format so that one can read older programs and conform to the older restrictions if there is no access to Fortran-90/95/2003. Thus, always use the extension .f90 (or .f95 if using a Fortran-95 compiler).

    In FIXED SOURCE CODE FORMAT, blanks are ignored by the compiler (except between quotes in output or literal strings), thus

    	e.g.,  NUMBER <==> N  UM  BE   R
    HOWEVER, blanks DO count as far as counting columns is concerned.

    NOTE: even if one uses the "old" Fixed Source Code format to write a program in a file, one can still use any new Fortran 90/95/2003 statements.

  3. Miscellaneous:

  4. Fortran Skeleton:
            PROGRAM identifier  ! This line can be omitted.
    	[variable declarations]
    	[SUBROUTINE and FUNCTION codes usually follow
    	in any order.]

E. Data

  1. Structures:
    Fortran has scalars and arrays (one uses PARENTHESES to enclose subscripts). Fortran-90/95/2003 has introduced records (i.e., "classes," which are called derived types) and pointers.

  2. Types:
    INTEGER, REAL, CHARACTER (LEN=...), LOGICAL, COMPLEX. Fortran-90/95/2003 has introduced an extended precision version of the standard types, thus superceding the older (and still permitted) DOUBLE PRECISION.

  3. Declaration:
    Before the first executable statement of any program segment (i.e., main program or subprogram), variables are (or can be) declared. Unlike C++, there can be no declarations in the body of a program segment, after the beginning of the executable code.

    Fortran has preserved default typing for non-declared variables! If the identifier begins with any letter between I and N (inclusive), the variable is considered to be if type INTEGER, otherwise it is considered to be REAL.

    One can "turn off" this default typing and force all variables to be declared explicitly by using

            IMPLICIT NONE
    immediately before any declaration statement and after the initial PROGRAM (or similar) line of the segment.
    --> Current style recommends always using IMPLICIT NONE to detect possible typos in identifier names!

    To declare variables explicitly, one lists them after the name of the type (and any "attribute" and an optional double colon) in a declaration statement, separating the identifers with commas (similar to C/C++ syntax). E.g.,

              INTEGER I, J, K
    	  REAL :: X, Y, Z
    	  CHARACTER (LEN=10) :: NAME1, NAME2
    The older style of declaration of character variables was as follows:
              CHARACTER*10 SURNAM
    To declare an array, one can indicate the maximum subscript value in parentheses (assuming the low value is 1) after the identifer name. Alternatively, one includes the attribute DIMENSION after a comma and the type name and before the double colon (for Fortran-90/95/2003). One can also specify an alternative starting value for the subscript by including low and high values together, separated by a colon. E.g.,
              REAL A(50), B(-10:10)
    Fortran-90/95/2003 permits designating an array as "ALLOCATABLE", indicating the number of dimensions by using a colon, and then allocating space using the ALLOCATE statement in the course of the program. One can later DEALLOCATE an array, if it is no longer needed.

  4. Initialization, Constant Declaration:
    One can initialize variables when declaring them. E.g.,
              REAL ::  A = 1.0
    	  INTEGER :: I = 2, J = 3
    Alternatively, one may use the older DATA statement to initialize variables declared explicitly or by default.
              DATA  A/2.7/, I,J/3,5/
    To specify an identifier as an unchangeable constant, Fortran-90/95/2003 style is to use the PARAMETER attribute when declaring the variable and assigning it a value. For example,
              REAL, PARAMETER :: PI = 3.1415926

  5. Storage/Scope Rules:
    For independent segments, all variables are local to the (sub-)program in which they are declared (explictly or implicitly). Unlike variables declared in the main program in Pascal, or variables declared outside any function in C/C++, there are no global variables in Fortran (unless one uses local subprograms or modules). Information is transferred to independent subprograms via parameters or modules (or via the older COMMON statement).

  6. Array Storage:
    Higher dimensioned arrays are stored by "column major" order. In other words, if we list the elements of an array in the order they were stored, the first subscripts would vary the fastest. NOTE: this is the reverse of the storage scheme used in C/C++ and Pascal, which is usually called "row major" order. The storage scheme has implications when the programmer allows the language to choose its defaults, e.g., for array input/output, or when linking a Fortran program segment with a C function.

    For example, suppose we have the array A declared as INTEGER A(3,3). The elements are stored in this order:

    A(1,1), A(2,1), A(3,1), A(1,2), A(2,2), A(3,2), A(1,3), A(2,3), A(3,3).

  7. Derived Type:
    Fortran-90/95/203 includes the ability for a user to define "record" variable types (similar to C++ classes or structs). In Fortran-90/95/2003 terminology, these are called "derived types." One begins by using the keyword TYPE followed by the user-chosen name of the type and then on separate lines, the components, as in the following example.

            TYPE INFOLINE
    	   CHARACTER (LEN = 30) :: NAME
    	   CHARACTER (LEN = 50) :: ADDRESS
    	   INTEGER :: ZIP
    To declare variables of the new type, one again uses the keyword TYPE, followed by the name of the derived type in parentheses. For example,
            TYPE (INFOLINE) :: ACCOUNT, LIST(100), TEMP

    In general, the principles are the same as with C++ or Pascal for record variables, except that instead of using the period (dot), . , as a subdivider, one uses the percent sign, %. For example,


F. Constants

  1. Numeric:
    INTEGERs are written without any decimal point. E.g., 5 or -2.

    REALs are written with a decimal point and (optionally) an exponent. E.g., 3.1415926 or 10.2E22

    DOUBLE PRECISION reals are written with a decimal point and an exponent with a D before the exponent. E.g., 1.0D00.

    Extended precision numeric constants can be written with an underscore and the identifier related to their precision. E.g., 1.0_DP (assuming DP has the appropriate value).

    COMPLEX numbers are written as a pair of reals separated by a comma, within parentheses. E.g., (5.2,6.721)

  2. Non-Numeric:
    LOGICAL constants are .TRUE. and .FALSE. They must be surrounded by periods!

    CHARACTER strings are enclosed in single (or double, for Fortran-90/95/2003) quotes, e.g., 'Hello'. In pre-Fortran-90, it was also permitted to use the "Hollerith" descriptor which was the letter H preceeded by the number of characters, e.g., 5HHello. (Note that the Hollerith descriptor was designated as an "obsolescent" feature and has been officially dropped in Fortran 2003.)

  3. Vectors:
    One can specify multiple constant values for vectors stored as a one dimensional array by enclosing the values with a set of double symbols, (/ and /). For example, it is possible to declare and initialize an array by using a vector of constants as an "array constructor" as follows:
                INTEGER, DIMENSION(5) :: C = (/1, 3, 4, 0, 4 /)

G. Operators

  1. Arithmetic:
    The operators are +, -, *, /, and ** (for exponentiation).
    Precedence: ** take precedence over * and / which take precedence over + and -.
    Evaluation: left to right except for repeated **.
    Thus, e.g., 2**3**5 is equivalent to 2**(3**5).
    Parentheses may be used as in algebra to supercede normal precedence.
    NOTE: As with C/C++ (and unlike Pascal), / is used both for integer and real division and takes its cue from the operands.
    If both operands are integer, an INTEGER division is performed.
    Thus, for example,
    5/2 evaluates to 2
    2/5 evaluates to 0
    5/2. evaluates to 2.5
    2.0/5 evaluates to 0.4

  2. Logical:
    Two or three letter codes are used surrounded by periods (as with the logical constants). Fortran-90/95/2003 also permits symbols.
    Relational Operators:
            F77     F90/95/2003  
    	.EQ.	==
    	.LT.	<
    	.LE.	<=
    	.GT.	>
    	.GE.	>=
    	.NE.	/= 
    Combinational Operators:
    	Fortran     C++
            .AND.       &&
    	.OR.        ||
    	.NOT.	    !
    As in C++, logical expressions MUST be surrounded by parentheses.

    The arithmetic operators have higher precedence than relational, which have higher precedence than .NOT. which has a higher precedence than .AND. which has a higher precedence than .OR.

  3. Character:
    Two (division) slashes, // (without any space), is used for string "concatenation," that is joining two character strings into one. E.g.,
    If B is declared as CHARACTER (LEN=7) and B = 'John' // 'son', then the value of B is Johnson

H. Control Structures

  1. Assignment:
    Assignment is indicated by = (without any colon as in Pascal), e.g.,
              A = 2 + 3
    Note: the left side must be a variable.
    The right side may be a variable, expression, or function.

    Fortran-90/95/2003 permits the use of the assignment and arithmetic operators for array variables as well. For example, if A, B, and C are all arrays of the same dimension, an assignment (and arithmetic operation) such as

              A = B + C
    is legal, and adds corresponding elements in arrays B and C and places the result in A.

    One can also multiply every element in an array by a constant in a similar manner, e.g., A = B * 3, or use a library function to operate on every element in an array, e.g., A = SQRT(B).

  2. Repetition:
    Fortran-77 only had a counted loop, but Fortran-90/95/2003 has introduced conditional and infinite loops as well. The Fortran counted loop is often called the DO loop because of the keyword DO.

    The older FORTRAN-77 style explicitly includes the statement number of the final statement of the DO loop. The final statement was often the CONTINUE statement, a statement which does absolutely nothing. As an example:

            DO 20 I = 1, 20
      20    CONTINUE
    In this example, the loop variable I starts at the value of 1 and finishes with the value of 20. Any legal statement number could be used to indicate the last statement of the loop.

    The Fortran-90/95/2003 style omits the statement label and uses an END DO as the final statement in the loop as this revised example shows:

            DO I = 1, 20
    	END DO
    To use a "stride" or "step," one adds another comma and number in the header line, for example:
            DO I = 1, 23, 2
    	END DO
    In this example, I starts at the value of 1 and increases by increments of 2 to 23. (To count "down," one may use a stride, e.g., -1.)

    As a matter of style, one should not use the older DO-loop when a compiler accepts the Fortran-90/95/2003 version of DO loops!

    In Fortran-66, a negative stride was not permitted, and the termination "test" was performed at the end of the loop. Thus, every DO-loop was performed at least once, even if the condition seemed otherwise inconsistent.

    With Fortran-77 and Fortran-90/95/2003, the testing is performed at the beginning of the loop and so for appropriate start and stop loop values, the interior of the loop might never be performed.

    Although Fortran-77 introduced the possibility of real variables and values as loop boundaries, this is now scheduled for obsolescence.

    Using the following structure,

    	END DO
    forces an infinite loop. Though not too useful in itself, it can be helpful if used with an EXIT clause.

    The EXIT statement is used with a one-line IF (see next section) and forces a termination of a loop. Its form is exemplified as follows:

            IF ( .NOT. (TOT <= 100) ) EXIT
    This would end the looping and continue the program with the statement after the END DO.

    We can use CYCLE in a similar way to skip the rest of the statements in the loop but continue the loop process itself.

    Fortran-90/95/2003 also includes the DO WHILE loop exemplified as follows:

            DO WHILE ( condition )
    	END DO
    As long as the condition is true, the loop will continue its operation, but when the condition turns false, the loop is exited.

  3. Simple Choice (IF statement):
    The IF structure ("block IF") performs the same way in Fortran as in C++, but in Fortran, one uses an ENDIF to conclude the statement and the keyword THEN after the condition. No braces are needed for either section to group the statement, either. For example,
            IF (A < B) THEN
    	END IF
    NOTE that the concluding END IF can be spelled as one word or two on most systems. NOTE also that the IF...THEN, ELSE, ENDIF must be on separate lines from the statements they govern.

    The ELSE clause may be omitted, for example,

            IF (A == B) THEN
    	END IF
    One can include several ELSE clauses in one IF if one uses the following form in which an ELSE IF is placed on a single line:
            IF (A >= B) THEN
    	ELSE IF (A > C) THEN
    	END IF
    In place of the THEN, one can put a simple statement to condense an IF statement to one line. For example,
            IF (A <= 0 ) A = -A
    This can be used in Fortran-90/95/2003 to EXIT out of an infinite loop (as the example above demonstrated).

    Older versions of Fortran include an "arithmetic IF" and an "(two-value) logical IF." Although still supported, they are not recommended for use, since they function by means of implicit "GO TO" statements, whose use is strongly discouraged by modern programming style.

  4. Multiple Choice (SELECT CASE statment):
    Fortran-90/95/2003 has included a new statement corresponding to the switch statement of C++ (or the case of Pascal). Each option is a distinct alternative (as in Pascal) and, thus, there is no need for anything like the C++ break command. As an example,
            SELECT CASE (IVAR)
    	   CASE (1)
    	      CALL SUB1
    	   CASE (2:4)
    	      CALL SUB2
    	      CALL SUB3
    (1) IVAR in parentheses after SELECT CASE is the variable whose value will be tested against options listed in the alternatives;
    (2) the second alternative shows how to list a range of consecutive values;
    (3) each option is a mutually exclusive alternative (unlike C++).

  5. END Statements:
    Each (sub)program segment must include an END statement as its last physical statement. This indicates the end of the code for that program segment. Fortran-90/95/2003 permits the optional addition of the segment type (e.g., PROGRAM, SUBROUTINE, FUNCTION, or MODULE) and the segment's name (e.g., END PROGRAM TEST).

  6. Terminating Program Segments:
    STOP and RETURN are statements that indicate the LOGICAL end (rather than the physical end) of the program segment. Program segments may have more than one STOP or RETURN statement if there was more than one "terminal" point of the code.

    STOP is the last executable statment in the main program and will actually stop the program (similar to the exit call in C/C++).

    RETURN is the last executable statment in the subprogram and will causes the subprogram to return back to the point from which it was invoked.

    Usually, either statment comes, physically, right before the END statement, though not of necessity (depending on the logical flow of the program).

    Fortran-90/95/2003 permits omitting the STOP or RETURN statement when it would occur immediately before the END.

    A subprogram may also include a STOP statement if the desired effect is to cause an immediate termination of the program without returning to any calling point.

I. I/O -- Input/Output

In standard Fortran, Input and Output commands have two parts:
  1. FORMAT Statements:
    The FORMAT statement begins with a statement number, includes the keyword FORMAT and is followed by a set of parentheses containing one or more field descriptors. E.g.,
         102     FORMAT(1X,I5,F10.3)

  2. READ Statements:
    The READ statement includes the keyword READ, followed by a set of parentheses containing pertinent information, followed by a list of variables (separated by commas). Thus, in general, we have
    	READ(idevice,iformat) var_list

  3. WRITE Statements:
    The WRITE statement includes the keyword WRITE, followed by a set of parentheses containing pertinent information, followed by a list of variables (separated by commas). Thus, in general, we have
    	WRITE(idevice,iformat) var_list

  4. File I/O:
    At SCU, on the Math/CS Department Linux Server math, any number other than 5, 6, or 7 can be used as device numbers to associate input/output with data files in the user's directory. The number 20 would be associated with data file ftn20 and other numbers would change the last two digits in the file name.

    Fortran also includes specific statements to OPEN and CLOSE data files by name and associate them with specific device numbers.

  5. Carriage Control:
    On OUTPUT FORMATS, the first character that WOULD be printed is not printed but is "eaten up" by the printer (and, on some systems, also the monitor) and is interpreted to determine how the rest of the line should be printed. This feature has been dropped with Fortran 2003.

    For ease of understanding, it may be best to think that an output line begins with column 0 or column "cc" (carriage control) and the first VISIBLE column is the next column, i.e., column 1. Whatever appears in column 0 is not printed but is interpreted as a printer command.

    blank the rest of the line is printed on the same line
    1 a "top-of-form" (page eject) is performed before the remaining information is printed (ignoring column 0 on the top of the next page).
    0 "double space," i.e., a line is skipped before the rest of the line is printed (ignoring column 0 on the next line).
    + the previous line is overstruck (double printing).
    To avoid unintended things from happening on output, common practice is to begin output FORMAT statements with a 1X or ' ' as the first field descriptor.

    NOTE: Most other characters are ignored (i.e., are intepreted as if they were a blank), although certain implementations allow other possibilities.

    Note that Unix provides the system command asa to convert files with Fortran carriage controls to other formats. See the on-line documentation for more information.

  6. Sample Program:
            PROGRAM TEST2
            100 FORMAT(7X, 'Miles', 3X, 'Kilometers')
    	DO NUM = 5,60,5
    	   MILES = NUM
    	   KILOS = MILES*8.0/5.0
    	   WRITE(6,'(2F10.1)') MILES, KILOS
    	END DO

J. Subprograms

  1. Library Functions:
    There are numerous library functions available which can be invoked in any program without need for any preliminary "include-like" statements such as in C/C++.

  2. User-Defined Functions:
    One writes a separate program segment, placing in the program file after the main program. This makes the function, in a sense, "global" and able to be invoked by any other segment. Alternatively, one can include a "local" function within a main program right before the END statement if one inserts a CONTAINS immediately before the subprogram code.

    The first line of a function indicates the data-type of the return value of the function, followed by the keyword FUNCTION, followed by the function name, followed by the argument list in parentheses. For example,

                REAL FUNCTION ROOT(A,B,C)

  3. Subroutines:
    As with user-defined functions, one can either write a subroutine (corresponding to a void function in C/C++) as a separate program segment or include it as a local routine within the calling program.

    The first line of a subroutine starts with the keyword SUBROUTINE, followed by the subroutine name, followed by the argument list in parentheses. For example,

                SUBROUTINE ROOTS(A,B,C,X1,X2)
  4. Notes:
    1. REMEMBER: In user-defined external (or "global") routines, all variables are local. Information must be conveyed through arguments/parameters or via MODULEs or COMMON statements.
    2. Subprogram codes come after main program code or (if local), before the end of the main program, after a CONTAINS statement.
    3. REMEMBER: FUNCTIONs return a value. Therefore they must be used (e.g., assigned to a variable). Cf. the way the library math functions (e.g., sqrt, sin) are used.
    4. All Fortran parameters can change their values. Fortran does not have the equivalent of C/C++ value parameters. (Arrays are always passed "by address/reference." Depending on the compiler, scalars are passed either "by address/reference" or "by value-result." More elsewhere.)
    5. Fortran subprogram parameters could be function names themselves. Thus, for example, one could write a subprogram to compute the approximate derivative of an arbitrary function, and pass the actual function into this subprogram as one of the parameters.

  5. Sample Program:
    	REAL :: A,B,C,X1,X2
    	READ(5,*) A,B,C
    	CALL ROOTS(A,B,C,X1,X2)
    	WRITE(6,10) A,B,C
    	10 FORMAT(1X, 'The roots of', F10.2,'x**2 + ', &
    	          F10.2,'x + ',F10.2,' are ')
            WRITE(6,11) X1,X2
    	11 FORMAT(1X,F10.5,' AND ',F10.5)
    	REAL :: A,B,C,XP,XN,RAD
    	RAD = SQRT(B**2 - 4.0*A*C)
    	XP = (-B+RAD)/(2.0*A)
    	XN = (-B-RAD)/(2.0*A)

  6. Modules:
    Fortran-90/95/2003 has introduced a new program segment called a MODULE. It can contain type definitions, variable declarations, and even specialized routines. It is most useful when several subprogram segments make use of the same user-defined data-types, or a series of variables or suite of functions related to a specific data-type. Since elements in a module can be designated as PUBLIC or PRIVATE, a module can be used in a way similar to a C++ class for object-oriented programming practices.

    A simple example of a module containing data is the following:

          MODULE SAMPLE1
    	   INTEGER, DIMENSION (10) :: K
          END MODULE 
          PROGRAM TEST8
             USE SAMPLE1
          INTEGER I
          CALL SUB1
          DO I = 1,10
    	WRITE(6,*) K(I)
          END DO
             USE SAMPLE1
          INTEGER I
          DO I = 1, 10
    	   K(I) = 21-2*I
          END DO
    The following is an example of a module definition for a stack, with the data being designated as PRIVATE and the member functions for a stack being coded as local module routines.
            MODULE STACK_MOD
              TYPE STACK
                  INTEGER :: STACKELEMENT(100)
                  INTEGER :: TOP
               END TYPE
              SUBROUTINE INIT(S)
              TYPE(STACK) :: S
              S%TOP = 0
              END SUBROUTINE
              TYPE(STACK) :: S
    	  INTEGER :: ITEM
              IF (S%TOP < 100) THEN
                 S%STACKELEMENT(S%TOP) = ITEM
                 WRITE(*,*) "STACK OVERFLOW"
              TYPE(STACK) :: S
              IF (S%TOP > 0) THEN
                POP = S%STACKELEMENT(S%TOP)
                S%TOP = S%TOP-1
                WRITE(*,*) "STACK UNDERFLOW"
              END FUNCTION
            END MODULE

K. Pointers

Pointer variables have been introduced in Fortran-90/95/2003 and they are similar, but not identical, to the concept of pointers in other languages that implement pointers. In other languages, pointer identifiers commonly store an address (rather than an integer, real, character, or other type of value). In contrast, a pointer in Fortran-90/95/2003 is an alias, that is an alternative name, for the variable it points to. In practice, this means

One major difference in the ways "pointers" can be used in Fortran-90/95/2003 is that they can also be used to "point" to a section of a large data structure, for example, to a row or column of an array. In addition, they can be used as variables in their own right. Thus, the concept of a "pointer" variable in Fortran-90/95/2003 is much more versatile than similar variables in other programming languages.

  1. Declaring Simple Pointer Variables:
    To declare a pointer variable, one adds the attribute keyword POINTER in the variable declaration before the double colon, as follows:
             REAL, POINTER :: Y

    One also needs to identify those variables to which Y (or any other appropriate pointer) can point, and this is done by adding the attribute keyword TARGET in the variable declarations of such variables, as follows:

             REAL, TARGET :: X
  2. Assignment of Pointer Variables:
    Fortran-90/95/2003 has introduced a pointer assignment operator which is indicated by using two symbols together in this way: =>

    If X and Y are declared as in the previous section, and if one invokes the pointer assignment statement,

             Y => X
    then Y would, in effect, become an alias for X. In other words, the memory location associated with X is now also the memory location associated with Y. Thus if the value of X changes, so does the value of Y and vice versa, since both X and Y refer to one and the same memory location.

  3. Using Pointer Variables for Linked Structures:
    One can use Fortran-90/95/2003 pointer variables to create nodes for linked structures in a way similar to what is done in other languages. As an example, a list node can be defined as a new "derived" data type using a pointer variable as follows:
         TYPE NODE
            INTEGER              :: INFO
         END TYPE NODE

    After defining this new data type NODE, one can then declare variables to be pointers to elements of this data type in the usual way, for example:

    This statement would declare LIST and TEMP to be variables which could be used as pointers to memory sections of derived type NODE.

    Note, however, that after declaration, LIST and TEMP do not contain any information, nor do they "point" to any node. This is so because there have been no variables declared with a "target" pointer attribute and no assocation has taken place!

    To allocate memory for a node, one may make use of the library statement ALLOCATE, which is similar in function to the Pascal procedure new or the C++ function new when used with pointer variables. Invoking the following statement

    causes memory to be allocated for a derived variable of the type of which LIST is declared to be (i.e., in this case, of type NODE) and then the identifier LIST can be used to access the fields of the node.

    A pointer variable can be set or reset to "null" by invoking the library statement NULLIFY. (This has the same effect as using the Pascal nil or the C++ NULL in an assignment statement.) In Fortran-90/95/2003, this statement is used as in this example:

    Fortran-95 also permits LIST%NEXT => NULL()

L. Other Statements and Features

There are various other Fortran statements and constructs, some relatively new, others little used, or used only in certain implementations. Programmers should look in a book or manual for further information. Some older, but little used, statements are designated for "obsolescence" by Fortran-90/95/2003.

M. Fortran Style

Because of the lack of program flow structures in older versions of Fortran, and the undisciplined use of GO TO statements, often code written in an older style was difficult, if not impossible to read, and thus also to debug. The following example shows equivalent sections of code written in an older style (which still is legal Fortran code) and re-written using a Fortran-90/95/2003 SELECT CASE statement and a block IF statement.

An Unstructured Segment of Fortran (Old FORTRAN 66 Style)

        IF(K.EQ.1) GO TO 212
	IF(K.EQ.2) GO TO 18
	GO TO 106
  212	X(1) = 5.
        Y(K) = K+2.5
   32	IF(K*J.LE.4) GO TO 184
   30	L4 = K*J-K+1
        Y(L4) = 1.0
        X(L4) = Y(L4)
  210	IF(K-J.GE.7) GO TO 265
        GO TO 184
   18	X(K) = 6.
        Y(K) = K-1.5
	GO TO 32
  106   IF(K.NE.3) GO TO 10
        Y(K) = 0.0
        X(K) = Y(K)
	GO TO 23
  265	IF(K.GE.J+7) GO TO 10
        X(K-J+1) = 0.0
	Y(K-J+1) = 1.0
	GO TO 10
  184	IF(K*J.LE.4) GO TO 23
  180	L = K*J-2*K
        Y(L) = 0.0
	X(L) = Y(L)
   23	IF(K-J.GE.7) X(K-J+1) = 1.0
        IF(K*J.LE.4) GO TO 30
	GO TO 265
   10	IF(K-J.GE.7) Y(K-J+1) = 0.0

An Equivalent Segment of Fortran That is Structured (New Fortran-90/95/2003 Style)

        SELECT CASE (K)
	      X(1) = 5.0
	      Y(K) = K+2.5
	      X(K) = 6.
	      Y(K) = K-1.5
	      X(K) = 0.0
	      Y(K) = 0.0
	IF(K*J <= 4) THEN
	      L4 = K*J-K+1
	      X(L4) = 1.0
	      Y(L4) = 1.0
	      L = K*J-2*K
	      X(L) = 0.0
	      Y(L) = 0.0
	IF(K-J >= 7) THEN
	      X(K-J+1) = 1.0
	      Y(K-J+1) = 0.0
	      X(K-J+1) = 0.0
	      Y(K-J+1) = 1.0

N. Pragmatics

Fortran programs are compiled (not interpreted). Frequently, sections of code (e.g., subroutine libraries) are separately compiled and linked together before execution.

In older versions of Fortran, variable allocation was static. Fortran-90/95/2003 allows dynamic allocation of pointers and arrays. By default, all variables are local.

Because of the static variable allocation in older versions of Fortran, recursive subprogram calls did not exist.

O. Comments

  1. About 1985 it was estimated that there was over $40 billion of Fortran code in existence. Until recently all major number crunching software packages were written in Fortran. Fortran-90/95/2003 has features designed for array processing on parallel computers that other languages do not have.
  2. The Cray line of computers have compilers for Fortran-77, Fortran-90, C, and a version of C++.
  3. There is continued work on "High Performance Fortran (HPF)," a superset of Fortran geared for advanced, parallel processors. Code written in HPF usually has additional commands which begin with !HPF, which, when run using standard Fortran-90/95/2003 are ignored since they begin with the comment signal, the exclamation point.
  4. The ongoing development of Fortran has been prompted by developments in the abstract study and analysis of computer languages since the 1950s.

    Fortran's original informality of syntax led to concerns about completeness of specification, determination of legal expressions, resolutions of ambiguities.

    The development of compiler theory indicated that earlier versions of Fortran were disasters for code translation and thus improvements were desirable. This accounted for some of the new features in more recent versions of Fortran.

    Fortran was commonly blamed for a space disaster because of the "default type" declaration feature. (Although this "blame" is thought to be false nowadays.) The first U.S. Mariner I rocket, launched on July 22, 1962 from Cape Canaveral on the first mission to Venus, was destroyed at a cost of $18.5 million because it started veering toward populated areas. The blame was commonly placed on a mistake by a programmer. It was commonly said that instead of writing

            DO 3 I=1,3   ! comma between 1 and 3
    a programmer had written
            DO 3 I=1.3   ! period between 1 and 3
    The first statement is a legitimate initial statement for a counted loop, but the second statement is a legitimate assignment statement giving the REAL variable DO3I the value of 1.30.

    By using the END DO statement and Free Source Code Format, this error would now be detected during compilation.

This web page has been translated into Serbo-Croatian by Web Geeks.

This page is maintained by Dennis C. Smolarski, S.J. dsmolarski "at" scu.edu
Last updated: 28 September 2005. Minor update 31 March 2011. Additional updates, 24 May 2012; 18 Sept 2012; 19 Sept 2012; 25 Sept 2012. Correction as to where FORTRAN was initially developed: 7 March 2014.