External routines. Internal subroutines. Interfaces. Give an example

DIMENSION attributes and ALLOCATABLE. Description arrays. Give an example.

A Fortran uses the DIMENSION attribute to declare arrays.

The DIMENSION attribute requires three

components in order to complete an array

specification, rank, shape, and extent.

The rank of an array is the number of “indices”or “subscripts.” The maximum rank is 7 (i.e., seven-dimensional).

The shape of an array indicates the number ofelements in each “dimension.”

The extent is written as m: n, where m and n (mn) are lower and upper bounds. Each dimension has its own extent. An extent of a dimension is the range of its

index. If the lower bound is omitted, the default is 1.

The DIMENSION attribute has the following form:

DIMENSION(extent-1, extent-2, …, extent-n)

Here, extent-i is the extent of dimension i. This means an array of dimension n (i.e., n indices) whose i-th dimension index has a range given by extent-i.

For example, if we write DIMENSION(0:2,3), it will mean that there is a 2-dimensional array (i.e., a table). Possible values of the first index are 0,1,2 and the second 1,2,3.

Array declaration is simple. Add the DIMENSION attribute to a type declaration.

In many situations, one does not know exactly the shape or extents of an array. As a result, for declaring array we can use the ALLOCATABLE attribute.

The ALLOCATABLE attribute indicates that at the declaration time one only knows the rank of

an array, but not its extent. Therefore, each extent has only a colon.

INTEGER,ALLOCATABLE,DIMENSION(:):: a

REAL,ALLOCATABLE,DIMENSION(:,:):: b

The type of such arrays called dynamic arrays.

The operator describing the array can be without an attribute DIMENSION, shape of the array can be indicated immediately after the identifier, for example:

REAL X(10, 20, 30), Y(100), Z(2, 300, 2, 4)

As mentioned, this form is convenient for simultaneous descriptions of different forms of arrays, but is undesirable if described arrays have any attributes.

External routines. Internal subroutines. Interfaces. Give an example.

External routines typically used to perform specific tasks

within the overall objectives of the program as a whole. External routines are called from the main program or other subprograms and at completion research return control to the calling component.

The structure of the outer subroutine similar to the structure of the main program, except title:

subroutine_ stmt

[ specification_part ]

[ execution_part ]

[ CONTAINS

internal_routines…]

end_subroutine stmt

Example of the external routine:

SUBROUTINE outer

REAL:: x, y

:

CONTAINS

SUBROUTINE inner

REAL y

y = x + 1.

:

END SUBROUTINE inner! SUBROUTINE mandatory

END SUBROUTINE outer

Internal routines in contrast to external can not contain their own internal routines. Internal subroutine will automatically receive access to all facilities of a carrier, including other internal routines. All requests to internal routines may be carried out only from the component of the carrier. Example of the internal routine:

PROGRAM main

IMPLICIT NONE

REAL:: X, Y, S

CALL sub(X, Y, S)

PRINT*, S

End PROGRAM main

SUBROUTINE inner_Sub(A, В, С)

REAL:: А, В, С

С = SQRT(A**2 + B**2)

END SUBROUTINE inner_sub

A subroutine is referenced by a CALL statement:


Понравилась статья? Добавь ее в закладку (CTRL+D) и не забудь поделиться с друзьями:  



double arrow
Сейчас читают про: