Anda di halaman 1dari 13

Chapter 2

FORTRAN
Introduction
The first phase in programming a problem to the digital computer is to decide on the
approach or method to be used in obtaining the solution. Since the digital computer
is capable of performing only arithmetic operations, problems that cannot be solved
by arithmetic procedures, when in their usual form, must be put in a form consistent
with such procedures. However, the closed-form solution of a differential equation,
for example, is not an arithmetic operation. An arithmetic procedure for solving
such an equation must be developed.
Once a method of solution has been selected, the problem must be programmed
to the computer as a defined step-by-step procedure. This is called an algorithm1 .
The second phase of programming is to establish a general outline of the steps
involved in the algorithm. One method is to write the steps in a numbered sequence.
More frequently, however, the algorithm is stated graphically by means of a flow
chart, in which the steps are displayed in the form of a block diagram. A flow
chart may show the required steps in considerable detail, or it may merely outline
the general procedure necessary to obtain a computer solution to the problem. In
general, excessively detailed procedure should be avoided in the flow chart, since its
primary purpose is merely to outline the overall process, which will be implemented
by the more specific steps appearing in the finished computer program.
The computer must be instructed very specifically for it to perform its operations. Each step must be initiated by an instruction. The instructions, which
the computer interprets and executes, must be transmitted to it in a coded form,
which it can interpret. Thus the third phase of programming is to write the specific program steps in coded form, following the outline provided by the flow chart.
1

An algorithm is a procedure that describes in an unambiguous manner a finite sequence of


steps to be performed in a specified order. The object of the algorithm is to implement a numerical
procedure to solve a problem or approximate a solution to the problem.

31

This procedure is called coding, and the resulting program is called a machinelanguage program, but fortunately, it is no longer necessary to code the steps
manually. The computer itself can be programmed to write the machine-language
program. The desired program is first written in one of the less-detailed and much
simpler languages, normally called procedure-oriented languages, or, as they are
sometimes called compiler languages, very similar to ordinary English and algebra. A program written in one of these languages must first be translated into
a machine language program by another machine language program. After it has
been translated the resulting machine language program (object program) is executed. To accomplish this translation, the computer must first have a specially
written machine-language program read into its memory. Such a special program
is called a translator, or processor, or a compiler. There are many languages
designed for different types of requirements. The FORTRAN (formula translating
system) processor, or compiler, is an example of such a special program, one of the
first of the procedure -oriented languages was developed by IBM Corporation for
its computers back in 50s. Engineers and scientists in handling problems involving
considerable amounts of numerical computation designed it primarily for use.

2.1
2.1.1

GENERAL STRUCTURE OF COMPILER


LEVEL LANGUAGE
The Elements of FORTRAN

The sentences of FORTRAN are called statements. The elements used to form
these statements include constants, variables, subscripted variables, operation symbols, expressions, and mathematical functions.

2.1.2

Constants

A constant is a fixed, unchanged quantity. There are three principal types of constants: (1) numerical, (2) logical, and (3) literal. Numerical constants may be
further subdivided into integers constants, real constants, and complex constants.
Both real and complex constants may be single precision or double precision.
2.1.2.1

Integer Constants

An integer constant is a number written without decimal point and can thus be only
a whole number. For example,
0

216

1
32

4321

2.1.2.2

Real Constants

Real constants are numbers written with a decimal point. There are two basic forms,
ordinary decimal form such as -983.245, and exponent form, which consist of the
use of a real constant followed by an E and either a positive or negative power of
10. For example,
1.57463E + 13
2.1.2.3

2.3567E 11

32E + 4

Complex Constants

An ordered pair of real constants represents complex constants. The two real constants are separated by a comma and enclosed in parentheses. The first real constant
is the real part of the complex constant, and the second is the imaginary part.
For example,
(5.71, 2.86) represents 5.71 2.86i
Both parts should be of the same precision.
2.1.2.4

Logical Constants

Logical constants specify the logical values true and false. When appearing in
a sourse program, they are written as
.TRUE. and .FALSE.

2.1.3

Variables

A FORTRAN variable is a symbolic name for a quantity that is stored in computer


memory. It consists of from one to six alphabetic and numeric (alphanumeric)
characters, the first of which must be alphabetic. Real and complex variables may
be either single-precision or double precision.
2.1.3.1

Integer Variables

The first character of an integer variable name is I, J, K, L, M, or N. A different


