• Aucun résultat trouvé

Explicit Data Type Declaration

Dans le document FORMAT (3(1) STOP (Page 39-50)

There are two ways you can declare data type explicitly-using the IMPLICIT statement or using explicit type statements.

• Implicitly

In the IMPLICIT statement, the first letter of a name is associated with a particular type. Thus, if you want to declare all names beginning with a certain letter or $ (or range of letters) as a particular type and, optionally, length, use the IMPLICIT statement. For example,

IMPLICIT CHARACTER*15 (C)

would cause a name, CAT, to be given type character, 15 characters long.

The IMPLICIT statement declaration takes precedence over the implied default typing.

• Explicitly

To identify a specific name as a particular type, use an explicit type statement (integer, real, complex, logical, or character). The way to explicitly accomplish the same thing shown in the above example for the variable CAT is as follows:

CHARACTER*15 CAT

An explicit specification takes precedence over an IMPLICIT declaration.

Typing Groups of Names-IMPLICIT Statement

Using the IMPLICIT statement, you can explicitly specify the data types for names beginning with specific letters. For example, if you specify:

IMPLICIT DOUBLE PRECISION (A-C, F) IMPLICIT LOGICAL (E,L),CHARACTER(D,G,H) your program will treat data items as shown below.

NAMES BEGINNING WITH HAVE DATA TYPE A through C, and F DOUBLE PRECISION

E and L LOGICAL

D, G, and H CHARACTER through K, M, N INTEGER

0 through

z~

REAL

mM Extension

HAVE LENGTH 8

4

4 (default) 4 (default)

If you specify an IMPLICIT statement with the following initial letters:

(Y - B)

the compiler performs a "wraparound" scan to find the beginning initial (Y), and the ending initial (B)-which is lower in the FORTRAN collating sequence than Y.

That is, you are implicitly typing all names beginning with Y, Z, $, A, and B. You'll get a warning message when this situation occurs; however, your program will compile and execute.

'--_ _ _ _ _ _ _ _ _ _ _ End of mM Extension _ _ _ _ _ _ _ _ _ _ -.11 Typing Specific Names-Explicit Type Statements

Explicit type statements declare the data type for specific names in your program.

For such names, you can specify the data type and, optionally, the length. You may optionally specify dimension information for array names. You may also optionally assign initial values for names of every data type.

For example, you can specify:

DOUBLE PRECISION MEDNUM CHARACTER

*

80 INREC /'ABCD'/

Chapter 2. Referencing Data as Variables, Arrays, and Constants

13

INTEGER * 2 REAL*16

IBM Extension COUNTR

BIGNUM, ARRA Y2*4(5,5) As an alternative for MEDNUM, you could specify:

REAL * 8 MEDNUM

~ _ _ _ _ _ _ _ _ _ End of IBM Extension _ _ _ _ _ _ _ _ _ _ -' These statements declare that:

MEDNUM is a real variable 8 bytes long.

INREC is a character variable 80 bytes long and contains ABCD followed by 76 blanks.

COUNTR is an integer variable 2 bytes long.

BIGNUM is a real variable 16 bytes long.

ARRA Y2 is a two-dimensional array, with elements 4 bytes long (specified by

*4). There are five elements in each dimension (specified by (5,5» for a total of 25 elements. Arrays are explained in "Arrays and Subscripts" on page 20.

If you specify an IMPLICIT statement in the same program as these explicit type statements-such as one of the following:

IMPLICIT DOUBLE PRECISION (A-C, F) IMPLICIT LOGICAL (E,L) ,CHARACTER (D,G,H)

the explicit type statements override the IMPLICIT statement specifications, and in your program:

INREC is a character variable 80 bytes long, not an integer variable 4 bytes long.

MEDNUM is a real variable 8 bytes long, not an integer variable 4 bytes long.

But all other names beginning with I or M represent integers that are each 4 bytes long.

BIGNUM is a real variable 16 bytes long.

ARRA Y2 is a two-dimensional array, with elements that are 4 bytes long (specified by *4). There are five elements in each dimension (specified by (5,5».

COUNTR is an integer variable 2 bytes long.

But all other names beginning with A, B, or C represent real variables 8 bytes long.

Constants

Defining Constants by Value

A constant is a datum in a program that has a fixed, unvarying value. You can refer to a constant by its value, or you can name the constant and use the name in all program references.

The constants you can use are:

• Arithmetic (integer, real, complex or double precision)-use arithmetic constants for arithmetic operations, and to initialize integer, real, complex, or double precision variables, and as arguments for subroutines, and so forth.

• Logical-use logical constants in logical expressions, and to initialize logical variables, and as arguments for subroutines, and so forth.

• Character-use character constants in character and relational expressions, and to initialize character variables, and as arguments for subroutines, and so forth.

IBM Extension

• Hollerith-use Hollerith constants as data only in FORMAT statements and in initialization other than character initialization.

• Hexadecimal-use hexadecimal constants to initialize items.

• Literal (FORTRAN66 only)-similar in usage to character constants.

'--_ _ _ _ _ _ _ _ _ _ End of mM Extension _ _ _ _ _ _ _ _ _ _ ...

You can use constants in your program by merely specifying their values. For example:

eIRe =2*PI*RAD or

eIRe =2.0*PI*RAD

where the value 2 represents an integer constant of that value, and where the value 2.0 represents a real constant of that value.

You can specify all types of constants in this way:

• Arithmetic Constants--integer, real, complex, or double precision

Integer Constant-written as an optional sign followed by a string of digits.

For example:

-12345 12345

Chapter 2. Referencing Data as Variables, Arrays, and Constants

15

Real Constant-can take two forms:

1. Basic Real Constant-written as an optional sign, followed by an integer part (made up of digits), followed by a decimal point, followed by a fraction part (made up of digits). Either the integer or fraction part can be omitted. In the latter case, a decimal point is not necessary. For example:

+123.45 0.12345

2. Basic Real Constant with Real Exponent-written as a basic real constant followed by a real exponent; the real exponent is written as one of the letters D, E, or Q, followed by a 1- or 2-digit integer constant. Optionally, the exponent can be signed. (See D exponent below for example of double precision constant.) For example:

0.12345E+2 E exponent (which occupies four storage positions and has the value + 12.345; the precision is approximately 6 decimal digits)

0.12345D-03 D exponent (which occupies eight storage positions and has the value +0.00012345; the precision is approximately 15 decimal digits)

IBM Extension

-1234.5Q03 Q exponent (which occupies 16 storage positions and has the value -1,234,500; the precision is approximately 32 decimal digits)

'--_ _ _ _ _ _ _ _ _ End of IBM Extension _ _ _ _ _ _ _ _ _ _ ...

Complex Constant-written as a left parenthesis, followed by a pair of integer constants or real constants separated by a comma, followed by a right parenthesis.

The first integer or real constant represents the real part of the complex number; the second integer or real constant represents the imaginary part of the complex number. The real and imaginary parts need not be of the same precision; the smaller part is made the same precision as the larger part. For example:

(123.45,-123.45E2) (has the value + 123.45-12345i; both the real and imaginary parts have lengths of 4)

( 123.45,-123.45D2)

12345,-123.45Q2)

IBM Extension

(has the value

+

123.45-12345i; the real part is 4 bytes long, the imaginary part is 8 bytes long) (The real part (a real constant) is converted from 4 bytes long to a real constant that is 8 bytes long.)

(has the value

+

12345-12345i; the real part is 4 bytes long, the imaginary part is 16 bytes long) (The real part (an integer constant) is converted from 4 bytes long to a real constant that is 16 bytes long.)

""'""-_ _ _ _ _ _ _ _ _ End of IBM Extension _ _ _ _ _ _ _ _ _ _ ...

Note: In these examples, the character i has the value of the square root of -1.

Logical Constant-written as .TRUE. or .FALSE. in expressions. (In input/output statements, you can use T or F as abbreviations.)

IBM Extension

(You can also use T and F as abbreviations in the DATA initialization statement. )

'--_ _ _ _ _ _ _ _ _ _ End of IBM Extension _ _ _ _ _ _ _ _ _ _ ...

For a logical item named COMP, you can specify, for example:

LOGICAL CaMP COMP=.FALSE.

This sets the logical item COMP to the value "false."

• Character Constant-a string of characters, enclosed in apostrophes. The character string can contain any characters in the computer's character set.

For example:

'PARAMETER = ' 'THE ANSWER IS:'

"'TWAS BRILLIG AND THE SLITHY TaVES'

Note: If you want to include an apostrophe within the character constant, you code two adjacent apostrophes, as shown in the last example, which is

displayed as:

'TWAS BRILLIG AND THE SLITHY TaVES

Chapter 2. Referencing Data as Variables, Arrays, and Constants

17

IBM Extension

• Hollerith Constant-valid only in a FORMAT statement. It is written as an integer constant followed by the letter H, followed by a string of characters.

The character string can contain any characters in the computer's character set.

For example:

100 FORMAT(I3,11H

=

THE NORM)

200 FORMAT (2D8.6, 18H ARE THE 2 ANSWERS)

VS FORTRAN now implements Hollerith constants for the FORMAT and DATA statements, for noncharacter data type, and for subroutine and function calls. For example:

DATA INT2/2HEF/, REAL4/4HABCD/

RESLT = FUNCT1 ( A,B, 4H1234)

However, Hollerith constants in a DATA statement are not recommended.

• Hexadecimal Constant-written as the character Z, followed by a hexadecimal number, made up of the digits 0 through 9 and the letters A through F. You write a hexadecimal constant as 2 hexadecimal digits for each byte.

You can use hexadecimal constants only in a DATA statement to initialize data items of all types and in explicit type statements that allow initialization.

REAL *4 TEMP

DATA TEMP/ZCIC2C3C4/

• Literal Constant-in FORTRAN 66, performs functions similar to the FORTRAN 77 character constant, and the Hollerith constant. (Reference documentation is contained in IBM System/360 and System/370 FORTRAN IV Language.)

1 . -_ _ _ _ _ _ _ _ _ _ End of IBM Extension _ _ _ _ _ _ _ _ _ _ ...

Defining Constants by Name-PARAMETER Statement

If your program uses one constant frequently, you can use the PARAMETER statement to name the constant and assign it a value. You can do this once, before your program first uses the constant, and then refer to the constant, wherever it's used, by its name.

There are two advantages in handling constants this way:

• The name for the constant can be a meaningful name-which makes the logic of the program easier to understand by someone doing maintenance updates.

• If the value of the constant must be changed, you can change it once, in the PARAMETER statement, and all references throughout the program are updated.

Variables

Use the PARAMETER statement to assign names and values to constants. For example:

CHARACTER *5 C1,C2

PARAMETER (C1='DATE ',C2='TIME ',RATE=2*1.414)

The character explicit type statement defines items C 1 and C2 as character· items of length 5. The PARAMETER statement then defines these items as named program constants:

Cl has the value "DATE ." The constant is five characters long; the blank following the word DATE is part of the constant.

C2 has the value "TIME ." The constant is five characters long; the blank following the word TIME is part of the constant.

RATE is defined implicitly as a REAL*4 item. Therefore, it's a real constant, four storage positions long, with a value of 2 times 1.414 or 2.828.

You'll note that RATE is defined through the expression 2*1.414; when you define a constant using an expression in this way, the expression you specify must be a constant expression, and can contain no implicit or explicit function references.

In the PARAMETER statement, the value you assign to the constant must be consistent with its data type; that is, Cl and C2 must contain character data, and RATE must contain real data. If any data conversions must be performed, they are made according to the rules for the assignment statement. (To find out what the

"assignment statement" is, see" Assigning Values to Variables, Array Elements, -and Character Substrings" on page 50.)

The value you assign through a PARAMETER statement to a character constant must contain no more than 255 characters.

A variable is a named data location occupying a storage area. The value of a variable can change during program execution.

The first occurrence of a variable name defines the variable; no specific definition statement is needed.

The first letter of the name of a variable determines its data type, as described in

"Implied Default Data Type Declaration" on page 12, or you can define its data type explicitly, as described in "Explicit Data Type Declaration" on page 12.

The value contained in a variable is always the current value stored there. Before you've assigned a value to a variable, its contents are undefined. You can set an initial value into a variable using the DATA statement; alternatively, your first executable statement referring to it (for example, a READ statement or an assignment statement) can assign a value to it.

Chapter 2. Referencing Data as Variables, Arrays, and Constants

19

Arrays and Subscripts

One-Dimensional Arrays

An array is a named set of consecutive data locations that have the same data type and length.

In FORTRAN, you assign a name to the entire array and then refer to each of the individual locations, called array elements, by specifying its position within the array through one or more subscripts, depending upon the number of dimensions in the array.

You can define an array by using the DIMENSION statement, the explicit type statement, or the COMMON statement.

To define a one-dimensional array, you specify only one dimension declarator. For example, you want to define a one-dimensional array, named ARRAY 1 , that contains five array elements. You can do so through the following DIMENSION declaration:

DIMENSION ARRAY1 (5)

In this case, you've defined ARRA Yl as an array containing five elements, each implicitly defined a,s a real item 4 bytes long.

Subscript Re/eI'eIICI!S: Program references to ARRA Yl take the form of subscripts:

• As integer constants:

ARRAY1 (2)

In this example, the subscript specifies a refet:ence to the second array element.

• As integer variables:

ARRAY1 (NUM)

where (NUM) represents the integer variable subscript, which can be assigned values from 1 through 5. (For ARRA Yl, any other values produce invalid array references.)

mM Extension

You can also specify subscript references as real constants, variables, or expressions; the compiler converts the real value to an integer value.

L..-_ _ _ _ _ _ _ _ _ _ End of mM Extension _ _ _ _ _ _ _ _ _ _ _

Multidimensional Arrays

In VS FORTRAN, arrays can have up to seven dimensions; that is, you can specify up to seven dimension declarators to define the array, and up to seven subscripts to identify a specific array element. (The number of subscripts you specify must always equal the number of dimensions in the array.)

Multidimensional arrays are stored in column-major order; that is, the first subscript always varies most rapidly, and the last subscript always varies least rapidly.

For example, if you define the 3-dimensional array REAL*4 ARR3(2,2,2)

it's placed in storage in the order shown in Figure 2. In this example, the lower bounds of the subscripts are 1; therefore, the first array element is (1,1,1); you'd refer to the second array element as ARR3(2,1,1), and you'd refer to the seventh array element as ARR3 (1,2,2).

Arrays-Implicit Lower Bounds

In the preceding examples, the subscripts are shown as having a range from 1 through the upper bound for each dimension of the array; that is, in ARR3, the implicit lower bound for each dimension is 1, and the explicit upper bound for each dimension is 2.

Arrays-Explicit Lower Bounds

In VS FORTRAN, you can also explicitly state both the lower and upper bounds for any array. For example, for ARR3A you could specify:

DIMENSION ARR3A(4:5,2:3,1:2)

The layout in storage (as shown in Figure 2) is exactly the same as for ARR3;

however, valid array references would range from ARR3A(4,2,1) through ARR3A(5,3,2).

ARR3-Implicit Lower Bounds

ARR3A-Explicit Lower Bounds

Figure 2. Three-Dimensional Array-1mp6cit and Exp6cit Lower Bounds

Chapter 2. Referencing Data as Variables, Arrays, and Constants

21

Arrays-Signed -Subscripts

In VS FORTRAN, your array declaration can specify positive or negative signed declarators for either the lower or the upper bounds. This can make a difference in the number of array elements the array contains.

For example, if you define ARR2 and ARR2S as follows:

DIMENSION ARR2(4,2), ARR2S (-2:2,2)

the two arrays are laid out in storage as shown in Figure 3:

• Valid array references for ARR2 range from ARR2(1,1) through ARR2(4,2), and there are eight array elements.

• Valid array references for ARR2S range from ARR2S(-2,1) through

ARR2S(2,2) (with ARR2S(O,1) and ARR2S(O,2) included), and there are ten array elements.

Because a zero subscript is valid for ARR2S, there are two more array elements in ARR2S than in ARR2.

ARR2(4,2)-is arranged in storage like this:

I

1, 1

I

2, 1

I

3, 1

I

4, 1

I

1,2

I

2,2 3,2 4,2

ARR2S(-2:2,2)-is arranged in storage like this:

I

-2, 1

I -

1 , 1

I

0, 1

I

1, 1

I

2, 1

I

-2, 2

I -

1 , 2 0 , 2 1,2 2,2

Figure 3. Arrays--Effect of Negative Lower Bounds

Arrays--Execution-Time Considerations

When performance is critical, as in inner loops, specify arrays as one-dimensional rather than multidimensional. The fewer the dimensions in an array, the faster your array references execute. Always be sure that subscript values refer to elements within the bounds of the array; if they don't, you may destroy data or instructions.

For large arrays of more thtHi a few thousand elements, the program should endeavor to vary the first subscript

ttlost

tapidly in order to localize the reference pattern and prevent excessive "pagirtg." See the example under "Processing Multidirh.ensional Arrays-Nested

bo

Statement" on page 63.

Dans le document FORMAT (3(1) STOP (Page 39-50)