<<

Fortran 90 Program Structure and Layout

Modular Structure Split total program into modular units. Any number (including 0) subprograms. One (and only one) main program.

Each program/subprogram should have one major task Input/Output Calculations

Now let’s examine the sample program.

AS3013: F90 lecture 2 1 Declaration section (non-executable) Forces variables Program name PROGRAM first to be declared IMPLICIT NONE Type declarations REAL x, y, z Data types of Variables must precede Execution section ** starts at the beginning ** PRINT *, 'Enter two numbers to add:' Assignment READ *, x, y and Read/Write values using default devices Input/Output (keyboard/screen) in a default (*) format Statements z = x + y Assign to z the sum of x and y PRINT *, 'Result =', z ** finish at the end ** Termination section END PROGRAM first last statement in unit Comments start with ! anywhere on a line, anywhere before END

AS3013: F90 lecture 2 2 Subprograms: Same structure as main program CALLed from main program or another subprogram First statement: SUBROUTINE name or: FUNCTION name Statements: are written in lines. Most can be written on one line. May be continued onto more lines if last character of line is an ampersand (&) Use to improve layout. Maximum 40 lines in a statement. A line may contain 0 to 132 characters.

Recommendation: no more than about 70 characters on a line keeps program readable and manageable

In Fortran 90 a statement may be anywhere on a line

AS3013: F90 lecture 2 3 Comments: • Important for understanding code Even if you have written it yourself

• An exclamation mark (!) anywhere in any line starts a comment to the end of the line. • Comments have no effect on execution Blank lines and blanks in lines improve readability.

ADD COMMENTS WITHIN THE PROGRAM TO DELINEATE SECTIONS AND EXPLAIN ACTIONS

THE FIRST LINES OF EVERY SHOULD CONTAIN THE PROGRAMMER’S NAME, THE DATE, AND THE PURPOSE OF THE PROGRAM.

AS3013: F90 lecture 2 4 Fortran 90: Data types and constants CHARACTER constant: a string of one or more characters between single or double quotes. e.g. "Fred", ' ' (a blank) Numerical constants may be positive, negative or zero. If positive the sign is optional. Embedded commas (e.g. 1,000,000) or spaces (1 000 000) are not permitted. constant: any number not containing a point and/or exponent. e.g. 2153 -36 0 123456 999 Range is machine dependent. On SUN, PC and many others to > |2 000 000 000| Complete accuracy. (32 bits = 4 bytes)

AS3013: F90 lecture 2 5 REAL constant: any number containing a decimal point and/or exponent (“scientific” notation) e.g. 21.362 -9.6405 0.0 -25. .00125 The exponent is the letter E followed by an integer constant representing a 4 6 e.g. -3.5E4 (! " 3.5#10 ) 0.263E-6 (! 0.263"10# ) 5E+12 (! 5"1012 ) 1E-8 (! 10"8 ) decimal point not required if exponent present. Precision depends on computer and compiler. SUN, PC: 6 to 7 decimal digits Exponent range is ±38 ~ ±10±38

REAL numbers: Beware of errors and truncation.

DOUBLE PRECISION: for greater range and precision (64 bits total). 15-17 digits, range ~ ± 1 0 ± 3 0 8 . Constants must be in exponent form using D. e.g. 5D12, 3.141592653589793D0, 1D0 STRONGLY RECOMMENDED FOR SCIENTIFIC WORK AS3013: F90 lecture 2 6 Fortran 90: Standard character set A to Z (upper case), digits 0 to 9, underscore _ special characters: space = + - * ( ) , . ' : " ! % & ; < > ? $ For portability it is advisable to stick to these characters even in character constants, but most computers now use the ASCII character set to represent characters as 8 bit numbers (0-255). additional characters: a to z (lower case), # @ [ \ ] ^ ` { | } Any character acceptable to the computer may be used in CHARACTER constants.

WARNING: Fortran compilers do not distinguish between upper and lower case except in character constants. i.e. ABCDE ≡ abcde ≡ AbCdE ≡ aBcDe, etc.

Other data types: LOGICAL, COMPLEX

AS3013: F90 lecture 2 7 Variable names: (and most other names, e.g. for PROGRAM) • First character must be a letter. • Remaining characters may be any combination of letters, digits or underscore ( _ ) characters. • Maximum length is 31 characters. e.g. A A12 Alpha_3 Next_Month width Variables are names of ‘boxes’ in computer memory where numerical or other data values are stored. We must tell the compiler what type of data they will hold by declaring all variables in Type statements. e.g. old style new style REAL a, b, or REAL :: a, b, c INTEGER count, year INTEGER :: count, year

Use IMPLICIT NONE at the start of each program unit. Use comments to create a variable dictionary. AS3013: F90 lecture 2 8