Basic Fortran
Basic Fortran
Meteorology 227
Fall 2018
FORTRAN Data Types
• INTEGER
• REAL
• COMPLEX
• CHARACTER
• LOGICAL
INTEGER
• Whole numbers (positive, negative, or zero)
• 33745.6E-2, 337456E-3
• 12,345 , 63
Character Strings or Strings
• Sequences of symbols from the FORTRAN
character set.
– ANSI standard character set (Table 2.1)
• Must be enclosed between double quotes or
between apostrophes (single quotes).
• Length = number of characters in string.
• “PDQ123-A” has a length=8
• “” has a length =
• ‘Don’t’ or “Don’t”
Identifiers
• Names used to IDENTIFY programs, constants,
and variables.
• Must begin with a letter, which may be followed
by up to 30 letters, digits, or underscores.
– R2-D2 ?
– 6Feet ?
• Use meaningful identifiers that suggest what
they represent.
• FORTRAN 90 is not case-sensitive
Variables
• Associated with memory locations.
• Variable names are identifiers and must follow the rules
for forming valid identifiers.
• Type statements
– The type of a FORTRAN variable determines the type of value
that may be assigned to the variable.
• Examples
– INTEGER :: Hours
– REAL :: Temp
– INTEGER :: Hour, Minute, Second
– REAL :: Temp, Dew_Point, Wet_Bulb
Variables cont.
• More examples
– CHARACTER(LEN=20) :: Name
– CHARACTER(20) :: Name
– CHARACTER :: First_Initial
• Naming Cautions
– Any variable whose type is not explicitly declared in a
type statement is subject to implicit naming
conventions.
• I,J,K,L,M,N → INTEGER
• All others → REAL
IMPLICIT NONE
• Implicit naming can cause problems!
– Mass = 12.345
• IMPLICIT NONE
– Should be used in every program (and module).
– All variables and constants must be specified
explicitly.
Variable Initialization/Parameters
• Initialization
– REAL :: Temp = 28.5, Dew_Point = 26.5
– REAL :: Temp = 28.5
– REAL :: Dew_Point = 26.5
• Parameters
– Type-specifier, PARAMETER :: List
– INTEGER, PARAMETER :: Base_Temp = 50
– REAL, PARAMETER :: Pi = 3.141593, TwoPi = 2.0*Pi
– CHARACTER(2), PARAMETER :: Units = “cm”
– CHARACTER, PARAMETER :: Units = “cm”
– How is this used? Examples
Operations and Functions
• Operators: +, -, *, /, **
• Numeric operations
– 3.0 + 4.0 = 7.0, 9.0/4.0 = 2.25
– 3 + 4 = 7, 9/4=2
• Substring
– Unit = “kilometer”
– Unit(5:7) = “met”
Assignment Statement
REAL :: XCoordinate, YCoordinate
INTEGER :: Number, Term
XCoordinate = 5.23
YCoordinate = SQRT(25.0)
Number = 17
Term = Number / 3+2
XCoordinate = 2.0 * XCoordinate
• Class example
• Specification
• Execution
• Subprogram
• Opening documentation
– Explains the purpose of the program
– Contains variable list
– Provides other information about the program
• INTEGER :: Loop
• Compile program
– GNU FORTRAN compiler (gfortran).
– Fix compile time errors.
• Execute Program
– Fix run time errors.
– Evaluate results, fix errors.
Practice
• Read the ‘Program Style and Design’ section on page 40
and the ‘Potential Problems’ section on page 41.