SPEIFIATION 2 STATEMENTS hapter SYS-ED/ OMPUTER EDUATION TEHNIQUES, IN.
Objectives: You will learn: Type specification. Type declarations other than HARATER. Statements: HARATER, IMPLIIT, DIMENSION, PARAMETER, EXTERNAL, OMMON, EQUIVALENE. Internal storage structure for arrays. hanging a value. SYS-ED \OMPUTER EDUATION TEHNIQUES, IN. (FORTRAN PRG - 2.02) h 2: Page i
1 Type Specification The type specification statements are used to assign a type to specific name and provide the compiler with information on the structure required for these names. This is necessary because: OMPLEX and DOUBLE PREISION REALS are both floating point format and require two arithmetic storage units per data element. INTEGER, REAL, and LOGIAL require one arithmetic storage unit per data element. HARATER items require one or more character storage units per data element depending on their length. DOUBLE PREISION OMPLEX items are floating point and require four arithmetic storage units per data element. SYS-ED \OMPUTER EDUATION TEHNIQUES, IN. (FORTRAN PRG - 2.02) h 2: Page 1
Type declarations are not executable and must precede the first executable statement in the program or subprogram. When dimension information appears in a declaration statement, the associated name becomes the name of an array, and the product of the declared sizes determines the amount of storage to be reserved for that array. No DIMENSION statement is needed if a name is dimensioned in a type statement. However, a name may be associated with dimension information in a DIMENSION statement and also appear in type statement without dimension information. Example: DIMENSION NONE(12) REAL MOST, NONE SYS-ED \OMPUTER EDUATION TEHNIQUES, IN. (FORTRAN PRG - 2.02) h 2: Page 2
The general form of this type declaration statement is: typ type-list 1.1. Type Declarations Other than HARATER where: typ is INTEGER, REAL, DOUBLE PREISION, OMPLEX, or LOGIAL, A type-list is a string of names separated by commas, with or without dimensions, separated by commas. Names may represent variables, arrays, symbolic constants, functions, and dummy procedures. Examples: OMPLEX DATA, DRIVE, IMPORT REAL INTENT, MPH, ZERO INTEGER FAR(60), AGE, YEAR, DATE LOGIAL MODEL, METHOD DOUBLE PREISION UNSURE, ALPHA(20,20), PLATE OMPLEX*16 VALS(100) SYS-ED \OMPUTER EDUATION TEHNIQUES, IN. (FORTRAN PRG - 2.02) h 2: Page 3
The general form of this type of declaration statement is: HARATER*n type-list 2 HARATER Statement where: type-list is a list of names, with optional dimension information and optional length specifications "*n", separated by commas. n is the optional length specification (if n is omitted, so is *), and may be any of the following: an unsigned, non-zero, INTEGER constant. a positive valued integer constant expression enclosed in parentheses. an asterisk in parentheses. A length specification immediately following the word HARATER sets the default length for all items in the list. If no length specification is given, the default is length one. A length specification immediately following a list item applies only to that item and overrides the list default. SYS-ED \OMPUTER EDUATION TEHNIQUES, IN. (FORTRAN PRG - 2.02) h 2: Page 4
Example: HARATER*5 ALPHA(8), TEXT, WORD34*12, WORDY*(6+3) ALPHA is a HARATER array of eight elements, each of which has length five. TEXT is a HARATER variable of length five. WORD34 is a HARATER variable of length twelve. WORDY is a HARATER variable of length nine. The length (*) may only be used for list items which are symbolic constant names, dummy arguments, or names which appear in a FUNTION, SUBROUTINE, or ENTRY statement. SYS-ED \OMPUTER EDUATION TEHNIQUES, IN. (FORTRAN PRG - 2.02) h 2: Page 5
3 IMPLIIT Statement The IMPLIIT statement is used to change or confirm the default implicit type associated with the first letter of a name. For example, the statement IMPLIIT INTEGER (W) would make INTEGER the implicit default type for names beginning with W. The general form of the IMPLIIT statement is: IMPLIIT type(r1,r2,...),type(...),... where: type is INTEGER, REAL, DOUBLE PREISION, OMPLEX, LOGIAL, HARATER, or HARATER*n (where n is the length of HARATER variables). rx is a single letter or range of letters in alphabetical order. A range is written as, for example, E-H. SYS-ED \OMPUTER EDUATION TEHNIQUES, IN. (FORTRAN PRG - 2.02) h 2: Page 6
An explicit type declaration overrides the type given by the IMPLIIT statement. An implicit type derived from the first letter of the name is used if the first letter does not appear in an IMPLIIT statement. The IMPLIIT statement only affects names in the program unit which contains it. With one exception, IMPLIIT statements must precede all other declaration statements in a program unit. The single exception is that PARAMETER statements may precede IMPLIIT statements; in such cases, the IMPLIIT statements have no effect on the type of the names used in the PARAMETER statements. A common use of the IMPLIIT statement is to declare all names to be implicitly LOGIAL (by IMPLIIT LOGIAL (A-Z)), and then to type explicitly all names used. This effectively renders FORTRAN more 'strongly typed' and gives the compiler more chance to flag the use of mis-spelt variables. Examples: IMPLIIT INTEGER(I-N),REAL(-H),DOUBLE PREISION(O-W) IMPLIIT OMPLEX(X,Y,Z),LOGIAL(B),HARATER*5(A) SYS-ED \OMPUTER EDUATION TEHNIQUES, IN. (FORTRAN PRG - 2.02) h 2: Page 7
4 DIMENSION Statement The programmer determines the size and attributes (number of subscripts and their bounds) of an array by reserving storage for the array in a DIMENSION statement, OMMON statement, or a type declaration statement. The form of the DIMENSION statement is: DIMENSION vl(dl),v2(d2),... where: The variable names, vj, have dimensions dj with one to seven dimension declarations separated by commas, as in SPAE(5,6,20). The DIMENSION statement is not executable and it must precede the first executable statement of a program or subprogram. The dimension declaration of a name must also precede the first use of the name in a DATA statement. It is not permitted to dimension the same array name more than once in a program unit. SYS-ED \OMPUTER EDUATION TEHNIQUES, IN. (FORTRAN PRG - 2.02) h 2: Page 8
Array elements are stored by columns in ascending locations. In the array declared as A(3,3,3): 4.1. Internal Storage Structure for Arrays A111 A121 A131 A211 A221 A231 A311 A321 A331 A112 A122 A132 A212 A222 A232 A312 A322 A332 A113 A123 A133 A213 A223 A233 A313 A323 A333 SYS-ED \OMPUTER EDUATION TEHNIQUES, IN. (FORTRAN PRG - 2.02) h 2: Page 9
5 OMMON Statement The OMMON statement is used to reserve a block of storage which can be accessed by different program units. This allows the different program units to define and reference the same data in the same storage locations without including the data as arguments in subprogram references. The OMMON statement also provides the user with the capability of forcing specific data elements to be stored in adjoining memory locations. The storage area reserved by a OMMON statement is called a common block. ommon blocks may be unlabelled or labeled (given a name). Example: OMMON ROSE(20), TULIP, PANSY, DAF OMMON /ZOO/ WOMBAT(20,30), IBEX(2), LION, MOOSE DIMENSION PANSY(100), MOOSE(18) SYS-ED \OMPUTER EDUATION TEHNIQUES, IN. (FORTRAN PRG - 2.02) h 2: Page 10
A statement which reserves the unlabelled (or blank) common block has the form: OMMON nlist And a statement which reserves labeled common has the form: OMMON /cbname1/ nlist1 /cbname2/ nlist2... where: cbnamej are common block names. nlistj are lists of variable names and array names (with or without dimension information) separated by commas. When subject to the following restrictions, a common block name may be any FORTRAN name and may be used elsewhere in a program as another kind of name: A common block name may not be the name of any symbolic constant referenced in the same program unit. A common block name may not be the name of an EXTERNAL function or subroutine referenced from the program unit. SYS-ED \OMPUTER EDUATION TEHNIQUES, IN. (FORTRAN PRG - 2.02) h 2: Page 11
The OMMON statement is not executable and must appear before the first executable statement in a program or subprogram. The order of DIMENSION, OMMON, EQUIVALENE, or type declarations is immaterial. Any number of OMMON statements may appear in a program unit. If a HARATER variable or array is in a common block, all the data elements in that common block must be of type HARATER. The length of a common block is determined from the number and type of the list variables. SYS-ED \OMPUTER EDUATION TEHNIQUES, IN. (FORTRAN PRG - 2.02) h 2: Page 12
6 EQUIVALENE Statement The EQUIVALENE statement is used to designate names which will share storage. EQUIVALENE is most commonly used when two or more arrays can share the same storage or when individual elements of an array are given specific names. The general form of the EQUIVALENE statement is: EQUIVALENE (nlist1), (nlist2),... where: nlistj is a list of variable names, array names, array element names or character substring names: (name1, name2,...) and each equivalence list must contain at least two names. Neither dummy arguments nor variables representing function names defined with current subprograms are allowed. Only integer constant expressions may be used as subscripts or substring expressions. Example: DIMENSION A(20), B(10), (12) EQUIVALENE (A,B), (A(16),(2)) The above EQUIVALENE statement associates: A(1) to A(10) with B(10). A(16) to A(20) with (2) to (6). SYS-ED \OMPUTER EDUATION TEHNIQUES, IN. (FORTRAN PRG - 2.02) h 2: Page 13
EQUIVALENE is not executable and must appear before the first executable statement. EQUIVALENE does not cause type conversion if the equivalenced names have different data types; nor does it imply mathematical equivalence; nor does a variable equivalenced to an array have the properties of an array. HARATER variables may only be equivalenced with other HARATER variables, but the lengths need not be equal. If the lengths of associated HARATER variables are not the same, their association in an EQUIVALENE statement may cause other associations, as in the following statements: HARATER A*6, B*6, (2)*4 EQUIVALENE (A,(1)), (B,(2)) This causes A and (1) to begin at the same first character position and also causes B and (2) to begin at the same character position. Since the subscripted variables are stored contiguously, although A and B have not been explicitly associated, the last two characters of A and the first two characters of B will always be identical. SYS-ED \OMPUTER EDUATION TEHNIQUES, IN. (FORTRAN PRG - 2.02) h 2: Page 14
6.1. Rules for EQUIVALENE Any variable may be made equivalent to any other variable, provided: Rule 1 Rule 2 Rule 3 It causes no rearrangement of the order in any common block, No common block is extended backwards from the first name explicitly declared in that common block. The same storage location is not referenced more than once in an equivalence list. Examples: Example 1: OMMON A(5), B(3) DIMENSION D(4) EQUIVALENE (A(5), D(1)), (B(2), D(3)) is legal and causes the associations: A(1) A(2) A(3) A(4) A(5) D(1) B(1) D(2) B(2) D(3) B(3) D(4) SYS-ED \OMPUTER EDUATION TEHNIQUES, IN. (FORTRAN PRG - 2.02) h 2: Page 15
Example 2: OMMON A(5), B(3) DIMENSION D(4) EQUIVALENE (A(4), D(1)), (B(1), D(4)) is illegal by rule 1. Example 3: EQUIVALENE (F,D(1)),(F,D(3)) is illegal by rule 3. Example 4: DIMENSION FAT(6) OMMON /IFAT/ SKINNY EQUIVALENE (SKINNY,FAT(2)) is illegal by rule 2. However, EQUIVALENE (SKINNY, FAT(1)) is legal. SYS-ED \OMPUTER EDUATION TEHNIQUES, IN. (FORTRAN PRG - 2.02) h 2: Page 16
6.2. hanging a Value The use of the EQUIVALENE statement to cause sharing of storage locations requires a knowledge of which FORTRAN statements cause a new value to be stored in a location. There are nine categories of such statements: Assignment statements store a new value in the variable on the left side of the equal sign. ASSIGN i TO n statements store a new value in n. A DO statement or an implied-do in an input/output list stores a new indexing value. READ statements store new values in the variables mentioned in the input list. Execution of input/output statements containing status specifications defining that variable. Execution of an INQUIRE causes certain entities to be defined. Equivalenced locations become defined when those locations are defined. Subprograms may change the value of an argument or variable in common. DATA statements cause locations to become initially defined. SYS-ED \OMPUTER EDUATION TEHNIQUES, IN. (FORTRAN PRG - 2.02) h 2: Page 17
7 PARAMETER Statement The PARAMETER statement is used to give a symbolic name to a constant. This is useful when a symbolic name would be more meaningful than a numeric constant, also when the user wants to guarantee that a name will not have its value changed by legal methods. Use of the PARAMETER statement also has the advantage of easing the updating of the program; a simple edit of one line will change the value of a constant throughout the program unit. Example: INTEGER SIZE PARAMETER (PLANK = 6.6256E-27, SIZE=200) where: The form of the PARAMETER statement is: PARAMETER (name1 = const1, name2 = const2,...) namej is a FORTRAN name. constj is a constant expression. Each namej is the name of a constant which is defined with the value of the corresponding constj. Each constj is a constant expression containing only constants or previously defined symbolic constants. The expression constj is evaluated during compilation and converted to the type of namej; however, LOGIAL and HARATER names must have corresponding typed expressions. SYS-ED \OMPUTER EDUATION TEHNIQUES, IN. (FORTRAN PRG - 2.02) h 2: Page 18
Only one definition of a name by a PARAMETER statement is permitted in a program unit. With the following restrictions, a symbolic constant can appear anywhere that a constant of the same type could appear: A symbolic constant cannot appear as part of a FORMAT statement or a FORMAT specification. A symbolic constant must not be used to form part of another constant. For example, it may not be the integer, fraction, or exponent part of a REAL constant. If the type of a symbolic name of a constant is not the default type implied by its first letter, its type must be specified by a type statement or IMPLIIT statement prior to its appearance in a PARAMETER statement. The symbolic name may not appear in any other declaration statement except as part of array dimension information. If the symbolic name is to be HARATER and its length is not the default length of one, the length must also be specified before the appearance of the name in a PARAMETER statement; its length must not be changed by subsequent statements. SYS-ED \OMPUTER EDUATION TEHNIQUES, IN. (FORTRAN PRG - 2.02) h 2: Page 19
Examples: Example 1: PARAMETER (N=100) REAL A(N,N),B(2*N+1,N-1) DO 10 I = 1,N A(I,I) = D(I) 10 ONTINUE Example 2: INTEGER VALID(5), BEGIN PARAMETER (BEGIN=3) PARAMETER (TWO = 2.0, LAST = 3*BEGIN + 4) DATA VALID /0, 4*BEGIN/ OMMON /SHARE/ A(LAST), D(8) SYS-ED \OMPUTER EDUATION TEHNIQUES, IN. (FORTRAN PRG - 2.02) h 2: Page 20
8 EXTERNAL Statement When the actual argument list of a subprogram reference contains a name which will itself be used as a subprogram name it must be declared in an EXTERNAL statement in the calling routine. Example: SUBROUTINE PRIMES EXTERNAL MODULO... PRINT *, MULTI(K, MODULO, L1, L2)... END FUNTION MULTI(K, F, M1, M2) MULTI = K * F(M1,M2) END SYS-ED \OMPUTER EDUATION TEHNIQUES, IN. (FORTRAN PRG - 2.02) h 2: Page 21
The compiler treats F as a function in the function MULTI because F was not dimensioned as an array in MULTI. However, it is necessary to declare MODULO as a function in subroutine PRIMES so that the compiler will not treat it as a variable in the reference to MULTI. The EXTERNAL statement declares MODULO to be an external procedure (that is a function or subroutine). MULTI could be referenced elsewhere with a function other than MODULO as its second argument; MULTI would then be computed using that function instead of MODULO. The form of the EXTERNAL statement is: EXTERNAL name1, name2,... where: namej is a function, subroutine, or dummy argument name. EXTERNAL is non-executable and must appear before the first executable statement in a program unit. A function name used as an actual argument requires an EXTERNAL statement. However, a function reference used as an actual argument does not need to be in an EXTERNAL statement since it represents a value rather than the name of a function. SYS-ED \OMPUTER EDUATION TEHNIQUES, IN. (FORTRAN PRG - 2.02) h 2: Page 22