Anda di halaman 1dari 7

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)

Anda mungkin juga menyukai