next up previous contents
Next: About this document ... Up: Physics 265: Introduction to Previous: 11.2 Text Editors: emacs,   Contents

12. Introduction to FORTRAN90/95

You fill find that there are many similarities between FORTRAN90/95 and MATLAB. In this section we will highlight several of the differences so that you can begin writing programs in FORTRAN90/95 right away. The first difference is that all FORTRAN90/95 programs must begin and end with the statements PROGRAM myprog (where myprog is the name of your program) and END PROGRAM. Note that FORTRAN90/95 is not case-sensitive so there is no difference between capitalizing and not capitalizing. My personal preference is not to capitalize at all since common text editiors such as emacs and vim perform syntax highlighting and use different colors for keywords, variables, etc. (much the same way the m-file editor for MATLAB does). Each FORTRAN90/95 program you write should also have the statement IMPLICIT NONE as the second line. This command forces the programmer to explicitly ``declare'' all the variables (and their types) that are to be used. MATLAB let us use implicit typing but this can be dangerous. With explicit typing, you must declare all your variables in the first part of your program before the first executable statement (DO, IF/THEN, etc.). For example, if you want to use the counter variables i and j in a couple of DO loops in your program, you would declare them as integer, scalar variables by including the declaration statement INTEGER i, j in the beginning of your program. To declare an array of 100 floating point values named $f$, use REAL, DIMENSION(100) :: f. Table 12.1 lists some common fortran keywords and a simple example of how each is used.
Table 12.1: Common keywords in FORTRAN90/95. Keywords are shown in capital letters. Arguments and variables are shown in lower case.
Statement Description Examples
Assignment assigns a value to a variable pi = 3.1415
statement   text = 'abcedf'
Block IF Branching conditional construct IF (x > 0.) THEN
construct   result = SQRT(x)
    ELSE IF (x==0.) THEN
    result = 0.
    result = SQRT(-x)
    END IF
CALL Calls a subroutine CALL numerical_sort(data)
CASE Branching among mutually SELECT CASE (j)
  exclusive choices CASE (1,2,5)
    block 1 commands
    CASE (-3:0)
    block 2 commands
    block 3 commands
CHARACTER Declares variables or named CHARACTER(len=10) :: string
  constants of character type  
CLOSE Closes a file CLOSE(10)
COMPLEX Declares variables or named constants COMPLEX xypair
  of complex type COMPLEX, DIMENSION(10) :: carray
CYCLE Branches to top of loop CYCLE
DO (counting A loop that repeats a block of DO j = 1, 10, 2
loop) construct statements a specified number of times write(*,*) ``Root is '', SQRT(j)
    END DO
DO (while A loop that repeats a block of DO
loop) construct statements until a condition is met IF (j==1) EXIT
    END DO
END FUNCTION Last statement of a function END FUNCTION myfunc
END MODULE Last statement of a module END MODULE mymod
END PROGRAM Last statement of a program END FUNCTION myprog
END SUBROUTINE Last statement of a subroutine END SUBROUTINE mysub
EXIT Branches out of a loop to the IF (val < 0.) EXIT
  first statement after the loop  
FORMAT Defines the format for 12 FORMAT('Int = ', I4)
  input or output of data WRITE(*,12) counter
FUNCTION Declares the beginning of a INTEGER FUNCTION factorial(n)
  user-defined function subprogram  
IF Executes or skips the associated IF (y < 0.) y = SQRT(-y)
  statement depending on the  
IMPLICIT NONE Forces explicit typing IMPLICIT NONE
INTEGER Declares variables or named INTEGER :: i, j, k
  constants of integer type INTEGER, DIMENSION(25) :: i
LOGICAL Declares variables or named LOGICAL test1
  constants of logical type LOGICAL, DIMENSION(25) :: flag
OPEN Opens a file OPEN(10)
PROGRAM Defines a program and its name PROGRAM my_prog
READ Reads in data READ(*,*) key_input
    READ (12, 50) a(1), a(2)
REAL Declares variables or named REAL fvalue
  constants of floating-point type REAL, DIMENSION(3,3) :: avecs
RETURN Returns control from a procedure RETURN
  to the routine that invoked the  
STOP Stops execution of the program STOP
SUBROUTINE Declares the beginning of a SUBROUTINE inverse(matrix)
  user-defined subroutine  
WHERE Masked array assignment WHERE (x > 0.)
    x = SQRT(x)
WRITE Writes data WRITE(*,*) screen_out
    WRITE(myfile,'(6F8.3)') vector(1:6)
    WRITE (12, 50) a(1:3)

next up previous contents
Next: About this document ... Up: Physics 265: Introduction to Previous: 11.2 Text Editors: emacs,   Contents
Gus Hart 2005-01-28