Fundamentals of Fortran 90
description
Transcript of Fundamentals of Fortran 90
![Page 1: Fundamentals of Fortran 90](https://reader033.fdocuments.in/reader033/viewer/2022061401/56815aa2550346895dc82908/html5/thumbnails/1.jpg)
Fundamentals of Fortran Fundamentals of Fortran 9090
Basic Building Blocks
![Page 2: Fundamentals of Fortran 90](https://reader033.fdocuments.in/reader033/viewer/2022061401/56815aa2550346895dc82908/html5/thumbnails/2.jpg)
2
Identifiers
An identifier is a name that a programmer creates for items within a program including: variables, constants, and subprograms.
![Page 3: Fundamentals of Fortran 90](https://reader033.fdocuments.in/reader033/viewer/2022061401/56815aa2550346895dc82908/html5/thumbnails/3.jpg)
3
Rules for Identifiers1 The first character must be a letter.2 The remaining characters may be
letters, digits, or the underscore.3 There may be no more than 31
characters in an identifier.
![Page 4: Fundamentals of Fortran 90](https://reader033.fdocuments.in/reader033/viewer/2022061401/56815aa2550346895dc82908/html5/thumbnails/4.jpg)
4
Guidelines for Identifiers Capitalize the first letter If the identifier is composed of two
words which have been concatenated (run together) Capitalize the first letter of each word Optionally, place an underscore between
the words Select identifiers that have some
significance to the problem being solved
![Page 5: Fundamentals of Fortran 90](https://reader033.fdocuments.in/reader033/viewer/2022061401/56815aa2550346895dc82908/html5/thumbnails/5.jpg)
5
Identifiers Valid:
TaxRate Last_Page Radius Not Valid
Time-secs 3rdYear
![Page 6: Fundamentals of Fortran 90](https://reader033.fdocuments.in/reader033/viewer/2022061401/56815aa2550346895dc82908/html5/thumbnails/6.jpg)
6
Variables
Each variable can store a single value Since computers use several different internal
representations for various types of data, a specific data type must be associated with each variable
The value can be changed while the program is running, but the data type cannot be changed
A variable is a memory location in the primary memory of a computer.
![Page 7: Fundamentals of Fortran 90](https://reader033.fdocuments.in/reader033/viewer/2022061401/56815aa2550346895dc82908/html5/thumbnails/7.jpg)
7
Data TypesFortran 90 supports five basic data types
Fortran 90 allows a programmer to define additionaldata types to fit a specific application.
INTEGER REAL CHARACTER LOGICAL COMPLEX
![Page 8: Fundamentals of Fortran 90](https://reader033.fdocuments.in/reader033/viewer/2022061401/56815aa2550346895dc82908/html5/thumbnails/8.jpg)
8
Declarations
A declaration reserves variables (memory locations) for each of the identifiers and associates a data type with each variable
A declaration DOES NOT provide an initial value for any variable
type_name :: list of identifiers separated by commas
Syntax
![Page 9: Fundamentals of Fortran 90](https://reader033.fdocuments.in/reader033/viewer/2022061401/56815aa2550346895dc82908/html5/thumbnails/9.jpg)
9
Examples of DeclarationsREAL :: A, BINTEGER :: KINTEGER :: NLOGICAL :: IsThereCHARACTER (6) :: W, T, H*17
• W, T and H are character variables
• W and T will hold exactly 6 characters
• H will hold exactly 17 characters
![Page 10: Fundamentals of Fortran 90](https://reader033.fdocuments.in/reader033/viewer/2022061401/56815aa2550346895dc82908/html5/thumbnails/10.jpg)
10
Default Types
Fortran does not require that variables be declared
If a variable is referenced without being declared, Fortran will give that variable a data type based on the first letter of its identifier I through N ==> INTEGER otherwise ==> REAL
![Page 11: Fundamentals of Fortran 90](https://reader033.fdocuments.in/reader033/viewer/2022061401/56815aa2550346895dc82908/html5/thumbnails/11.jpg)
11
The IMPLICIT Statement The IMPLICIT statement can be used to
override Fortran’s rules for assigning data types to undeclared variables
Good programming style is to declare all variables in a program
If the statement IMPLICIT NONE precedes the first declaration, the Fortran 90 compiler will ensure that every variable which is used in the program has been explicitly declared
![Page 12: Fundamentals of Fortran 90](https://reader033.fdocuments.in/reader033/viewer/2022061401/56815aa2550346895dc82908/html5/thumbnails/12.jpg)
12
Writing an INTEGER Value String of digits No commas Optional + or - sign Examples:
21856 -89 0
![Page 13: Fundamentals of Fortran 90](https://reader033.fdocuments.in/reader033/viewer/2022061401/56815aa2550346895dc82908/html5/thumbnails/13.jpg)
13
Integers: Valid & Invalid Valid
234 +236 -225 0
Not Valid 5- --5 17.0 14,555
![Page 14: Fundamentals of Fortran 90](https://reader033.fdocuments.in/reader033/viewer/2022061401/56815aa2550346895dc82908/html5/thumbnails/14.jpg)
14
Writing a REAL Value (1) Fixed Point Form ( xxx.xx )
String of digits with decimal point No commas Optional + or - sign Valid Real Values:
-54.873 0.0 6.5 18.0 0.56 NOT Valid Reals:
12,345.67 12
![Page 15: Fundamentals of Fortran 90](https://reader033.fdocuments.in/reader033/viewer/2022061401/56815aa2550346895dc82908/html5/thumbnails/15.jpg)
15
Writing a REAL Value (2) Floating Point Form
Analogous to scientific notation Basic form: fEn
Fixed point real ( f ) “Mantissa” letter E integer exponent ( n )
Interpretation f*10n
Examples: 6.57E-3 for 0.00657 -45.6E4 for -456000.0 0.7206E+3 for 720.6
![Page 16: Fundamentals of Fortran 90](https://reader033.fdocuments.in/reader033/viewer/2022061401/56815aa2550346895dc82908/html5/thumbnails/16.jpg)
16
Writing a Character Value Two forms
‘string’ “string”
Either form can be used Examples
“This is easy” ‘What is your name?’ “That’s all folks”
![Page 17: Fundamentals of Fortran 90](https://reader033.fdocuments.in/reader033/viewer/2022061401/56815aa2550346895dc82908/html5/thumbnails/17.jpg)
17
Declaring a Constant
This variant on the basic declaration is used to give a symbolic name to a constant
The use of declared constants is considered to be a better programming practice than writing the value of the constant in the code itself unless the constant is an integral part of a formula and will never change value or precision
type_name, PARAMETER :: name = value
Syntax
![Page 18: Fundamentals of Fortran 90](https://reader033.fdocuments.in/reader033/viewer/2022061401/56815aa2550346895dc82908/html5/thumbnails/18.jpg)
18
Examples Using PARAMETER
INTEGER, PARAMETER :: NumStates = 50
REAL, PARAMETER :: PI = 3.14159
CHARACTER (*), PARAMETER :: Prompt = “next>“
![Page 19: Fundamentals of Fortran 90](https://reader033.fdocuments.in/reader033/viewer/2022061401/56815aa2550346895dc82908/html5/thumbnails/19.jpg)
19
Assignment Statement Syntax
target_variable = expression
Syntax
The value represented by the expression on the right hand side of the equal sign is stored in the target variable, replacing its previous value
![Page 20: Fundamentals of Fortran 90](https://reader033.fdocuments.in/reader033/viewer/2022061401/56815aa2550346895dc82908/html5/thumbnails/20.jpg)
20
Assignment Statement Uses
Store a specific value in a variable N = 32 ; X = -3.5 ; W = ‘cat’
Copy a value from one variable to another T = N
Store a computed value in a variable X = 3.5 + W
![Page 21: Fundamentals of Fortran 90](https://reader033.fdocuments.in/reader033/viewer/2022061401/56815aa2550346895dc82908/html5/thumbnails/21.jpg)
21
Numeric Conversions on Assignment
INTEGER :: K, N, MREAL :: W, C, Y
N = 6.8
X = 53
K = -6W = K
Y = 7.8M =N
Integer value 6 will be stored in N (truncation rather than rounding)
Real value 53.0 will be stored in X
Integer value -6 will be stored in KReal value -6.0 will be stored in W
Real value 7.8 will be stored in YInteger value 6 will be stored in M
![Page 22: Fundamentals of Fortran 90](https://reader033.fdocuments.in/reader033/viewer/2022061401/56815aa2550346895dc82908/html5/thumbnails/22.jpg)
22
Character Padding and Truncation on Assignment
CHARACTER (5) :: A, BCHARACTER (8) :: C
A = ‘frog’
B = “elephant”
C = B
frog’ will be stored in A (padded out to five characters with blank at end)
Note: The symbol ’ represents a blank space
eleph will be stored in B (extra characters are truncated from the right end)
eleph’’’ will be stored in C (B contained eleph which must be padded to 8 characters)
![Page 23: Fundamentals of Fortran 90](https://reader033.fdocuments.in/reader033/viewer/2022061401/56815aa2550346895dc82908/html5/thumbnails/23.jpg)
23
Numeric ExpressionsA numeric expression is any mathematically valid sequence of operators, operands, and parentheses.
An operand is a constant, variable, or value returned by a function.
![Page 24: Fundamentals of Fortran 90](https://reader033.fdocuments.in/reader033/viewer/2022061401/56815aa2550346895dc82908/html5/thumbnails/24.jpg)
24
Arithmetic Operators
+ add- subtract* multiply/ divide** exponentiation
A**B means AB
![Page 25: Fundamentals of Fortran 90](https://reader033.fdocuments.in/reader033/viewer/2022061401/56815aa2550346895dc82908/html5/thumbnails/25.jpg)
25
Result Types The result type is determined for each
mathematical operation The type is based on the type of the
operands If both operands are INTEGER, the result
is an integer If at least one operand is REAL, the
result is a real number
![Page 26: Fundamentals of Fortran 90](https://reader033.fdocuments.in/reader033/viewer/2022061401/56815aa2550346895dc82908/html5/thumbnails/26.jpg)
26
Precedence Rules Evaluate subexpressions in parentheses Determine the value returned by any
function call Evaluate ** (right to left)
2**3**5 is the same as 2**(3**5) Evaluate * and / from left to right Evaluate + and - from left to right
![Page 27: Fundamentals of Fortran 90](https://reader033.fdocuments.in/reader033/viewer/2022061401/56815aa2550346895dc82908/html5/thumbnails/27.jpg)
27
Precedence Rules All exponentiations are performed
first; consecutive exponentiations are performed from right to left
2 ** 3 ** 2 = 2 ** 9 = 512 All multiplications and divisions are
performed next, in the order in which they appear from left to right.
10 / 5 * 2 = 2 * 2 = 4
![Page 28: Fundamentals of Fortran 90](https://reader033.fdocuments.in/reader033/viewer/2022061401/56815aa2550346895dc82908/html5/thumbnails/28.jpg)
28
Precedence Rules The additions and subtractions are
performed last, in the order in which they appear from left to right 10 + 8 – 2 + 3 = 19
Another example 2 + 4 ** 2 / 2
2 + 16 / 2 2 + 8 10
![Page 29: Fundamentals of Fortran 90](https://reader033.fdocuments.in/reader033/viewer/2022061401/56815aa2550346895dc82908/html5/thumbnails/29.jpg)
29
Characteristics of the / Operator If numerator and denominator are integers,
the quotient is truncated to an integer• 23/5 is 4 rather than 4.6
If either the numerator or denominator is a real number, the quotient is a real number
• 12/9.0 is 1.3333• 3.6/1.2 is 3.0• 8.6/2 is 4.3• 23.0/5.0 is 4.6
![Page 30: Fundamentals of Fortran 90](https://reader033.fdocuments.in/reader033/viewer/2022061401/56815aa2550346895dc82908/html5/thumbnails/30.jpg)
30
Characteristics of the ** Operator
If B is a nonnegative integer, Fortran uses repeated multiplication to evaluate A**B
If B is a real number, Fortran uses the expression eB*ln(A) to approximate A**B. Ex. (-4.0) ** 2.0 is undefined, because the
logarithms of negative values are not defined.
Consider A**B
![Page 31: Fundamentals of Fortran 90](https://reader033.fdocuments.in/reader033/viewer/2022061401/56815aa2550346895dc82908/html5/thumbnails/31.jpg)
31
Type Conversions
Using both integers and reals in a computation is a poor programming practice.
Fortran provides some type conversion functions to convert a value to a different data type.
Type conversions should be used to avoid mixed data types in an expression.
The original value is not changed, but the converted value is placed in a temporary (unnamed) variable.
![Page 32: Fundamentals of Fortran 90](https://reader033.fdocuments.in/reader033/viewer/2022061401/56815aa2550346895dc82908/html5/thumbnails/32.jpg)
32
Type Conversion Examples
INTEGER ==> REAL REAL ==> INTEGERINTEGER N
REAL X
N = 15
X = 3.7 * REAL(N)X = 3.7 * 15.0N is unchanged
INTEGER K
REAL Y
Y = 2.8
K = INT(Y) + 5K = 2 + 5Y is unchanged
![Page 33: Fundamentals of Fortran 90](https://reader033.fdocuments.in/reader033/viewer/2022061401/56815aa2550346895dc82908/html5/thumbnails/33.jpg)
33
A Few FunctionsA good programmer makes use of functions that are providedas part of the programming language. Fortran 90 has an extensive collection of intrinsic functions. (See Appendix D)
SQRT(X) Preferred to X**(0.5)
ABS(X) Absolute value
SIN(X), COS(X), TAN(X) Angles must be in radians
EXP(X) eX
LOG(X) ln X
LOG10(X) log10X
MOD(A,B) A mod B
![Page 34: Fundamentals of Fortran 90](https://reader033.fdocuments.in/reader033/viewer/2022061401/56815aa2550346895dc82908/html5/thumbnails/34.jpg)
34
Form of a Program ------------ Header portion ------------- Initial comment block PROGRAM statement --------- Specification portion --------- IMPLICIT NONE Type specification statements -------- Execution portion ------------ READ, PRINT, and assignments -------- Ending portion --------------- END PROGRAM statement
![Page 35: Fundamentals of Fortran 90](https://reader033.fdocuments.in/reader033/viewer/2022061401/56815aa2550346895dc82908/html5/thumbnails/35.jpg)
35
Structure of a Line of Source Code
Maximum of 132 characters. Can begin anywhere on the line. Two or more statements can appear on a single
line if they are separated by semicolons. Blanks can be inserted to improve comprehension. A comment can appear at the end of the line. If a statement label is used, it must appear first.
A label is an unsigned integer with at most 5 digits. If a statement is too long, place an ampersand, &,
at the end and continue on the next line.
![Page 36: Fundamentals of Fortran 90](https://reader033.fdocuments.in/reader033/viewer/2022061401/56815aa2550346895dc82908/html5/thumbnails/36.jpg)
36
Comments
Everything from the exclamation mark (!), to the end of the line is a comment
A comment can appear on a line by itself or at the end of some other line Comments are included in a program to provide a person with
information about the program Comments are ignored by the compiler
! comment
Syntax
![Page 37: Fundamentals of Fortran 90](https://reader033.fdocuments.in/reader033/viewer/2022061401/56815aa2550346895dc82908/html5/thumbnails/37.jpg)
37
The PROGRAM Statement
Provides a name (identifier) for the program Optional but strongly recommended Required by most programming standards Must be the first statement, but can be preceded
by comments
PROGRAM program_name
Syntax
![Page 38: Fundamentals of Fortran 90](https://reader033.fdocuments.in/reader033/viewer/2022061401/56815aa2550346895dc82908/html5/thumbnails/38.jpg)
38
The END PROGRAM Statement
This is the only required statement Must be the last line in the source code file program_name must match that on the
PROGRAM statement Omit the program-name if there is no
program statement
END PROGRAM program_name
Syntax
![Page 39: Fundamentals of Fortran 90](https://reader033.fdocuments.in/reader033/viewer/2022061401/56815aa2550346895dc82908/html5/thumbnails/39.jpg)
39
The STOP Statement
Causes the program to stop its execution at that point
More than one STOP statement can be included
This statement is not required, but is recommended by some coding standards
STOP
or STOP n !n is an integer value
Syntax
![Page 40: Fundamentals of Fortran 90](https://reader033.fdocuments.in/reader033/viewer/2022061401/56815aa2550346895dc82908/html5/thumbnails/40.jpg)
40
Simple Input (list-directed)
input_list is a list of variables which will receive the values that are read
Each READ statement begins reading a new line of data A READ statement can read more than one line of data
READ *, input_list
Syntax
![Page 41: Fundamentals of Fortran 90](https://reader033.fdocuments.in/reader033/viewer/2022061401/56815aa2550346895dc82908/html5/thumbnails/41.jpg)
41
Simple Output (list-directed)
output_list is a list of items to be printed character string literals will be printed exactly variables will have their values printed
Each PRINT statement starts a new line of output Fortran will use its internal rules to determine the
location and appearance of values on each line
PRINT *, output_list
Syntax