Basic Elements of Fortran - PowerPoint PPT Presentation

1 / 75
About This Presentation
Title:

Basic Elements of Fortran

Description:

write(*,*) The potential energy on moon is', PE. end program example ... characters enclosed in single ( ) or double ( ' ) quotes. ... – PowerPoint PPT presentation

Number of Views:197
Avg rating:3.0/5.0
Slides: 76
Provided by: yjsh3
Category:

less

Transcript and Presenter's Notes

Title: Basic Elements of Fortran


1
Chapter 2
Basic Elements of Fortran
2
  • Engineers and scientists design and execute
    computer
  • programs to accomplish a goal.
  • The goal typically involves technical
    calculations that
  • would be too difficult or take too long to be
    performed
  • by hand.
  • Fortran is one of the computer languages commonly
  • used for these technical calculations.
  • This chapter introduces the basic elements of the
  • Fortran language. By the end of the chapter, you
    will
  • be able to write simple but functional Fortran
    programs.

3
The Fortran Character Set
  • Every language, whether it is a natural language,
  • such as English, or a computer language, such as
  • Fortran, Pascal, 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 90/95
  • language is known as the Fortran character set.
  • It consists of the 86 symbols shown in the Table.

4
Total of 86 symbols
5
  • Note that uppercase and lowercase letters of the
  • alphabet are equivalent in the Fortran character
    set.
  • (For example, the uppercase letter A is
    equivalent to
  • the lowercase letter a.
  • In other words, Fortran is case insensitive.
  • This behavior is in contrast with case-sensitive
  • languages, such as C, in which A and a are two
  • totally different characters.

6
equivalent
program example ! This program is to
! calculate the sum ! and average of two
! variables, a and b. real
a,b,sum,avg read , a,b sumab
avg(ab)/2. print , a,b,sum,avg end
program example
PROGRAM EXAMPLE ! This program is to
! calculate the sum ! and average of two
! variables, a and b. REAL
A,B,SUM,AVG READ , a,b SUMAB
AVG(Ab)/2. PRINT , A,B,SUM,AVG END
PROGRAM EXAMPLE
7
The Structure of a Fortran Program
  • Each Fortran program consists of a mixture of
  • executable and non-executable statements, which
  • must occur in a specific order.
  • The Fortran program is divided into three
    sections
  • 1. The declaration 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.
  • 2.The execution section consists of one or more
  • statements describing the actions to be
    performed
  • by the program.

8
  • 3. The termination section consists of a
    statement or
  • statements stopping the execution of the
    program
  • and telling the compiler that the program is
    complete.
  • Note that comments may be inserted freely
    anywhere
  • within, before, or after the program.

program example ! This program is to
calculate the summation ! and average of
two variables, a and b. real a,b,sum,avg
read , a,b sumab ! Calculate the
sum avg(ab)/2. ! Calculate the average
print , a,b,sum,avg end program example
9
program example ! This program is to
calculate the summation ! and average of
two variables, a and b. real a,b read
, a,b real sum,avg sumab !
Calculate the sum avg(ab)/2. ! Calculate
the average print , a,b,sum,avg end
program example
The declaration section must be placed at the
beginning of the program.
10
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 the
variables to multiply together. WRITE(,) Enter
the numbers to multiply (i, j) READ(,) i, j !
Multiply the numbers together k i j ! Write
out the result. WRITE(,) Result, k ! Finish
up. STOP END PROGRAM
Enter the numbers to multiply (i, j) 2,3 Result
6
11
The Declaration Section
  • The declaration section consists of the
    nonexecutable
  • 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.
  • Fortran program names may be up to 31 characters
  • long and contain any combination of alphabetic
    cha-
  • racters, digits, and the underscore ( _ )
    character.
  • The first character in a program name must always
    be
  • alphabetic.
  • ? my_first_program ? example_12
  • ? JohnMary ? hw-10
  • ? 99_program ? hwlt10gt

?
12
The Execution Section
  • The execution section consists of one or more
    executable
  • statements describing the actions to be
    performed by the
  • program.
  • Comments may be embedded anywhere throughout the
  • execution section.
  • There are four executable statements in the
    example
  • 1. WRITE(,) Enter the numbers to
    multiply (i, j)
  • 2. READ(,) i, j
  • ! Multiply the numbers together
  • 3. k i j
  • ! Write out the result.
  • 4. WRITE(,) Result, k

comments
13
The Termination Section
  • The termination section consists of the STOP and
  • END PROGRAM statements.
  • The STOP statement tells the computer to stop
    running
  • the program. The END PROGRAM statement tells the
  • compiler that there are no more statements to be
    com-
  • piled in the program.
  • When the STOP statement immediately precedes the
  • END PROGRAM statement, it is optional. The
    compiler
  • will automatically generate a STOP command when
    the
  • END PROGRAM statement is reached. The STOP
  • statement is therefore rarely used.

14
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 the
variables to multiply together. WRITE(,) Enter
the numbers to multiply READ(,) i, j !
Multiply the numbers together k i j ! Write
out the result. WRITE(,) Result, k ! Finish
up. STOP END PROGRAM
optional
15
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
  • program uses the constant.
  • A Fortran variable is a data object that can
    change value
  • during the execution of a program.

16
program example ! This program is to
calculate the potential ! energy of an
object with mass m and at ! height h.
real m, h, PE real, parameter g9.8
write(,) Input the mass and height
read , m, h PEmgh ! Calculate the
potential energy write(,) The
potential energy is, PE end program example
constant
variable
Are there any more variables in the program?
17
program example ! This program is to
calculate the potential ! energy of an
object with mass m and at ! height h.
real m, h, PE real, parameter g9.8
write(,) Input the mass and height
read , m, h PEmgh ! Calculate the
potential energy on earth write(,) The
potential energy on earth is, PE
!-------------------------------------------------
----------------------- gg/6. PEmgh
! Calculate the potential energy on moon
write(,) The potential energy on moon is,
PE !-----------------------------------------
------------------------------- end program
example
18
  • When a Fortran compiler encounters a variable, it
  • reserves a known location in memory for the
    variable
  • and then references that memory location
    whenever
  • the program uses the variable.
  • Each Fortran variable in a program unit must have
    a
  • unique name.
  • Fortran names may be up to 31 characters long and
  • may contain any combination of alphabetic
    characters,
  • digits, and underscore ( _ ) character. However,
    the
  • first character in a name must always be
    alphabetic.

19
  • The following examples are valid variable names
  • time
  • distance
  • z123456789
  • I_want_to_go_home
  • The following examples are invalid variable
    names
  • this_is_a_very_long_variable_name
  • 3_days
  • A
  • number-11

20
  • You should always include a data dictionary in
    the header
  • of the program. A data dictionary lists the
    definition of each
  • variable used in a program. The definition
    should include
  • both a description of the contents of the item
    and the units
  • in which it is measured.
  • Program example
  • !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
    !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  • ! distance (meter) distance traveled by the
    car from !
  • ! location a to
    location b. !
  • ! velocity (meter/second) the average velocity
    of the car !
  • ! driven by the
    student. !
  • !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
    !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  • Real distance, velocity

21
  • Fortran has five intrinsic or build in types of
  • constants and variables.
  • ?Three of them are numeric (type INTEGER,
  • REAL, and COMPLEX)
  • ? one is logical (type LOGICAL), and
  • ? one consists of strings of characters
  • (type CHARACTER).
  • The simplest forms of the INTEGER, REAL,
  • CHARACTER, and LOGICAL data types are
  • discussed in this chapter.

22
Integer Constants and Variables
  • The integer 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 either with or without a plus sign.
  • ?? 0
  • ? -999
  • ? 1234567
  • ? 17
  • ? 1,000,000
  • ? -100.

23
  • An integer variable is a variable containing a
    value of
  • the integer data type.
  • 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 16 to 64 bits
    on
  • different computers, the largest integer that
    can be
  • stored in a computer also varies.
  • Smallest Integer Value -2n-1
  • Largest Integer Value 2n-1 -1
  • Attempts to use an integer larger than the
    largest possi-
  • ble value or smaller than the smallest possible
    value
  • result in an error called an overflow condition.

24
integer i, j, k, m, n, o integer, parameter
c12 real, parameter c214.5 read (,)
i, j k c1 i m i j n c2 o n / i
write (,) i, j, k, m, n, o
integer constant
real constant
If i2 and j7, then
_ _ _ _ _ _
25
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.
  • Real constants may be written with or without an
    expo-
  • nent. If used, the exponent consists of the
    letter E followed
  • by a positive or negative integer that
    corresponds to the
  • power of 10 used when the number is written in
    scientific
  • notation.
  • The mantissa of the number should contain a
    decimal
  • point.

26
  • The following examples are valid real constants
  • 10.
  • -999.9
  • 1.0E-3 ( 1.0?10-3, or 0.001 )
  • 123.45E20 ( 123.45 ?1020, or
    1.2345?1022 )
  • 0.12E1 ( 0.12?101, or 1.2 )
  • The following examples are not valid real
    constants
  • 1,000,000. (Embedded commas are illegal.)
  • 111E3 ( A decimal point is required in the
    mantissa.)
  • -12.0E1.5 (Decimal points are not allowed in
    exponents.)

27
The real data type stores numbers in a type of
scien- tific notation.
exponent
decimal point
mantissa
range
precision
28
  • 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 (the
    number of
  • significant digits), while the number of bits
    allocated to
  • the exponent determines the range of the
    constant (i.e.
  • the largest and 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.

29
Total number of bits
30
real a, b, c, d, e, f real, parameter
c12.0, c214.5 read (,) a, b c c1 a d
a b e c2 f e / a write (,) a, b, c, d,
e, f
real constant
If a2. and b7., then
_ _ _ _ _ _
31
Character Constants and Variables
  • The character data type consists of strings of
    alpha-
  • numeric characters. A character constant is a
    string of
  • characters enclosed in single ( ) or double (
    ) quotes.
  • The minimum number of characters in a string is
    one,
  • while the maximum number of characters in a
    string
  • varies from compiler to compiler.
  • The characters between the two single or double
    quotes
  • are 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.

32
character (9) student1, student2 character
(9) student character (31), parameter
noThe ID number of the student is
student1B95504001 student2B95504002
write(,) no, student1 write(,) no, student2
student2student1 write(,) student1, student2
The ID number of the student is B95504001 The
ID number of the student is B95504002 B95504001
B95504001
33
character (9) word1, word2 character (18)
word word1good word2morning
wordword1word2 write(,) word1 write(,)
word2 write(,) word
character (9) word1, word2 character (18)
word word13 word24 wordword1word2
write(,) word1 write(,) word2 write(,)
word
Error message appears when compiling !
34
  • The following are valid character constants
  • This is a test!
  • (A single blank)
  • (These characters are
    legal in a character
  • context, even though
    they are not a part
  • of the Fortran character
    set.)
  • 3.141593 (A character string, not a
    number.)
  • The following are not valid character constants
  • This is a test! (No single or double quotes.)
  • This is a test! (Mismatched quotes.)
  • Try this one. (Unbalanced single quotes.)

35
  • If a character string must include an apostrophe,
    then
  • the apostrophe may be represented by two
    consecutive
  • single quotes. For example, the string Mans
    best friend
  • would be written in a character constant as
  • Mans best friend
  • Mans best friend
  • Alternatively, the character string containing a
    single
  • quote can be surrounded by double quotes. For
    example,
  • Mans best friend

36
  • Similarly, a character string containing double
    quotes can
  • be surrounded by single quotes. The character
    string
  • Who cares? could be written in a character
    constant as
  • Who cares?
  • Character constants are most often used to print
    descri-
  • ptive information using the WRITE statement. For
    exam-
  • ple, the string result is a valid character
    constant
  • WRITE(,) Result , k
  • A character variable is a variable containing a
    value of
  • the character data type.

37
Logical Constants and Variables
  • The logical data type contains only two possible
    values
  • TRUE or FALSE.
  • A logical constant can have one of the following
    values
  • .TRUE. or .FALSE.. (The periods are required on
    either
  • side of the values to distinguish them from
    variable
  • names.)
  • .TRUE. (valid)
  • .FALSE. (valid)
  • TRUE (No periods this is a
    variable name.)
  • .FALSE (Unbalanced periods.)

38
  • Logical constants are rarely used, but logical
    expressions
  • and variables are commonly used to control
    program
  • execution, as will be seen in Chapter 3.
  • A logical variable is a variable containing a
    value of the
  • logical data type.

logical index, index1 index.true. index1.fal
se. If (index) then ..
39
Default and Explicit Variable Typing
  • The two ways in which the type of a variable can
    be
  • defined are default typing and explicit typing.
  • If the type of a variable is not explicitly
    specified in the
  • program, then default typing is used. By
    default
  • ?Variable names beginning with the letters I,
    J, K, L,
  • M, or N are assumed to be of type integer.
    Variable
  • names starting with any other letter are
    assumed to
  • be of type real.
  • For example,
  • incr (type integer)
  • big (type real)

40
program example ! This program is to
show the ! definition of default type.
read , a, b sumab avg(ab)/2.
print , a,b,sum,avg read(,) i, j
isumij iavg(ij)/2 write(,) i, j,
isum, iavg end program example
No declaration statements!!
__ __ __ __ __ __ __ __
If (a2., b3.) and (i2, j3)
41
  • The type of a variable may also be explicitly
    defined in
  • the declaration section at the beginning of a
    program.
  • The following statements can be used to specify
    the
  • type of variables
  • INTEGER var1, var2, var3,
  • REAL var11, var12, var13,
  • LOGICAL var21, var22, var23,
  • These nonexecutable statements are called type
  • declaration statements. They should be placed
    after
  • the PROGRAM statement and before the first
    execu-
  • table statement in the program.

42
program example ! This program is to show
the ! definition of explicit type. reala,
b, sum, avg, i, j, isum, iavg read , a, b
sumab avg(ab)/2. print ,
a,b,sum,avg read(,) i, j isumij
iavg(ij)/2 write(,) i, j, isum, iavg end
program example
__ __ __ __ __ __ __ __
If (a2., b3.) and (i2, j3)
43
  • No default names are associated with the
    character data
  • type, so all character variables must be
    explicitly typed
  • using the CHARACTER type declaration statement.
  • CHARACTER (lenltlengt) var1, var2, var3,
  • where ltlengt is the number of characters in the
    variables.
  • The (lenltlengt) portion of the statement is
    optional.
  • CHARACTER (len10) first, last two
    10-character

  • variables
  • CHARACTER initial a
    1-character variable
  • CHARACTER(15) id a
    15-character variable

44
Keeping Constants Consistent in a Program
  • You should always keep your physical constants
    consi-
  • stent throughout a program. The best way to
    achieve
  • consistency and precision throughout a program
    is to
  • assign a name to a constant and then to use that
    name
  • to refer to the constant throughout the program.
  • Named constants are created using the PARAMETER
  • attribute of a type declaration statement.
  • type, PARAMETER namevalue, name1value1
  • For example
  • REAL, PARAMETER pi3.141593, g9.8

45
Assignment Statements and Arithmetic
Calculations
  • Calculations are specified in Fortran with an
    assignment
  • statement, whose general form is
  • variable_name expression
  • The assignment statement calculates the value of
    the
  • expression to the right of the equal sign and
    assigns that
  • value to the variable named on the left of the
    equal sign.
  • The equal sign is called the assignment operator.

46
  • For example,
  • sum a b
  • A statement like
  • i i 1
  • is complete nonsense in ordinary algebra, but it
    makes
  • perfect sense in Fortran. In Fortran the
    statement
  • means Take the current value stored in variable
    i, add
  • one to it, and store the result back into
    variable i.

47
  • The standard arithmetic operators included in
    Fortran
  • are
  • addition
  • - subtraction
  • multiplication
  • / dividion
  • exponentiation ?? ab means ab.
  • The following rules apply when using Fortran
    arithmetic
  • operators
  • No two operators may occur side by side.
  • a-b (illegal) ? a(-b)
  • a-2 (illegal) ? a(-2)

48
2. Implied multiplication is illegal in
Fortran. x(yz) (illegal) ? x(yz) 3.
Parentheses may be used to group terms whenever
desired. When parentheses are used, the
expressions inside the parentheses are
evaluated before the expressions outside the
parentheses. 2 ( ( 8 2 ) / 5 ) 2
( 10 / 5 ) 2 2
4
49
Integer Arithmetic
  • Integer arithmetic is arithmetic involving only
    integer
  • data. Integer arithmetic always produces an
    integer result.
  • This rule is especially important to remember
    when an
  • expression involves division, since there can be
    no frac-
  • tional part in the answer. This behavior can
    lead to sur-
  • prising and unexpected answers.
  • 3/4 0 4/4 1 5/4 1
    6/4 1
  • 7/4 1 8/4 2 9/4 2
    10/4 2
  • You should never use integers to calculate
    real-world
  • quantities that vary continuously, such as speed
    or time.

50
Real Arithmetic
  • Real arithmetic ( or floating-point arithmetic)
    is arith-
  • metic involving real constants and variables.
    Real arith-
  • metic always produces a real result that is
    esentially
  • what you expect.
  • 3./4. 0.75 4./4. 1. 5./4. 1.25
    6./4.1.50
  • 7./4. 1.75 8./4. 2. 9./4. 2.25
    1./3. 0.3333333
  • As a result of limitation in precision, some
    quantities that
  • are theoretically equal will not be equal when
    the compu-
  • ter evaluates them. For example,
  • (1./30000000000.)3. ? 9.999999E-11
  • (1./20000000000.)2. 1.000000E-10

51
Hierarchy of Operations
  • Fortran has established a series of rules
    governing the
  • hierarchy or order in which operations are
    evaluated
  • within an expression.
  • The contents of all parentheses are evaluated
    first,
  • starting from the innermost parentheses and
    working
  • onward.
  • 2. All exponentials are evaluated, working from
    right to left.
  • 3. All multiplications and divisions are
    evaluated, working
  • from left to right.
  • 4. All additions and subtractions are evaluated,
    working
  • from left to right.

52
For example a3. b2. c5. d4. e10.
f2. g3.
outputa(bc)(de)/fg
3.(2.5.)(4.10.)/2.3.
3.7.14./2.3. 3.7.14./8.
21.14./8. 294./8.
36.75
53
For example a3. b2. c3.

outputa(bc)3.(2.3.)
3.8.
6561. output(ab)c(3.2)3.
9.3.
729. outputabc3.2.3.
3.8. 6561.
54
Mixed-Mode Arithmetic
  • Expressions containing both real numbers and
    integers
  • are called mixed-mode expressions, and
    arithmetic
  • involving both real numbers and integers is
    called mixed-
  • mode arithmetic.
  • In the case of an operation between a real number
    and
  • an integer, the computer converts the integer
    into a real
  • number, and real arithmetic is used on the
    numbers.
  • Integer expression 3/2 1
  • Real expression 3./2. 1.5
  • Mixed-mode expression 3./2 1.5

55
  • Expression Result
  • 1 1 / 4 1
  • 1. 1 / 4 1.
  • 1 1. / 4 1.25
  • nres 1.25 9 / 4 nres 3
  • Mixed-mode expression are dangerous because
  • they are hard to understand and may produce
  • misleading results. Avoid them whenever
    possible.

56
Intrinsic Functions
  • In mathematics a function is an expression that
    accepts
  • one or more input values and calculates a single
    result
  • from them.
  • Scientific and technical calculations usually
    require fun-
  • ctions that are more complex than the simple
    addition,
  • subtraction, multiplication etc. Many of the
    most
  • common ones are built directly into the Fortran
    language.
  • They are called intrinsic functions. Less common
    func-
  • tions are not included in the Fortran language,
    but the
  • user can supply any function needed to solve a
    particular
  • problem as either an external function or an
    internal
  • function.

57
Function name and Function Argument Result
arguments value type type
comments --------------------------------------
-------------------------------------- SQRT(X)
R R
Square root
of x for
x??0. --------------------------------------------
-------------------------------- ABS(X)
R/I
Absolute
value of
x ------------------------------------------------
---------------------------- SIN(X)
R R Sine of x

must be in radians ---------------------
--------------------------------------------------
----- COS(X) R
R Cosine of x
must be in
radians ------------------------------------------
----------------------------------
58
Function name and Function Argument
Result arguments value type
type comments ---------------------------
-------------------------------------------------
TAN(X) R R
Tangent of x
must be in
radians ------------------------------------------
---------------------------------- EXP(X)
R R e is
raised to
the xth
power --------------------------------------------
-------------------------------- LOG(X)
R R Natural
logarithm
of x for
xgt0 ----------------------------------------------
------------------------------ LOG10(X)
R R Base-10 logarithm

of x for xgt0 -------------------
--------------------------------------------------
-------
59
Function name and Function Argument Result
arguments value type type
comments --------------------------------------
-------------------------------------- INT(X)
R I
Integer part of x AINT(X)
x is
truncated ----------------------------------------
------------------------------------ NINT(X)
R I Nearest
integer to x ANINT(X)
x is rounded ---------------
--------------------------------------------------
----------- REAL(I) I
R Converts integer

value to real ------------------------------------
---------------------------------------- MOD(A,B)
R/I
Remainder or
modulo
function -----------------------------------------
-----------------------------------
60
N17 M5 mod(N,M)?2 mod(-N,M)?-2 t117.5
t21.8 mod(t1,t2)?1.3 a4. SQRT(a)?2.0
h_pi3.14159/2. sin(h_pi)?1.
t15.7 t25.4 int(t1)?5
aint(t1)?5.0 int(t2)?5
aint(t2)?5.0 int(-t1)?-5
aint(-t1)?-5.0 int(-t2)?-5
aint(-t2)?-5.0 nint(t1)?6
anint(t1)?6.0 nint(t2)?5
anint(t2)?5.0 nint(-t1)?-6
anint(-t1)?-6.0 nint(-t2)?-5
anint(-t2)?-5.0 3/int(t1)??
3/aint(t1)??
61
Function name and Function Argument Result
arguments value type type
comments --------------------------------------
-------------------------------------- MAX(A,B)
R/I Picks
the larger
of a and
b ------------------------------------------------
---------------------------- MIN(A,B)
R/I Picks the
smaller
of a and
b ------------------------------------------------
---------------------------- ASIN(X)
R R Inverse sine of
x
for --------------------------
--------------------------------------------------
ACOS(X) R R
Inverse sine of x
for
-------------------------------------------------
---------------------------
62
--------------------------------------------------
-------------------------- ATAN(X)
R R Inverse tangent of x

results in radians --------------------------
--------------------------------------------------
SIGN(A,B) The magnitude of the result
is obtained from
the first argument, and the sign
of the result is obtained from
the second
argument. ----------------------------------------
------------------------------------ DIM(A,B)
Subtract the second argument from
the first. If the
difference is positive,
it is taken as the value of the function
if the difference
is zero, the value of
the function is zero. ------------------------
--------------------------------------------------
---
63
X20. Y10. MAX(X,Y)?20. MIN(X,Y)?10.
DIM(X,Y)?10.0 DIM(Y,X)?0.0 A1.0 B-2.4
SIGN(A,B)?-1.0
payrateMIN(hours,40)rate1.5DIM(hours,40)
64
  • The argument of a function can be a constant, a
    variable,
  • an expression, or even the result of another
    function.
  • y SIN (3.141593) (argument is a constant)
  • y SIN (x) (argument is a
    variable)
  • y SIN (pix) (argument is an
    expression)
  • y SIN (SQRT(x)) (argument is the result of
    another

  • function)
  • Functions may never appear on the left side of
    the
  • assignment operator, since they are not memory
    loca-
  • tions and nothing can be stored in them.

65
List-Directed Input and Output Statements
  • An input statement reads one or more values from
    an
  • input device and stores them into variables
    specified
  • by the programmer. The input device could be a
    key-
  • board in an interactive environment or an input
    disk file
  • in a batch environment.
  • An output statement writes one or more values to
    an
  • output device. The output device could be a CRT
    screen
  • in an interactive environment or an output
    listing file in a
  • batch environment.

66
  • READ (,) input_list
  • where input_list is the list of variables into
    which the
  • values being read are placed.
  • The parentheses (,) in the statement contain
    control
  • information for the read.
  • The first field in the parentheses specifies the
    input/
  • output unit from which the data is to be read.
    An
  • asterisk in this field means that the data is to
    be read
  • from the standard input device for the computer
  • usually the keyboard when running in interactive
    mode.

67
  • The second field in the parentheses specifies the
    format
  • in which the data is to be read. An asterisk in
    this field
  • means that list-directed input (sometimes
    called free-
  • format input) is to be used.
  • The term list-directed input means that the types
    of the
  • variables in the variable list determine the
    required format
  • of the input data.
  • PROGRAM input_example
  • INTEGER i, j
  • REAL a
  • CHARACTER (len12) chars
  • READ(,) i, j, a, chars
  • END PROGRAM

68
PROGRAM input_example
INTEGER i, j REAL a
CHARACTER (len12) chars READ(,) i,
j, a, chars END PROGRAM
1, 2, 3., This one. (OK) 1,
2, This one., 3. (run-time error)
69
  • Each READ statement in a program begins reading
    from
  • a new line of input data. If any data is left
    over on the
  • previous input line, that data is discarded.
  • PROGRAM input_example_2
  • INTEGER i, j, k, l
  • READ (,) i, j
  • READ (,) k, l
  • END PROGRAM
  • input data 1, 2, 3, 4
  • 5, 6, 7, 8
  • ? i1, j2, k5, l6

70
  • The term list-directed output means that the
    types of the
  • values in the output list of the write statement
    determine
  • the format of the output data.
  • PROGRAM output_example
  • INTEGER ix
  • LOGICAL test
  • REAL theta
  • ix 1
  • test .TRUE.
  • theta 3.141593
  • WRITE(,) IX , ix
  • WRITE(,) THETA , theta
  • WRITE(,) COS(THETA), cos(theta)
  • WRITE(,) TEST ,test
  • END PROGRAM

71
The output resulting from those statements is
IX
1 THETA 3 .141593
COS(THETA) -1.000000 TEST
T
72
Initialization of Variables
PROGRAM init INTEGER i WRITE(,) i END PROGRAM
PROGRAM init INTEGER i1 WRITE(,) i END
PROGRAM
?
?
PROGRAM init INTEGER i READ(,) i WRITE(,)
i END PROGRAM
PROGRAM init INTEGER i i1 WRITE(,) i END
PROGRAM
?
?
73
The IMPLICIT NONE Statement
  • The IMPLICIT NONE statement disables the default
  • typing provisions of Fortran.
  • When the IMPLICIT NONE statement is included in a
  • program, any variable that does not appear in an
    explicit
  • type declaration statement is considered an
    error.
  • The IMPLICIT NONE statement should appear after
    the
  • PROGRAM statement and before any type
    declaration
  • statements.

74
PROGRAM test_1 REAL time 10.0 WRITE(,)
Time , tmie END PROGRAM
This program can be compiled and executed. The
answer will be Time 0.000000E00
(wrong answer)
75
PROGRAM test_1 IMPLICIT NONE REAL time
10.0 WRITE(,) Time , tmie END PROGRAM
When compiled with the same compiler, this
program produces the following compile-time
error WRITE(,) Time ,
tmie Error This name does not have
a type, and must have an explicit type.
Write a Comment
User Comments (0)
About PowerShow.com