Вы находитесь на странице: 1из 46

FORTRAN Basics

Fortran
Fortran is a (computer) language Like any other language it also has very precise grammar For programming language this (grammar) is termed as syntax

Fortran Alphabets
Letters: Upper and lower case letters: ABCDEFGHI J KLM NOPQRSTUVWXYZ abcdefghijklm nopqrst vwxyz Digits: 0123456789 Special Characters: space () *+-/:=_ !&\$;<>%? ,. Fortran is not case sensitive

Data Types
Data Types INTEGER REAL DOUBLE PRECISION COMPLEX LOGICAL CHARACTER Characteristics Whole numbers stored exactly Numbers, which may have fractional parts, with limited precision Similar to real but with greater Precision Complex numbers; stored as an ordered pair of real numbers A Boolean value; either true or false A string of characters of fixed length

CONSTANTS

Values of constants do not change during program execution

Integer Constants
The general form of an integer constant is:
sign digits

Integer Constants (contd.)

Examples of valid integer constants:
0 137 -2516 +17745

Some invalid integers:

5,280 16.0 --5

Why?

Real Constants
A real constant must contain a decimal point or an exponent ( or both) The letter E used in Fortran Represent times 10 to the power of. For example, the constant 1.23410-5 is written as 1.234E-5.

Real Constants (contd.)

The most general form of a real constant is:
sign digits . digits E
integer-part decimal-part basic-real-constant

sign digits
exponent exponent-section

Examples of valid real constants:

Decimal Form: 123.45, .123, 123., -0.12, +.12 Exponential Form: 12.34E3, 12.3E+3,-1.2E-3

Incorrect
12,355.67 65

Double Precision Constants

A double precision has a similar form to a real constant but the letter D in place of E is used conventionally even if the exponent is zero Some examples:
3.14159D0 1.0D-12 -3.6525D+02
The double precision displays 15 significant figures whilst the real type constants displays 7 s.f.

Complex Constants
Complex constants have the form of two real or integer constants separated by comma enclosed in parenthesis For A+iB the constant is (A,B) Examples
(3.14,-5.67) (0,0) (-0.999,2.718E15)

Logical Constants

There are only two possible logical constants

.TRUE.

.FALSE.

Character Constants
A string of characters enclosed between apostrophes or double quotes Examples
John or John or John Dow #2 or John Dow #2

Character Constants (contd.)

Special Case:
Loris Apple or Loris Apple Dont forget Jims book or Dont forget Jims book

Incorrect:
you and me Techs seminar Have a nice day

VARIABLES
Early programs were written in binary language
Computer instructions had to be hand-encoded into binary numbers Then the binary code was entered into the machine Programmers had to explicitly assign and reference any storage locations

Modern day programming languages allow us to assign symbolic names (known as variable names) for storing program computations and results

Fortran Variables
The type of a FORTRAN variable must be one of the six data types mentioned before. The type of each variables determines the type of value that may be assigned to that variable.

IDENTIFIERS

Identifiers are the names given to programs, constants, variables, and other entities in a program.

Fortran Identifiers
No more than 31 characters The first character must be a letter The remaining, if any, may be letters, digits or underscores

Use meaningful identifiers:

Good: Total, Rate, Length Not so good: ThisIsALongIdentifierName, X321, A_B_012, OPQ

Examples:
R RA_TE_ A12334

Some invalid identifiers:

M.T.U. R2-D2 6FEET
Why?

Type Declaration
INTEGER :: ZIP, Mean, Total REAL :: Average, error, sum CHARACTER (LEN=15) :: Name, Street CHARACTER(15) :: Name, Street CHARACTER :: letter, digit CHARACTER (10) :: City, Nation*20, Box, Bug*1

Variable Initialization

Used for assigning values to variables during declaration

REAL :: offset = 0.1, length = 10.0 INTEGER :: count count = 0

Is this initialization?

Giving Constants Names

Syntax:
type, PARAMETER :: name = value, name = value,

Examples:
INTEGER, PARAMETER :: Limit =100 REAL, PARAMETER :: PI = 3.14159, TWOPI = 2*PI CHARACTER(4), PARAMETER :: City=LA, LN=Newton CHARACTER(*), PARAMETER :: LastName=Newton

OPERATORS

Type
Arithmetic

Operators
** * + / -

Associativity
Right-to-left Left-to-right Left-to-right

Arithmetic Expressions
The order of evaluation of an expression:
Sub-expressions in parenthesis Function reference Exponentiation, i.e., raising to a power Multiplication and division Addition and subtraction