character than those listed may be used if the name appears in an INTEGER explicit
type specification statement, or if the variable is specified as integer by an IMPLICIT
type specification statement. If the variable is specified as integer by virtue of its first
character being I, J, L, M, or N, its type is said to be specified by the predefined
specification. For example, JOBNO is a valid integer variable name.
33

2.1.3.2

Real Variables

If a variable has its type specified by the predefined specification, its name will begin
with an alphabetic character other than I, J, K, L, M, or N. An example would be
VELOC

2.1.4

Arrays

A FORTRAN array is a set of variables identified by a single name. The different


elements are differentiated from one another by using subscripts with the array
name. Thus the array elements are subscripted variables. They may also be the
same modes (integer, real, and so forth) as nonsubscripted variables. The number
of elements in an array must be specified by giving the number of subscripts and the
maximum value of each subscript in a DIMENSION, COMMON, or explicit type
specification statement. For example,
DIMENSION A(50), B(30,30)
indicates that there is array named A having subscript of maximum value 50, and
another array named B with two subscripts, each having a maximum value of 30.
A(10) or B(10,20) would be particular elements of the respected arrays.

2.1.5

Arithmetic Expressions

The simplest arithmetic expression is a single constant, variable, or function. Combining constants, variables, and functions with arithmetic operators forms more
complicated expressions. These operators are
Operator

and /
+ and -

Operation
Exponentiation
Multiplication
Addition and subtraction

Hierarchy Level
1st
2nd
3rd

The hierarchy determines which of two consecutive operations is performed first.

2.1.6

Logical Expressions

A logical expression always has the value true or false and are used in Logical
IF statements. The simplest form of logical expression is a single logical constant, a
single logical variable, or a single logical function. More complicated logical expressions state conditions, which are either true or false. One such logical expression
is called a relational expression, which simply makes a declaration regarding the
relative magnitudes of two arithmetic expressions. These are: 34

Relational Operator

Definition

.GT.
.GE.
.EQ.
.NE.
.LT.
.LE.

Greater than
Greater than or equal to
Equal to
Not equal to
Less than
Less than or equal to

Relational operators may combine only single and double precision real and integer
arithmetic expressions. Combining relational expressions, logical constants, logical
functions, and logical variables with the logical operators .AND., .OR., and .NOT.
can form more complicated logical expressions. For example,
.NOT. X
is true if X is false, or false if X is true. The logical expression
X .AND. Y
is true if and only if both X and Y are true. The logical expression
X .OR. Y
is true if either X or Y or both are true,and false if both X and Y are false.
Still more complicated logical expressions involving several logical operators are
possible. Parentheses may be used to control the order in which operations are
performed in logical expressions. When parentheses are not used, a hierarchy determines the order of performing operations which is as follows:
Operation
Hierarchy
Evaluation of functions
1st

2nd
and /
3rd
+ and 4th
Application of relational operators
.GT., .LT., etc
5th
Application of .NOT.
6th
Application of .AND.
7th
Application of .OR.
8th

2.1.7

FORTRAN supplied Mathematical Functions

Certain mathematical functions are automatically available to the FORTRAN processor. There are two kinds of these supplied function subprograms. The first are
35

called intrinsic, built-in, or in-line functions. These requires only a few machinelanguage instructions, so these instructions are placed in the object program each
time the function name is used in the source functions. The second kind are called
external, library, or out-of-line functions. The preprogrammed procedures for
the functions are in a library, and only one set of machine-language instructions
would be placed in the object program, even though the function is used several
times.

Some commonly used out-of-line mathematical function subprograms are:


36

Function
Name
EXP
DEXP
CEXP
CDEXP
ALOG
DLOG
CLOG
CDLOG
ALOG10
DLOG10
ARSIN
DARSIN
ARCOS
DARCOS
ATAN
ATAN2
DTAN
DTAN2
SIN
DSIN
CSIN
CDSIN
COS
DCOS
CCOS
CDCOS
TAN
DTAN
COTAN
DCOTAN
SQRT
DSQRT
CSQRT
CDSQRT
TANH
DTANh
SINH
DSINH
COSH
DCOSH

No. of
arguments
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
2
1
2
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1

