Unformatted text preview:

Chapter 4 A Terse Introduction to Fortran 1 of 73 Fortran is not a flower but a weed It is hardy occasionally blooms and grows in every computer A Perlis In 1954 a project was begun under the leadership of John Backus at IBM to develop an automatic programming system that would convert programs written in a mathematical notation to machine instructions for the IBM 704 computer Many were skeptical that the project would be successful It was necessary for the program produced by the compiler to be almost as efficient as that produced by a good assembly language programmer First compiler in 1957 Successful Several ANSI standards 1966 Fortran 66 1978 Fortran 77 1990 Fortran 90 2 of 73 4 1 Source form Traditionally Fortran statements had a fixed format Columns 1 5 label a number Column 6 Continuation mark Columns 7 72 statement A C in column 1 indicates that the whole line is a comment Examples of Fortran statements C This is a comment line 10 I 1 DO 15 J I 10 A J 0 15 CONTINUE C Now a very long statement CALL SUBSUB X Y Q W 3 of 73 Free format is accepted by most Fortran compilers today Up to 132 characters per line are allowed in Fortran 90 An ampersand indicates line continuation For example CALL SUBSUB X Y Q W The exclamation mark is often used to indicate the beginning of a comment i 1 This is a comment 4 of 73 Lower case letters are considered equal to the corresponding upper case letters except inside a character constant Thus aBc 1 is the same as ABC 1 But aBc is not equal to ABC 5 of 73 4 2 Data types There are six basic data types in Fortran 77 1 Integer 2 Real 3 Double precision 4 Complex 5 Logical 6 Character Integer and real constants are similar to those in C Double precision constants are similar to real constants with exponents but a D is used instead of an E 1 D0 3 527876543D 4 Complex constants are pairs of reals or integers enclosed in parentheses and separated by a comma The first number is the real part and the second the imaginary part 1 23 23e1 Logical constants are TRUE and FALSE 6 of 73 The form of a character constant is an apostrophe followed by a nonempty string of characters followed by an apostrophe Two consecutive apostrophes represent the apostrophe abc a b 7 of 73 4 3 Variables Variables in Fortran 77 start with a letter contain only letters and digits and are no more than six characters long In Fortran 90 up to 31 characters are allowed and the underscore can form part of the variable name The type of variables is specified either explicitly in a type declaration statement or is implicit Examples of type declaration statements real a b c 10 10 integer i j k 100 double precision complex logical q p r character s t 5 u 20 20 character 10 v w y The num in the character statement specifies the length of the variable in number of characters It applies to the whole list if it appears after the character keyword or to the variable if it appears after a variable name 8 of 73 A scalar variable does not have to be declared in Fortran The first letter of an undeclared variable indicates its type Variables starting with letter I thorough N INteger are of type integer variables All other undeclared variables are of type real This default option can be changed with the IMPLICIT statement An array declarator is a variable name followed by a specification of the dimensions The dimension specification has the form d d In the main program the dimensions are specified using integer constants They could be a single integer say n meaning that the possible subscripts are from one to n Also they could be a pair of integers say m n meaning that the values of the subscripts for that dimension have to be between n and m 9 of 73 A declarator can also appear in a dimension statement Thus real a dimension a 10 10 b 5 15 5 is equivalent to b is implicitly declared as real real a 10 10 b 5 15 5 Note Dimension declarators for subroutine parameters can contain variables or can be the special character 10 of 73 4 4 Expressions Expressions in Fortran can be arithmetic relational logical and character Arithmetic expressions are similar to those in C except that in Fortran there is an exponentiation operator which has the highest precedence Thus the right hand side expression in the statement a 2 b 3 1 has the value 2b 3 1 Implicit type conversion is also similar to that of C The only character operator is concatenation AB DE 11 of 73 Relation operators compare the values of expression and produce a logical value of true or false The relation operators have lower precedence than arithmetic operators The relation operators are LT Less than LE Less than or equal to EQ Equal to NE Not equal to GT Greater than GE Greater than or equal to For example if a b gt c 1 then logical q q t 1 gt 5 if q then 12 of 73 The logical operators are NOT Logical negation AND Logical Conjunction OR Logical inclusive disjunction EQV Logical equivalence NEQV Logical nonequivalence 13 of 73 4 5 Fortran statements We will only discuss a few of the statements 1 GOTO statement e g GO TO 150 2 IF statement if a gt 5 a 5 if a gt 5 then a 5 b 1 end if if a gt 5 then a 5 b 1 else b 2 go to 17 end if 14 of 73 3 DO statement Iterative statement do 10 i 1 n do 15 j 1 n a i j 0 15 continue 10 continue do 10 i 1 n do 10 j 1 n 10 a i j 0 do i 1 n this form is not accepted by all compilers do j 1 n a i j 0 end do end do 15 of 73 2 do 2 i 1 n 2 This do loop updates elements 1 3 5 b i 1 continue 4 CALL statement For subroutine invocation call subsub a b 1 5 35 true 16 of 73 4 6 Program Units A Fortran program always includes a Main Program and may include subroutines and functions The generic name for the main program subroutines and functions is program unit There are three classes of functions in Fortran intrinsic functions statement functions and external functions All functions are referenced in the same way A function reference is always within an expression and has the form fun a a where the a s are the actual arguments For example the following expression contains two references to functions sin x root f a b y In this example sin and root are function names and x f a and b are actual arguments The actual arguments can be …


View Full Document
Loading Unlocking...
Login

Join to view A Terse Introduction to Fortran and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view A Terse Introduction to Fortran and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?