Arithmetic Expressions
Now lets find the value of the following expression
(5*(11-5)**2**2)*4+9 (5*[(11-5)]**2**2)*4+9 (5*6**2**2)*4+9 (5*6**[2**2])*4+9 (5*6**4)*4+9 (5*[6**4])*4+9 (5*1296)*4+9 [(5*1296)]*4+9 6480*4+9 (6480*4)+9 25920+9 25925
Sub-expressions in parenthesis Function reference Exponentiation (right to left) Multiplication and division (left to right) Addition and subtraction (left to right)

Integer Division
2*4*5/3**2 = ? 4.4444 Integer division results in another integer:
2*4*5/[3**2] 2*4*5/9 [2*4*5]/9 40/9 4

Mixed-mode Operation
Expressions involving different types of numeric operands: real and integer
Operation INTEGER REAL Conversion REALREAL Result REAL

Mixed-mode Operation (contd.)

1.0/4 = 0.25
1.0/4 1.0/{4} 1.0/4.0 0.25

3.0+8/5 = 4.0
3.0 + 8/5 3.0+[8/5] 3.0+1 3.0+{1} 3.0+1.0 4.0

Mixed-mode Operation (contd.)

3/4*5.0 = ?
3/4*5.0

5.0*3/4 = ?
5.0*3/4

3/4*5.0
{3/4}*5.0 0*5.0 0

5.0*3/4
5.0*{3}/4 5.0*3.0/4 [5.0*3.0]/4 15.0/4 15.0/{4} 15.0/4.0 3.75

Mixed-mode Operation (contd.)

This also affect the manner in which exponentiation are performed 2.0**3 2.0*2.0*2.0 8.0
(-4.0)**2 (-4.0)*(-4.0) 16.0 2.0**3.0 e3.0ln(2.0) 8.0 2**3.0 Is the answer real or integer? Are all these mixed mode operations?

Mixed-mode Operation (contd.)

exponentiation of a negative quantity raised to real power is undefined logarithms of negative values are not defined.
(-4.0)**2.0 e2.0 ln(-4.0) undefined

COMMON FUNCTIONS
ABS (x) SQRT (x) SIN (x) COS (x) TAN (x) ASIN (x) ACOS (x) ATAN (x) EXP (x) LOG (x) LOG10(x) :absolute value of x :square root of x :sine of x radians :cosine of x radians :tangent of x radians :arc sine of x :arc cosine of x :arc tangent of x :ex :ln(x) :log10(x)

Common Functions (contd)

INT (x) REAL (x) :integer part of x (x is real) :convert x (x is integer) to real

MAX (x1, ..,xn) :maximum of x1, , xn MIN (x1, ..,xn) :minimum of x1, , xn MOD(x,y) :x INT(x/y)*y

Assignment Statements: Assigning values to variables

Syntax:
variable = expression

Assignment Statements: Examples

INTEGER :: Unit, Amount, Total Unit = 5 Amount = 100.99 Total = Unit * Amount REAL, PARAMETER :: PI = 3.14159 REAL :: Area INTEGER :: Radius =5 Area = (Radius**2)*PI

Replacement Statement

INTEGER :: Counter = 0 Counter = Counter + 1 Counter = Counter + 3

Input/Output
Fortran provides two types of input /output statements:
Formatted input/output List-directed input/output

List-directed output PRINT*, output-list or WRITE (*,*) output-list List-directed input READ*, input-list or READ (*,*) input-list

Program Structure and Format

PROGRAM program-name IMPLICIT NONE specification/declaration part execution part END PROGRAM program-name

Some Simple Programs

Program that prints the following text in the terminal:
Programming is fun. And programming in Fortran is more fun.
first program

Program that inputs from user, two integers and adds them; then displays the result Program that illustrate the use of various arithmetic operators Program that uses intrinsic functions

sumof_ij

arith_oper

use_of_func

PROGRAM first IMPLICIT NONE PRINT*, Programming is fun PRINT*, And programming in FORTRAN is more fun END PROGRAM first

PROGRAM arith_exp IMPLICIT NONE INTEGER :: a=100, b=2, c=25, d=4, Result Result = a b PRINT*, ab = , result Result = b*c PRINT*, b*c = , result Result = a/c PRINT*, a/c = , result Result = a**d PRINT*, a**d = , result END PROGRAM arith_exp

PROGRAM sumof_ij PROGRAM IMPLICIT NONE Non executable portion INTEGER :: i, j, sumij READ*, i, j Executable portion sumij = i+j PRINT*, sumij END PROGRAM END PROGRAM sumof_ij

PROGRAM use_of_func IMPLICIT NONE INTEGER :: a=100, b=90 , c=17, d =4, Result REAL :: Rresult Result=MAX(a,b,c,d) PRINT*, Maximum = , result Result=MIN(a,b,c,d) PRINT*, Minimum = , result RResult=SIN(REAL(b)) PRINT*, sinb = , Rresult RResult=SQRT(REAL(a)) PRINT*, Square root of a = , Rresult END PROGRAM use_of_func