365
96.4
3.14159
and then read the numbers as streams of characters. However in our minds we can think of each number as a numeric value rather than a series of characters. This is similar to the way a computer processes numeric data.
A computer programmer writing a program to manipulate numeric values uniquely
identifies each value by a name which refers to a discrete object remembered in the computer's memory. Thus the previous values may be identified by names such as:
daysinyear
temperature
pi
Note that it is good programming practice to use names that relate to the value that is being referred to.
There are two main forms of numeric data, namely INTEGER and REAL. Integers are essentially a restricted set of the mathematical whole numbers and as such may only have discrete values (i.e. no fractional part). The following are valid integer values:
-3124 -96 0 10 365
Real numbers may have a fractional part and have a greater range of possible values. For example:
10.3 -8.45 0.00002
There are two forms in which real values may be written. The examples above are in fixed point form but floating point form (similar to scientific notation) may be used, for example:
2.576x1032 1.3 x 10-22
may be written in Fortran as
2.576E32 1.3E-22
where the E stands for `exponent' or `multiplied by 10 to the power of'.
The integer and real values listed above, when used in a computer program, are known as literal constants.
Why reals and integers? Integers are more accurate for discrete values and are processed faster but reals are necessary for many scientific calculations.
In addition to the basic real and integer numbers there are other types of number such as double precision (which have more significant figures than the default REAL type) and complex numbers (with a real and imaginary part).
As well as numbers, Fortran programs often require other types of data. Single letters, words and phrases may be represented by the CHARACTER data type, while the logical values `true' and `false' are represented by the LOGICAL data type (details later).
Unlike some programming languages in which certain words are reserved and may only be used by the programmer in precisely defined contexts Fortran has no reserved words. The programmer should take great care when naming objects not to use any words which form part of the language. In the course notes all words which have a defined meaning in the Fortran languages are given in uppercase and the user defined objects are given in lowercase.
REAL :: temperature, pressure
INTEGER :: count, hours, minutes
declares five variables, two which have values that are real numbers and three that have integer values.
The variable declaration statement may also be used to assign an initial value to variables as they are declared. If an initial value is not assigned to a variable it should not be assumed to have any value until one is assigned using the assignment statement described below.
REAL :: temperature=96.4
INTEGER :: daysinyear=365, monthsinyear=12, weeksinyear=52
The general form of a variable declaration is:
TYPE [,attr] :: variable list
Where attr are optional Fortran 90 `commands' to further define the properties of variables. Attributes will be described throughout the course as they are introduced.
REAL, PARAMETER :: pi=3.141592
The word REAL defines the type of pi and the word PARAMETER is an attribute of the REAL object which is known as pi and has the value 3.141592. Parameters may also be defined for other data types, for example:
INTEGER, PARAMETER :: maxvalue=1024
INTEGER, PARAMETER :: repeatcount=1000
The objects declared to be parameters may not be altered in the program.
+ Addition
- Subtraction
* Multiplication
/ Division
** Exponentiation
For example
cost * number
cost * number + postage
10 + 3
4 * pi
1 / pressure
pi * radius * radius
The expressions formed with arithmetic operators may be used in a variety of ways, one of which, the assignment statement, is described in the next section.
The arithmetic expression may also include brackets which should be used to clarify the required sequence of operations in an expression. For example:
pi*radius**2
might be interpreted as
(pi*radius)**2
The section of the expression which appears inside brackets is always evaluated first. In expressions which contain more than one operator the operations are carried out in an order which is determined by what are known as the "rules of precedence". The following table lists the priority or order of execution of the various operators.
The operators are evaluated in order of ascending precedence, that is, brackets first, then ** followed by * / and finally + -. Operators of equal precedence are evaluated working from left to right across the expression.
area = pi*radius*radius
The assignment statement has the general form:
variable = expression
To read in a value to say, a variable called radius, the following statement would be suitable
READ(5,*)radius
READ(*,*) radius
and the value of the variable area would be displayed on the screen by the following statement
WRITE(6,*) area
WRITE(*,*) area
The characters "(5,*)" should appear after every READ and the characters "(6,*)" after every WRITE (note that "(*,*)" may appear with either the READ or WRITE statements). The significance of these will be explained in a later section.
Several variables (or expressions) may be specified on one READ or WRITE statement as follows:
READ(5,*) length, breadth
WRITE(6,*) temperature, pressure, mass
WRITE(*,*) pi*radius**2, 2.0
area = pi*radius*radius !Calculate the area of circle
Comments are also used to inhibit the action of statements that are used to output intermediate values when testing a program but which may be required again. The following statement is said to be commented out and is not executed.
! WRITE (6,*) temp, radius*radius
PROGRAM circle_area
IMPLICIT NONE
!reads a value representing the radius of a circle,
!then calculates and writes out the area of the circle.
REAL :: radius, area
REAL, PARAMETER :: pi=3.141592
READ (5,*) radius
area = pi*radius*radius
WRITE (6,*) area
END PROGRAM circle_area
There are a number of points to note in this program:
In general programs should be laid out with each statement on one line. However, there is an upper limit of 132 characters per line, (depending on the editor used it is often more convenient to keep to a maximum of 80 characters per line) a statement which exceeds the line limit may be continued on the next line by placing an ampersand & at the end of the line to be continued. The line should not be broken at an arbitrary point but at a sensible place.
WRITE (6,*) temp_value, pi*radius*radius, &
length, breadth
More than one statement may be placed on one line using a semicolon as a statement separator.
length=10.0; breadth=20.0; area= length*breadth
This is not recommended as it can lead to programs which are difficult to read - a statement may be overlooked.
radius
area
A programmer may define special data types, known as derived data types to create aggregate structures, thus a circle could be modelled as follows:
TYPE circle
INTEGER :: radius
REAL :: area
ENDTYPE circle
This would create a template which could be used to declare variables of this type
TYPE (circle) :: cir_a, cir_b
Just like the intrinsic data types, the components of a derived data type may be given an initial value. For example:
TYPE (circle) :: cir=circle(2,12.57)
The derived type is so named because it is derived from the intrinsic types, such as real and integer. However derived types may be used in the definition of other derived types. If a type, point, is defined
TYPE point
REAL :: x_coord, y_coord
ENDTYPE point
then the previously defined type, rectangle, could be modified to include a spacial position
TYPE circle
TYPE (point) :: centre
INTEGER :: radius
REAL :: area
ENDTYPE circle
The general form of a derived type definition is
TYPE type name
component definition statement
component definition statement
.....
END TYPE [type name]
This is a simplified version of the complete specification of a derived type, other elements may be added to this definition later. Note that the typename is optional on the ENDTYPE statement but should always be included to improve program clarity.
The general form of the variable declaration statement may be modified to included the specification of a derived type
TYPE [(type name)] [,attr] :: variable list
cir_a%radius = 10.0
cir_a%area = pi * cir_a%radius**2
If a derived type has an element which is a derived type then a component may be accessed as follows
cir_a%centre%x_coord = 5.0
cir_a%centre%y_coord = 6.0
0 1 1.2E-10 -1 -1.0
0.0 0.1 1024 64.0 -1.56E12
abignumber thedate A_HUGE_NUMBER
Time.minutes 10times Program
1066 X HELP!
f[t] no way another-number
REAL :: x=10.0 y=0.01, z=0.5
INTEGER :: i=10, j=25, k=3
i + j + k * i
z * x / 10 + k
z * k + z * j + z * i
i * y - k / x + j
(a) a point with x,y and z coordinates.
(b) a time in hours, minutes and seconds.
(c) a date in day, month and year.
(d) a time comprised of the two types above.
(e) a type containing 3 reals and 2 integers.