BASIC CONCEPTS OF OBJECT ORIENTED FORTRAN LANAGUAGES
THE OO FORTRAN CHARACTER SET
Every languages, whether it is a natural language such as English, or a computer language such
as Fortran, Java or c + +, has its own special alphabet. Only the characters in this alphabet may
be used with the language.
The special alphabet used with the Fortran language is known as Fortran character set. The
Fortran 95 character set consists of 86 symbols, and the Fortran 2003 character set consists of 97
characters.
Number of symbols Type Values
26 Uppercase letters A-Z
26 Lowercase letters a-z
10 Digits 0-9
1 Underscore character _
5 Arithmetic symbols +, -, *, /, **
17 Miscellaneous ( ) . = : ; & % $ ? > < “ ‘
symbols and blank
11 Additional Fortran \ [ ] ^ { } # and @
2003 symbols
NB: The uppercase letters of the alphabet are equivalent to the lowercase ones in the Fortran
character set. (For example, the uppercase letter A is equivalent to the lowercase a). In order
words, Fortran is case insensitive.
THE STRUCTURE OF A FORTRAN STATEMENT
A Fortran program consists of a series of statements designed to accomplish the goal of the
programmer. There are two basic types of statements;
Executable Statements: Executable statements describes the actions taken by the program when
it is executed (additions, subtractions, multiplications, divisions etc.)
Non-executable Statements: It provides information necessary for the proper operation of the
program.
Fortran statements may be entered anywhere on a line, and each line maybe up to 132 character
long. If a statement is too long to fit onto a single line, then it may be continued on the next line
by ending the current line (and optionally starting the next line) with the ampersand (&)
character. For example, the following three Fortran statements are identical:
Ex 1: Output = input 1 + input 2 ! Sum the inputs
Ex 2: Output = input 1 &
+ input 2 ! Sum the inputs
Ex 3: 999 output = input 1 & ! Sum the inputs
& + input 2
Each of the statement specifies that the computer should add two quantities stored in input 1 and
input 2 and save the result in output. A Fortran 95 statement can be continued for up to 40 lines.
(A Fortran 2003 statement can be continued for up to 256 lines). The last statement above starts
with a number, known as a statement label. A statement label can be any number between 1 and
99999. It is the “name” of a Fortran statement, and may be used to refer to the statement in other
parts of the program. If a statement label is used, it must be unique within a given program unit.
For example, if 100 is used as a statement label on one line, it cannot be used again as a
statement label on any other line in the same program unit.
Any character following an exclamation point are comments, and are ignored by the Fortran
compiler. In the third example above, the comment is ignored, so the ampersand is treated by the
compiler as the last character on the line.
THE STRUCTURE OF A FORTRAN PROGRAM
To start programming in Fortran, a knowledge of the syntax of the language is necessary. Each
Fortran program consists of a mixture of executable and non-executable statements, which must
occur in a specific order. An example of a Fortran program that reads in two numbers, multiplies
them together, and prints out the result.
A simple Fortran program
PROGRAM my_first_program
! Purpose
! To illustrate some of the basic features of a Fortran program.
! Declare the variables used in this program.
INTEGER :: i, j, k ! All variables are integers
! Get two values to store in variables I and j
WRITE (*, *) ` Enter the numbers to multiply: `
READ (*, *) i, j
! Multiply the numbers together
K=I*j
! Write out the result.
WRITE (*, *) ‘Result = ‘, k
STOP
END PROGRAM my_first_program
This Fortran program, like all Fortran program units, is divided into three sections:
i. The Declaration section: This section consists of a group of non-executable
statements at the beginning of the program that define the name of the program and
the number and types of variables referenced in the program.
ii. The execution section: This section consists of one or more statements describing
the actions to be performed by the program.
iii. The termination section: This section consists of a statement or statements stopping
the execution of the program and telling the compiler that the program is complete.
The termination section consists of the STOP and END PROGRAM statement. The
STOP statement is a statement that tells the computer to stop running the program.
The END PROGRAM statement is a statement that tells the compiler that there are no
more statements to be compiled in the program
NB: The comments may be inserted freely anywhere within, before, or after the program.
PROGRAM STYLE
This example program follows a commonly used Fortran convention of capitalizing keywords
such as PROGRAM, READ and WRITE, while using the lowercase for the program variables.
Names are written with underscores between the words, as in my_first_program above. It also
uses capital letters for named constants such as PI (π). This is not a Fortran requirement; the
program would have worked just as well if all capital letters or all lowercase letters were used
Since uppercase and lowercase letters are equivalent in Fortran, the program functions
identically in either case. Some programmers use other styles to write Fortran program. For
example, Java programmers who also work with Fortran might adopt a Java-like convention in
which keywords and names are in lowercase, with capital letters at the beginning of each word.
Such a programmer might give this program the name myFirstProgram. This is an equally valid
way to write a Fortran program. It is not necessary for you to follow any specific convention to
write a Fortran program, but you should always be consistent in all of your programming styles.
Compiling, Linking, and Executing the Fortran Program
Before the sample program can be run, it must be compiled into object code with Fortran
compiler, and then linked with a computer's system libraries to produce an executable program.
These two steps are usually done together in response to a single programmer command. The
details of compiling and linking are different for every compiler and operating system. Fortran
programs can be compiled, linked, and executed in one of two possible modes: batch and
interactive. In batch mode, a program is executed without an input from or interaction with a
user.
By contrast, a program that is run in interactive mode is compiled, linked, and executed while a
user is waiting at an input device such as the computer screen or a terminal. Since the program
executes with the human present, it can ask for input data.
Fortran program Object file Executable program
Creating an executable Fortran program involves two steps; compiling and linking. From the
user as it is executing, and display intermediate and final result as soon as they are computed.
CONSTANTS AND VARIABLES
A Fortran constant is a data object that is defined before a program is executed, and that does not
change value during the execution of the program. When a Fortran compiler encounters a
constant, it places the value of the constant in a known location in memory, and then references
that memory location whenever the constant is used in the program. A Fortran variable is a data
object that can change value during the execution of a program. (The value of a Fortran variable
may or may not be initialized before on is executed). When a Fortran compiler encounters a
variable, it reserves a on location in memory for the variable, and then references that memory
location whenever the variable is used in the program.
Each Fortran variable in a program unit must have a unique name. The variable name is a label
for a specific location in memory that is easy for humans to remember and use. Fortran 95 names
may be up to 31 characters long, and may contain any combination of alphabetic characters,
digits, and the underscore (_) character. However, the first character in a name must always be
alphabetic. The following examples are valid:
Time
Distance
Z123456789
I_want_to_go_home
NB: When writing a program, it is important to pick meaningful names for the variables.
Meaningful names makes a program much easier to read and to maintain. Names such as day,
month and year are quite clear even to a person seeing a program for the first time. Since space
cannot be used in Fortran variables names. Underscore characters is best advisable to use to
create a meaningful names.
There are five intrinsic or "built-in types of Fortran constants and variables. Three of them are
numeric (types INTEGER REAL, and COMPLEX), one is logical (type, LOGICAL), and one
consists of strings of characters (type CHARACTER). In addition to the intrinsic data types,
Fortran permits a programmer to define derived data types, which are special data types intended
to solve particular problems.
Integer Constants and Variables
The integer data type consists of integer constants and variables. This data type can store only
integer values, it cannot represent numbers with fractional parts. An integer constant is any
number that does not contain a decimal point. If a constant is positive, it may be written cither
with or without a + sign. No commas may be embedded within an integer constant. The
following examples are valid integer constants:
0
-999
123456789
+17
The following examples are nor valid integer constants:
1,000,000 (Embedded commas are illegal).
-100. (If it has a decimal point, it is not an integer constant).
An integer variable is a variable containing a value of the integer data. Constants and variables of
the integer data type are usually stored in a single word on a computer. Since the length of a
word varies from 16bits up to 64bits on different computers, the largest integer that can be stored
in a computer also varies.
Real Constants and Variables
The real data type consists of numbers stored in real or floating-point format. Unlike integers.
The real data type can represent numbers with fractional components. A real constant is a
constant written with a decimal point. It may be written with or without an exponent. If the
constant is positive, it may be written either with or without a + sign. No commas may be
embedded within a real constant. The following examples are valid real constants:
10.
-999.9
+1.0E – 3 (= 1. 0 x 10-3, or 0.001)
123.45E20 (= 123.45 x 1020, or 1.2345 x 1022)
A real variable is a variable containing a value of the real data type. A real value is stored in two
parts: the mantissa and the exponent. The number of bits allocated to the mantissa determines
the precision of the constant (that is, the number of significant digits to which the constant is
known), while the number of bits allocated lo the exponent determines the range of the constant
(that is, the largest and the smallest values that can be represented). For a given word size, the
more precise a real number is, the smaller its range is, and vice versa.
Character Constants and Variables
The character data type consists of strings of alphanumeric characters. A character constant is a
string of characters enclosed in single (') or double (“) quotes. The minimum number of
characters in a string is 1, while the maximum number of character in a string varies from
compiler to compiler. The characters between the two single or double quotes are said to be in a
character context. Any characters representable on a computer are legal in a character context,
not just the 86 characters forming the Fortran character set.
The following are valid character constants:
‘{^} ‘ These characters are legal in a character context even though they are not a part of
the Fortran character set.)
“3.141593” (This is a character string, not a number.)
Default and Explicit Variable Typing
When we look at a constant, it is easy to see whether it is of type INTEGER, REAL. Or
CHARACTER. If a number does not have a decimal point, it’s of type INTEGER; if it has a
decimal point, it is of type REAL. If the constant is enclosed in single or double quotes, it is of
type CHARACTER. There are two possible ways in which the type of a variable can be defined:
default typing and explicit typing. If the type of a variable is not explicitly specified in the
program, then default typing is used. Any variable names beginning with the letters I, J, K, L, M,
or N are assumed to be of type INTEGER. Any variable names starting with another letter are
assumed be of type REAL.