Fortran 90 allows manipulations of various types of data. The data or variable determines the manipulations, operations, or functions allowed to them. Fortran allows five basic data types, plus the ability to define your own types (Section 5.4: Derived Data Types). The available data types are:
TYPE :: var
Where TYPE is the data type (i.e. real, integer,
etc) and var is the variable name. Examples:
REAL :: temp ! temp is a real variable
CHARACTER :: answer, name
! answer and name are variables of the character type
INTEGER :: A = 4 ! A is integer type and initialized as the value 4
REAL
Real data can be represented in decimal form as exponents. Real constants written in decimal form must contain a decimal point. A real constant written as "43" is not valid as it is recognized as integer type. "43.", however, is acceptable. Exponential representation in Fortran involves either an integer or decimal number followed by an exponent that is written as an E with an integer following (i.e. 4.56E2).
The default precision for real data is typically about seven decimal digits and the range around -10-38 to 1038 (although this is machine dependent). Processors must provide an option to represent the data with more precision than the default. This is termed double precision. Techniques for checking and assuring the precision needed for an application can be seen in another section (2.5 Precision).
Example declaration:
REAL :: A, B, C
A, B and C are of real type.
INTEGER
Integers are positive or negative whole numbers (including 0) represented by a string of numbers not including commas or decimal points. Integers can be preceded by a negative (-) sign.
Example:
INTEGER :: A = 4, B
A and B are integer type and A has been given an initial
value of 4. B has not been given an initial value. Be careful
because some compilers will assign it a value.
COMPLEX
Fortran allows variables to be declared as complex numbers. Variables such as this are declared with their first value as the real component and the second as the imaginary. The following statements show an example of a complex variable declaration in a portion of a program. Remember that !'s are comment statements explaining the coding logic.
! Declare A as a complex variable
COMPLEX :: A
! Set the value of A to 4.2 -3.1i
A = (4.2, 3.1)
A has been specified as a complex variable and then it's
values set as real part = 4.2, imaginary part = 3.1.
CHARACTER
Character strings are a sequence of Fortran symbols enclosed within quotes or apostrophes (either quotes or apostrophes can be used but it must be the same at beginning and end). The number of characters in a sequence is the length. Blanks are counted as characters. By default, the length of any character string is one. To avoid this, a length should be specified.
CHARACTER (LEN = n) :: name
CHARACTER (n) :: name
In both cases name is of character type with length = n. If an apostrophe or quotes are desired within the string then the alternative should be used to enclose the string. As an alternative the desired symbol can be entered double. As an example:
word = "can't"
word = 'can''t'
Both of these declarations will produce the same character
definition, can't.
LOGICAL
Logical variables have one of two values: .TRUE. or .FALSE. Logical values can be displayed but are represented by only a T or and F, preceded by a space. the values can also be read but are only assigned according to the first letter represented.
For example:
LOGICAL :: A, B
! Ask the user for the values of A and B
PRINT *, "What are the values of A
and B?"
READ (*,*) A, B
Here A and B have been declared logical variables. The user is prompted for the values of A and B. If the input for those statements are
.TRUE, .Turtle
both A and B will be assigned the value of .TRUE.
Logical expressions can be used for selection and repetition.
A list of logical expressions can be seen in the next
chapter and A.1.4.
PARAMETER DECLARATION
Often, a certain constant appears so often in a program it should be declared as a parameter.
INTEGER, PARAMETER :: N = 10
REAL, PARAMETER :: pi = 3.141593
This allows one statement to be made at the beginning of the program setting a parameter value to a particular constant. Once a constant is declared a parameter it can no longer be changed. This prevents errors from occurring later where the parameter may be inadvertently changed. Also, using a declared constant throughout a program in lieu of the numerical value allows the parameter to be changed in only one line of code.
For example a program may contain statements calculating area throughout the code:
area = 3.141593 * r ** 2
If statements using 3.14593 occur frequently in the program it is better to declare a constant (i.e. pi) initially. This way the value is always the same. Repeated typing increases the chance of typographical errors. Also, parameterization creates one area at the beginning of your code where a parameter needs to be changed, rather than searching throughout the program for its many entries.