Function
Type
Real(4)
Real(8)
Complex(8)
Complex(16)
Real(4)
Real(8)
Complex(8)
Complex(16)
Real(4)
Real(8)
Real(4)
Real(8)
Real(4)
Real(8)
Real(4)
Real(4)
Real(8)
Real(8)
Real(4)
Real(8)
Complex(8)
Complex(16)
Real(4)
Real(8)
Complex(8)
Complex(16)
Real(4)
Real(8)
Real(4)
Real(8)
Real(4)
Real(8)
Complex(8)
Complex(16)
Real(4)
Real(8)
Real(4)
Real(8)
Real(4)
Real(8)
37

Argument
Type
Real(4)
Real(8)
Complex(8)
Complex(16)
Real(4)
Real(8)
Complex(8)
Complex(16)
Real(4)
Real(8)
Real(4)
Real(8)
Real(4)
Real(8)
Real(4)
Real(4)
Real(8)
Real(8)
Real(4)
Real(8)
Complex(8)
Complex(16)
Real(4)
Real(8)
Complex(8)
Complex(16)
Real(4)
Real(8)
Real(4)
Real(8)
Real(4)
Real(8)
Complex(8)
Complex(16)
Real(4)
Real(8)
Real(4)
Real(8)
Real(4)
Real(8)

Definition
(a is argument)
ea

loge (a)

log10 (a)
arc sin(a)
(in radians)
arc cos(a)
(in radians)
arc tan(a)
arc tan( aa21 )
arc tan(a)
arc tan( aa21 )
sin(a)
( a is in radians)

cos(a)
( a is in radians)
tan(a)
tan(a)
cot(a)
( a is in radians)

tanh(a)

sinh(a)
cosh(a)

Some commonly used in-line mathematical function subprogram are


Function
Name
ABS
IABS
DABS
AINT
INT
IDINT

No. of
arguments
1
1
1
1
1
1

Function
Type
Real(4)
Integer(4)
Real(8)
Real(4)
Integer(4)
Integer(4)

Argument
Type
Real(4)
Integer(4)
Real(8)
Real(4)
Real(4)
Real(8)

AMOD
MOD
DMOD
REAL

2
2
2
2

Real(4)
Integer(4)
Real(8)
Real(4)

Real(4)
Integer(4)
Real(8)
Complex(8)

AIMAG

Real(4)

Complex(8)

CMPLX
DCMPLX
CONJG
DCONJG

2
2
2
2

Complex(8)
Complex(16)
Complex(8)
Complex(16)

Real(4)
Real(8)
Complex(8)
Complex(16)

2.1.8

Definition
(a is argument)
|a|
Truncates arguments
to largest integer
having an absolute
value less than or
equal to argument.
Sign same as sign
of argument
Defined as a1 [ aa12 ]a2
where [ aa12 ] is truncated
value of the quotient
Obtains the real part of
a complex argument
Obtains the imaginary
part of complex argument
Expresses two real
arguments in complex form
Obtains conjugate of a
complex argument

Statements and Expressions

A FORTRAN source program consists of sequences of entities called statements;


these are of eight types.
Three executable statements
1. Arithmetic and logical assignment statements
2. Control statements
3. Input/output statements
and five non-executable statements or declarations
1. FORMAT statement

38

2. DATA initialization statements


3. Specification statements
4. Statement function definition statements
5. Subprogram statements
Executable Statements
These are commands, which give rise to one or more machine language instructions when they are translated by the compiler. The executable statements
hence they must appear in the particular order in which they are to be executed give the actual flow or sequencing of instructions in a program.
1. Arithmetic-Assignment Statements
The Arithmetic-Assignments statements consists of a single subscripted
or nonsubscripted variable to the left of an equal sign and arithmetic
expression on the right of the equal sign. In executing the statement, the
arithmetic expression is evaluated, and the resulting value is assigned to
the variable to the left of the equal sign. For example
X = (B + SQRT (B B 4. A C))/(2. A)
If X has a value prior to the execution of the statement, this value will be
replaced by the value calculated during the execution. In FORTRAN
the equality sign does not have the customary mathematical meaning of
equal to . Instead it means is to be replaced by. For example
J =J +2
is a perfectly valid arithmetic-assignment statement.
2.1.8.1

Declarations

Declarations, on the other hand, are not directly translated into machine language
instructions. Declarations provide information, which the compiler needs in order
to translate (or compile) the program unambiguously. Consequently, declarations
need not, at least in theory, appear in any particular order in a program. In all
languages executable statements can be labeled (i.e., given a name or sometimes a
number) so that one can refer to them in other statements. Declarations cannot be
39

