Presentation is loading. Please wait.

Presentation is loading. Please wait.

Fortran: Specification Statements Session Six ICoCSIS.

Similar presentations


Presentation on theme: "Fortran: Specification Statements Session Six ICoCSIS."— Presentation transcript:

1 Fortran: Specification Statements Session Six ICoCSIS

2 Outline 1.Implicit Typing 2.Declaring Entities of Different Shapes 3.Named Constants and Constants Expressions 4.Initial Values for Variables 5.Public and Private Attributes 6.The Pointer, Target, and Allocatable Statement 7.The intent and Optional Statement 8.The save Attribute 9.The use Statement 10.Derived-type Definition 11.The Type Declaration Statement 12.Type and Type Parameter Specification 13.Specification Expression 14.The Namelist Statement

3 Introduction The compiler requires information about the entities involved. This information is provided at the beginning of each program unit or subprogram by specification statements.

4 Implicit Typing strong typing Vs. implicit typing In FORTRAN implcit type is assigned according to the initial letter of its name: Entities whose names begin with one of the letters i, j,..., n are of type default integer, and variables beginning with the letters a, b,..., h or o, p,..., z are of type default real. For no implicit typing the statement implicit none is available.

5 Declaring entities of differing shapes integer :: a, b integer, dimension(10) :: c, d integer, dimension(8,7) :: e Fortran permits using a single statement. Whether or not there is a dimension attribute present, arrays may be declared by placing the shape information after the name of the array: integer :: a, b, c(10), d(10), e(8, 7) or (with dimension for default shape) integer, dimension(10) :: c, d, e(8, 7)

6 Named constants and constant expressions Inside a program, we often need to define a constant for multiple usage pi = 3.14159 or to use a constant in declarations and executions, which later need to change. named constants: real, parameter :: pi = 3.14159

7 Initial values for variables Initialization in type declaration statements real :: a = 0.0 real, dimension(3) :: b = (/ 0.0, 1.2, 4.5 /) The data statement data object-list /value-list/ [[,] object-list /value-list/]... real :: a, b, c integer :: i, j, k data a,b,c/1.,2.,3./, i,j,k/1,2,3/ Pointer initialization and the function null real, pointer, dimension(:) :: vector => null() Default initialization of components: any object of a derived type is given a default initial value for a component type entry real :: value = 2.0 integer :: index type(entry), pointer :: next => null() end type entry

8 The public and private attributes If the entities in a module are to be accessible elsewhere, thay have the public attribute. If access is limited to only the procedures of the module, there is no possibility of an accidental corruption of these data by another procedure and design changes can be made within the module without affecting the rest of the program. In cases where entities are not to be accessible outside their own module, they may be given the private attribute. real, public :: x, y, z integer, private :: u, v, w or public :: x, y, z, operator(.add.) private :: u, v, w, assignment(=), operator(*)

9 The pointer, target, and allocatable statements There are statements for specifying the pointer, target, and allocatable attributes of entities. They take the forms: pointer [::] object-name[(array-spec)] [,object-name [(array-spec)]]... target [::] object-name[(array-spec)] [,object-name [(array-spec)]]... and allocatable [::] array-name[(array-spec)] [,array-name [(array-spec)]]... as in real :: a, son, y allocatable :: a(:,:) pointer :: son target :: a, y(10)

10 The intent and optional statements The intent attribute for a dummy argument that is not a dummy procedure or pointer may be specified in a type declaration statement or in an intent statement of the form intent( inout ) [::] dummy-argument-name-list subroutine solve (a, b, c, x, y, z) real :: a, b, c, x, y, z intent(in) :: a, b, c intent(out) :: x, y, z optional [::] dummy-argument-name-list

11 The save attribute To retain the value of a local variable in a subprogram, for example to count the number of times the subprogram is entered. real, save :: a A similar situation arises with the use of variables in modules. In theory, on return from a subprogram that accesses a variable whose scope is a module, the variable becomes undefined unless the main program accesses the module, another subprogram in execution accesses the module, or the variable has the save attribute. In practice, compilers treat module variables as having the save attribute. save [ [::] variable-name-list ] A save statement with no list is equivalent to a list containing all possible names.

12 The use statement use module-name provides access to all the public named data objects, derived types, interface blocks, procedures, generic identifiers, and namelist groups in the module named. Any use statements must precede other specification statements in a scoping unit. The only attribute of an accessed entity that may be specified afresh is public or private (and this only in a module), but the entity may be included in one or more namelist groups.

13 Derived-type definitions type [[,access]:: ] type-name [ private ] component-def-stmt [component-def-stmt]... end type [ type-name ] Each component-def-stmt has the form type [ [,component-attr-list] :: ]component-decl-list where type specifies the type and type parameters. Each component-attr is allocatable, pointer, or dimension(bounds-list). Each component-decl is component-name [ (bounds-list)][ *char-len ] or component-name [ (bounds-list)][ *char-len ] [ comp-int ] type, public :: lock private integer, pointer :: key(:) logical :: state end type lock

14 The type declaration statement Simple examples of the declarations of named entities by integer, real, complex, logical, character, and type(type-name) statements. The general form is type [ [, attribute]... :: ] entity-list

15 Specification expressions subroutine sample(arr, n, string) use definitions ! Contains the real a and the integer datasetsize integer, intent(in) :: n real, dimension(n), intent(out) :: arr ! Explicit-shape array character(len=*), intent(in) :: string ! Assumed length real, dimension(datasetsize+5) :: x ! Automatic array character(len=n+len(string)) :: cc ! Automatic object integer, parameter :: pa2 = selected_real_kind(2*precision(a)) real(kind=pa2) :: z ! Precision of z is at least twice ! the precision of a

16 The namelist statement It is sometimes convenient to gather a set of variables into a single group, in order to facilitate input/output (I/O) operations on the group as a whole. The method by which a group is declared is via the namelist statement namelist namelist-spec where namelist-spec is /namelist-group-name/ variable-name-list The namelist-group-name is the name given to the group for subsequent use in the I/O statements.


Download ppt "Fortran: Specification Statements Session Six ICoCSIS."

Similar presentations


Ads by Google