Fortran Lecture 5
Fortran Lecture 5
Versions/Standards
Fortran 66 - first standard
Fortran 77 - character handling
Fortran 90 - array and modular programming
Fortran 95 - functional programming
Fortran 2003 - object-oriented and generic programming
Fortran 2008 - concurrent programming.
Fortran 2015 - is under development
Fortran Program Structure without subprograms
[Declaration statements]
name (program name) must:
[Execution statements]
Begin with a letter
END [ PROGRAM name] (upper/lower case)
It contains letters, digits 0-9,
and underscore characters
Maximum of 31 characters.
names are case-insensitive.
Fortran Program Structure with internal subprograms
Subprogram(s)/procedures can
[PROGRAM name ] appears after the end of the main
program.
[IMPLICIT NONE]
Subprograms that are not contained
[Declaration statements] in the main program are called
external subprograms/procedures.
[Execution statements]
IMPLICIT NONE
When this statement is used in the program, any variable that does
not appear in an explicit type declaration statement is considered
error.
The decimal form consist of string of digits with exactly one decimal
point. e.g 1.4, -13.5, +0.04, 33333.5
Variables
Declaration of Variables
Variables Attributes
Various Fortran attributes may be specified for variables in their
type-declaration statement.
The PARAMETER attribute dictates that a constant is being
defined. A variable declared with this attribute may not have its
value changed within the program unit.
The PARAMETER attribute is used after the data type keyword,
followed by variable name followed by an = and then value for the
variable.
EXAMPLE :
REAL, PARAMETER :: PI = 3.143
! PI will be equal to 3.14 throughout the program
INTEGER, PARAMETER :: TOTAL = 10
! TOTAL will be equal to 10 throughout the program
Features of Fortran Program 10/14
Variable Initialization
Program Comments
Arithmetic Operator
variable = expression
where variable has been declared and therefore has a type
If the type of variable and expression are identical, the result is
saved to variable.
If the type of variable and expression are not identical, the result of
expression is converted to the type of variable.
1 INTEGER = REAL
The RHS is evaluated and then the value is truncated (all the
decimal places lopped off ) then assigned to the LHS.
2 REAL = INTEGER
The INTEGER is evaluated as REAL. The RHS expression is simply
stored (approximately) in the LHS.
Mixed Type Assignment - example 7/13
Relational Operator
Operator Meaning
< or .LT. Less than
> or .GT. Greater than
== or .EQ. Identically equal to
/ = or .NE. Not equal to
<= or .LE. Less than or equal to
>= or .GE. Greater than or equal to
Operators in Fortran 9/13
Logical Operator
Character Operator
Example:
’Man’ // ’chester’ gives ’Manchester’
CHARACTER(LEN = 7) :: word 1 =“missing”
CHARACTER(LEN = 3) :: word 2 = “you”
CHARACTER(LEN = 10) :: ans
ans = word 1 // word 2 ! missingyou
Operators in Fortran 11/13
Character substrings
Function Meaning
INT(x) Truncate a real number to an integer
NINT(x) Round a real number to the nearest integer
REAL(x) Convert to real
CEILING(x) Nearest integer greater than or equal to x
FLOOR(x) Nearest integer less than or equal to x
CMPLX(x) Real to complex
FRACTION(x) Fractional part of real
Write and Run fortran program 1/3