usually be labeled; in FORTRAN, however, the format declarations specifying the


arrangements of data on input or output records is always labeled.
Of fundamental importance in all level languages is a unit of structure smaller
than the statement, called an expression.

2.1.9

Units of Structure

Every language defines a set of symbols, which are the only legal symbols in that
language. These symbols are used to represent operands and operators. The basic
operands are
Constants
Variables
Or values given by subprograms
The latter are programs, which can be called by name and used by another program.
Each language has rules, which define those combinations of symbols, which are
to be used to represent operands and operators. Operands and operators can be
combined, again in accordance with a set of rules, to form a larger unit called an
expression. There are at least two types of expressions, arithmetic expressions
and Boolean expressions.
a) Arithmetic Expressions
the arithmetic operands are numerical constants, variables, or numerical values
given by subprograms. A variable should be taught of as the name given to a
location in memory where the current value of the variable is stored. Arrays such as
vectors or matrices are treated as subscripted variables. The subscripts, which are
used to locate an element, are usually put in parentheses after the name of array.
The basic arithmetic operators are multiplication, division, addition, and
subtraction.
There are usually two basic sets of arithmetic operations in the computer, integer operations, which use integer mode operands and floating points operations for floating point operands. In most compilers if an integer mode operand
is combined with a floating-point operand in an arithmetic operation, provision is
made to convert the integer operand to a floating-point number and to carry out
the operation as a floating operation. An arithmetic expression is a meaningful or
legal string of arithmetic operands and operators.
b) Relations
Arithmetic expressions can serve as operands for a set of binary operators, the
relational operators such as greater than, less than, equal to, to form simple
logical or Boolean expression which take on only the values true or false.
c) Boolean Expressions
40

The simple Boolean expressions can be used as operands for another set of operators, the Boolean operators such as conjunction and disjunction to form compound Boolean expressions which again can assume only the values true and false.

2.1.10

The Basic Executable Statements

There are four basic statement types, which must appear in all languages: the
substitution, unconditional transfer, conditional, and input-output statements.
a) Substitution or Assignment Statement.
This is usually of the form
V =xx+yy
Where x, y, and V are the names of variables ( i.e., memory locations) and is the
symbol for the multiplication operator.
b) Unconditional Transfer
An unconditional transfer is a jump to another statement in the program, usually out of sequence. Typically it looks like one of the following:
GO TO n
TRANSFER TO n
JUMP TO n
where n is a label of another executable statement.
c) Conditional
Each language has at least one statement which determines the sequence of
execution of statements according to the outcome of some test. The test consists
of evaluating either a Boolean or an arithmetic expression. The value of the test
expression determines which statement is to be executed next. Thus a conditional
is a statement at which the sequence of execution can branch: The branch may be
two-way, three-way, or in some cases many-way.
d) Input-Output
The statements, which are used to read in data from a particular input unit or
to print results on a particular output unit. Accompanying each input or output
statement there will be format declarations, which describes the arrangement of
the data on the input record or the output record. The executable statements are
usually of the form READ, WRITE, PRINT in each case followed by an ordered
listing of the names of variables to be read in or print out and an identification of
the relevant format declaration.
These four types of statements are in fact all that are needed to program ant
computable problem.
41

e) Iteration Statements
Iteration statements allow one to repeat a sequence of statements an arbitrary
number of times.

2.1.11

Declarations

Although there are a number of types of declaration, the dimension, and format
declarations are most important:
a) Dimension
Provision must be made to reserve a block of storage of sufficient size for each
array and to indicate the number of subscripts and the span of each subscript.
b) Format Declaration
For each input and output statement the arrangement of the values of the variables on the input or output record as well as a specification of the type of encoding,
for example characters, integer number, floating point number, must be provided.
This is done in format declarations.

2.1.12

Subprogram Definition

Frequently it is necessary to carry out the same computations many times in a


program but with different data inputs. It is useful, therefore, to introduce such
sections as separate programs, subprograms, which can be called with one statement. Subprograms are called by giving the name of the subprogram followed by
the list of variables, in parentheses, which are to be transmitted to the subprogram
for the calculations; these are the arguments of the subprogram. Subprograms are
called subroutines and functions in FORTRAN.
42

2.2

Flow Charting

43

Anda mungkin juga menyukai