21.02.2013 Views

Fortran 90 Program Structure and Layout

Fortran 90 Program Structure and Layout

Fortran 90 Program Structure and Layout

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

AS3013: F<strong>90</strong> lecture 2<br />

<strong>Fortran</strong> <strong>90</strong><br />

<strong>Program</strong> <strong>Structure</strong> <strong>and</strong> <strong>Layout</strong><br />

Modular <strong>Structure</strong><br />

Split total program into modular units.<br />

Any number (including 0) subprograms.<br />

One (<strong>and</strong> only one) main program.<br />

Each program/subprogram should have<br />

one major task<br />

Input/Output<br />

Calculations<br />

Now let’s examine the sample program.<br />

1


Declaration section (non-executable)<br />

<strong>Program</strong> name PROGRAM first<br />

IMPLICIT NONE<br />

Type declarations REAL x, y, z<br />

must precede<br />

Execution section ** starts at the beginning **<br />

Assignment<br />

<strong>and</strong><br />

Input/Output<br />

Statements<br />

AS3013: F<strong>90</strong> lecture 2<br />

Forces variables<br />

to be declared<br />

Data types of Variables<br />

PRINT *, 'Enter two numbers to add:'<br />

READ *, x, y<br />

Read/Write values using default devices<br />

(keyboard/screen) in a default (*) format<br />

z = x + y<br />

Assign to z the sum of x <strong>and</strong> y<br />

PRINT *, 'Result =', z<br />

** finish at the end **<br />

Termination section END PROGRAM first last statement in unit<br />

Comments start with ! anywhere on a line, anywhere before END<br />

2


Subprograms: Same structure as main program<br />

CALLed from main program or another subprogram<br />

First statement: SUBROUTINE name<br />

or: FUNCTION name<br />

Statements: are written in lines.<br />

Most can be written on one line.<br />

May be continued onto more lines if<br />

last character of line is an ampers<strong>and</strong> (&)<br />

Use to improve layout.<br />

Maximum 40 lines in a statement.<br />

A line may contain 0 to 132 characters.<br />

Recommendation: no more than about 70 characters on a line<br />

keeps program readable <strong>and</strong> manageable<br />

In <strong>Fortran</strong> <strong>90</strong> a statement may be anywhere on a line<br />

AS3013: F<strong>90</strong> lecture 2<br />

3


Comments:<br />

• Important for underst<strong>and</strong>ing code<br />

Even if you have written it yourself<br />

• An exclamation mark (!) anywhere in any line<br />

starts a comment to the end of the line.<br />

• Comments have no effect on execution<br />

Blank lines <strong>and</strong> blanks in lines improve readability.<br />

ADD COMMENTS WITHIN THE PROGRAM TO<br />

DELINEATE SECTIONS AND EXPLAIN ACTIONS<br />

THE FIRST LINES OF EVERY COMPUTER PROGRAM<br />

SHOULD CONTAIN THE PROGRAMMER’S NAME,<br />

THE DATE, AND THE PURPOSE OF THE PROGRAM.<br />

AS3013: F<strong>90</strong> lecture 2<br />

4


<strong>Fortran</strong> <strong>90</strong>: Data types <strong>and</strong> constants<br />

CHARACTER constant: a string of one or more characters<br />

between single or double quotes.<br />

e.g. "Fred", ' ' (a blank)<br />

Numerical constants may be positive, negative or zero.<br />

If positive the sign is optional.<br />

Embedded commas (e.g. 1,000,000) or spaces (1 000 000)<br />

are not permitted.<br />

INTEGER constant: any number not containing a<br />

decimal point <strong>and</strong>/or exponent.<br />

e.g. 2153 -36 0 123456 999<br />

Range is machine dependent.<br />

On SUN, PC <strong>and</strong> many others to > |2 000 000 000|<br />

Complete accuracy. (32 bits = 4 bytes)<br />

AS3013: F<strong>90</strong> lecture 2<br />

5


REAL constant: any number containing a decimal point<br />

<strong>and</strong>/or exponent (“scientific” notation)<br />

e.g. 21.362 -9.6405 0.0 -25. .00125<br />

The exponent is the letter E followed by an integer constant<br />

representing a power of 10<br />

e.g. -3.5E4( ! " 35 # 10 ) 0.263E-6<br />

4<br />

.<br />

# ( ! 0 263" 10 ) 6<br />

.<br />

"<br />

! 10 8<br />

! 5" 10 12<br />

AS3013: F<strong>90</strong> lecture 2<br />

( )<br />

5E+12 1E-8<br />

( )<br />

decimal point not required if exponent present.<br />

Precision depends on computer <strong>and</strong> compiler.<br />

SUN, PC: 6 to 7 decimal digits Exponent range is ±38 ~ ± ±<br />

10 38<br />

REAL numbers: Beware of rounding errors <strong>and</strong> truncation.<br />

DOUBLE PRECISION: for greater range <strong>and</strong> precision (64 bits total).<br />

15-17 digits, range ~ ± . Constants must be in exponent form using D.<br />

±<br />

10 308<br />

e.g. 5D12, 3.141592653589793D0, 1D0<br />

STRONGLY RECOMMENDED FOR SCIENTIFIC WORK<br />

6


<strong>Fortran</strong> <strong>90</strong>: St<strong>and</strong>ard character set<br />

A to Z (upper case), digits 0 to 9, underscore _<br />

special characters:<br />

space = + - * ( ) , . ' : " ! % & ; < > ? $<br />

For portability it is advisable to stick to these characters even in character<br />

constants, but most computers now use the ASCII character set to represent<br />

characters as 8 bit numbers (0-255).<br />

additional characters:<br />

a to z (lower case), # @ [ \ ] ^ ` { | }<br />

Any character acceptable to the computer may be used in CHARACTER<br />

constants.<br />

WARNING: <strong>Fortran</strong> compilers do not distinguish between upper<br />

<strong>and</strong> lower case except in character constants.<br />

i.e. ABCDE ≡ abcde ≡ AbCdE ≡ aBcDe, etc.<br />

Other data types: LOGICAL, COMPLEX<br />

AS3013: F<strong>90</strong> lecture 2<br />

7


Variable names: (<strong>and</strong> most other names, e.g. for PROGRAM)<br />

• First character must be a letter.<br />

• Remaining characters may be any combination of<br />

letters, digits or underscore ( _ ) characters.<br />

• Maximum length is 31 characters.<br />

e.g. A A12 Alpha_3 Next_Month width<br />

Variables are names of ‘boxes’ in computer memory where<br />

numerical or other data values are stored.<br />

We must tell the compiler what type of data they will hold by<br />

declaring all variables in Type statements.<br />

e.g. old style new style<br />

REAL a, b, c or REAL :: a, b, c<br />

INTEGER count, year INTEGER :: count, year<br />

Use IMPLICIT NONE at the start of each program unit.<br />

Use comments to create a variable dictionary.<br />

AS3013: F<strong>90</strong> lecture 2<br />

8

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!