HP Fortran for OpenVMS
Language Reference Manual


Previous Contents Index

5.12 NAMELIST Statement

The NAMELIST statement associates a name with a list of variables. This group name can be referenced in some input/output operations.

A NAMELIST statement takes the following form:

  • NAMELIST /group/var-list [[,] /group/var-list]...

group

Is the name of the group.

var-list

Is a list of variables (separated by commas) that are to be associated with the preceding group name. The variables can be of any data type.

Rules and Behavior

The namelist group name is used by namelist I/O statements instead of an I/O list. The unique group name identifies a list whose entities can be modified or transferred.

A variable can appear in more than one namelist group.

Each variable in var-list must be accessed by use or host association, or it must have its type, type parameters, and shape explicitly or implicitly specified in the same scoping unit. If the variable is implicitly typed, it can appear in a subsequent type declaration only if that declaration confirms the implicit typing.

The following variables cannot be specified in a namelist group:

Only the variables specified in the namelist can be read or written in namelist I/O. It is not necessary for the input records in a namelist input statement to define every variable in the associated namelist.

The order of variables in the namelist controls the order in which the values appear on namelist output. Input of namelist values can be in any order.

If the group name has the PUBLIC attribute, no item in the variable list can have the PRIVATE attribute.

The group name can be specified in more than one NAMELIST statement in a scoping unit. The variable list following each successive appearance of the group name is treated as a continuation of the list for that group name.

Examples

In the following example, D and E are added to the variables A, B, and C for group name LIST:


NAMELIST /LIST/ A, B, C 
 
NAMELIST /LIST/ D, E 

In the following example, two group names are defined:


CHARACTER*30 NAME(25) 
NAMELIST /INPUT/ NAME, GRADE, DATE /OUTPUT/ TOTAL, NAME 

Group name INPUT contains variables NAME, GRADE, and DATE. Group name OUTPUT contains variables TOTAL and NAME.

For More Information:

On namelist input, see Section 10.3.1.3; output, see Section 10.5.1.3.

5.13 OPTIONAL Attribute and Statement

The OPTIONAL attribute permits dummy arguments to be omitted in a procedure reference.

The OPTIONAL attribute can be specified in a type declaration statement or an OPTIONAL statement, and takes one of the following forms:

Type Declaration Statement:

  • type, [att-ls,] OPTIONAL [,att-ls] :: d-arg [,d-arg]...

Statement:

  • OPTIONAL [::] d-arg [,d-arg]...

type

Is a data type specifier.

att-ls

Is an optional list of attribute specifiers.

d-arg

Is the name of a dummy argument.

Rules and Behavior

The OPTIONAL attribute can only appear in the scoping unit of a subprogram or an interface body, and can only be specified for dummy arguments.

A dummy argument is "present" if it associated with an actual argument. A dummy argument that is not optional must be present. You can use the PRESENT intrinsic function to determine whether an optional dummy argument is associated with an actual argument.

To call a procedure that has an optional argument, you must use an explicit interface.

Examples

The following example shows a type declaration statement specifying the OPTIONAL attribute:


SUBROUTINE TEST(A) 
  REAL, OPTIONAL, DIMENSION(-10:2) :: A 
END SUBROUTINE 

The following is an example of the OPTIONAL statement:


SUBROUTINE TEST(A, B, L, X) 
  OPTIONAL :: B 
  INTEGER A, B, L, X 
 
  IF (PRESENT(B)) THEN        ! Printing of B is conditional 
    PRINT *, A, B, L, X       !   on its presence 
  ELSE 
    PRINT *, A, L, X 
  ENDIF 
END SUBROUTINE 
 
INTERFACE 
  SUBROUTINE TEST(ONE, TWO, THREE, FOUR) 
    INTEGER ONE, TWO, THREE, FOUR 
    OPTIONAL :: TWO 
  END SUBROUTINE 
END INTERFACE 
 
INTEGER I, J, K, L 
 
I = 1 
J = 2 
K = 3 
L = 4 
 
CALL TEST(I, J, K, L)            ! Prints:  1  2  3  4 
CALL TEST(I, THREE=K, FOUR=L)    ! Prints:  1  3  4 
END 

Note that in the second call to subroutine TEST, the second positional (optional) argument is omitted. In this case, all following arguments must be keyword arguments.

For More Information:

5.14 PARAMETER Attribute and Statement

The PARAMETER attribute defines a named constant.

The PARAMETER attribute can be specified in a type declaration statement or a PARAMETER statement, and takes one of the following forms:

Type Declaration Statement:

  • type, [att-ls,] PARAMETER [,att-ls] :: c = expr [, c = expr]...

Statement:

  • PARAMETER [(] c = expr [, c = expr]...[)]

type

Is a data type specifier.

att-ls

Is an optional list of attribute specifiers.

c

Is the name of the constant.

expr

Is an initialization expression. It can be of any data type.

Rules and Behavior

The type, type parameters, and shape of the named constant are determined in one of the following ways:

For example, consider the following statement:


PARAMETER (MU=1.23) 

According to implicit typing, MU is of integer type, so MU=1. For MU to equal 1.23, it should previously be declared REAL in a type declaration or be declared in an IMPLICIT statement.

A named constant must not appear in a format specification or as the character count for Hollerith constants. For compilation purposes, writing the name is the same as writing the value.

If the named constant is used as the length specifier in a CHARACTER declaration, it must be enclosed in parentheses.

The name of a constant cannot appear as part of another constant, although it can appear as either the real or imaginary part of a complex constant.

You can only use the named constant within the scoping unit containing the defining PARAMETER statement.

Any named constant that appears in the initialization expression must have been defined previously in the same type declaration statement (or in a previous type declaration statement or PARAMETER statement), or made accessible by use or host association.

Examples

The following example shows a type declaration statement specifying the PARAMETER attribute:


REAL, PARAMETER :: C = 2.9979251, Y = (4.1 / 3.0) 

The following is an example of the PARAMETER statement:


REAL(4) PI, PIOV2 
REAL(8) DPI, DPIOV2 
LOGICAL FLAG 
CHARACTER*(*) LONGNAME 
 
PARAMETER (PI=3.1415927, DPI=3.141592653589793238D0) 
PARAMETER (PIOV2=PI/2, DPIOV2=DPI/2) 
PARAMETER (FLAG=.TRUE., LONGNAME='A STRING OF 25 CHARACTERS') 

For More Information:

5.15 POINTER Attribute and Statement

The POINTER attribute specifies that an object is a pointer (a dynamic variable). A pointer does not contain data, but points to a scalar or array variable where data is stored. A pointer has no initial storage set aside for it; memory storage is created for the pointer as a program runs.

The POINTER attribute can be specified in a type declaration statement or a POINTER statement, and takes one of the following forms:

Type Declaration Statement:

  • type, [att-ls,] POINTER [,att-ls] :: ptr [(d-spec)] [,ptr [(d-spec)]]...

Statement:

  • POINTER [::] ptr [(d-spec)] [,ptr [(d-spec)]]...

type

Is a data type specifier.

att-ls

Is an optional list of attribute specifiers.

ptr

Is the name of the pointer. The pointer cannot be declared with the INTENT or PARAMETER attributes.

d-spec

Is a deferred-shape specification (: [,:]...). Each colon represents a dimension of the array.

Rules and Behavior

No storage space is created for a pointer until it is allocated with an ALLOCATE statement or until it is assigned to a allocated target. A pointer must not be referenced or defined until memory is associated with it.

Each pointer has an association status, which tells whether the pointer is currently associated with a target object. When a pointer is initially declared, its status is undefined. You can use the ASSOCIATED intrinsic function to find the association status of a pointer.

If the pointer is an array, and it is given the DIMENSION attribute elsewhere in the program, it must be declared as a deferred-shape array.

A pointer cannot be specified in a DATA, EQUIVALENCE, or NAMELIST statement.

Examples

The following example shows type declaration statements specifying the POINTER attribute:


TYPE(SYSTEM), POINTER :: CURRENT, LAST 
REAL, DIMENSION(:,:), POINTER :: I, J, REVERSE 

The following is an example of the POINTER statement:


TYPE(SYSTEM) :: TODAYS 
POINTER :: TODAYS, A(:,:) 

For More Information:

5.16 PRIVATE and PUBLIC Attributes and Statements

The PRIVATE and PUBLIC attributes specify the accessibility of entities in a module. (These attributes are also called accessibility attributes.)

The PRIVATE and PUBLIC attributes can be specified in a type declaration statement or a PRIVATE or PUBLIC statement, and take one of the following forms:

Type Declaration Statement:

  • type, [att-ls,] PRIVATE [,att-ls] :: entity [,entity]...
  • type, [att-ls,] PUBLIC [,att-ls] :: entity [,entity]...

Statement:

  • PRIVATE [[::] entity [,entity]...]
  • PUBLIC [[::] entity [,entity]...]

type

Is a data type specifier.

att-ls

Is an optional list of attribute specifiers.

entity

Is one of the following:

In statement form, an entity can also be a generic identifier (a generic name, defined operator, or defined assignment).

Rules and Behavior

The PRIVATE and PUBLIC attributes can only appear in the scoping unit of a module.

Only one PRIVATE or PUBLIC statement without an entity list is permitted in the scoping unit of a module; it sets the default accessibility of all entities in the module.

If no PUBLIC or PRIVATE statements are specified in a module, the default is PUBLIC accessibility. Entities with PUBLIC accessibility can be accessed from outside the module by means of a USE statement.

If a derived type is declared PRIVATE in a module, its components are also PRIVATE. The derived type and its components are accessible to any subprograms within the defining module through host association, but the type is not accessible from outside the module and in most cases the components are not accessible, either.

However, if a public entity is declared to be of a type declared PRIVATE, the components of that type are accessible as components of the entity. For example:


module m2 
type hidden 
   integer f1,f2 
end type hidden 
end 
 
module m3 
   use m2 
   private 
   type(hidden),public :: x 
  end 
 
subroutine import 
   use m3 
   x%f1 = 1 
   end subroutine 

In this example, the F1 component of X is accessible, even though the type HIDDEN is PRIVATE.

If the derived type is declared PUBLIC in a module, but its components are declared PRIVATE, any scoping unit accessing the module though use association (or host association) can access the derived-type definition, but not its components.

If a module procedure has a dummy argument or a function result of a type that has PRIVATE accessibility, the module procedure must have PRIVATE accessibility. If the module has a generic identifier, it must also be declared PRIVATE.

If a procedure has a generic identifier, the accessibility of the procedure's specific name is independent of the accessibility of its generic identifier. One can be declared PRIVATE and the other PUBLIC.

Examples

The following examples show type declaration statements specifying the PUBLIC and PRIVATE attributes:


REAL,  PRIVATE  :: A, B, C 
INTEGER, PUBLIC :: LOCAL_SUMS 

The following is an example of the PUBLIC and PRIVATE statements:


MODULE SOME_DATA 
  REAL ALL_B 
  PUBLIC ALL_B 
  TYPE RESTRICTED_DATA 
    REAL LOCAL_C 
    DIMENSION LOCAL_C(50) 
  END TYPE RESTRICTED_DATA 
  PRIVATE RESTRICTED_DATA 
END MODULE 

The following derived-type declaration statement indicates that the type is restricted to the module:


TYPE, PRIVATE  :: DATA 
  ... 
END TYPE DATA 

The following example shows a PUBLIC type with PRIVATE components:


MODULE MATTER 
  TYPE ELEMENTS 
    PRIVATE 
    INTEGER C, D 
  END TYPE 
... 
END MODULE MATTER 

In this case, components C and D are private to type ELEMENTS, but type ELEMENTS is not private to MODULE MATTER. Any program unit that uses the module MATTER, can declare variables of type ELEMENTS, and pass as arguments values of type ELEMENTS.

For More Information:

5.17 SAVE Attribute and Statement

The SAVE attribute causes the values and definition of objects to be retained after execution of a RETURN or END statement in a subprogram.

The SAVE attribute can be specified in a type declaration statement or a SAVE statement, and takes one of the following forms:

Type Declaration Statement:

  • type, [att-ls,] SAVE [,att-ls] :: [object [,object]...]

Statement:

  • SAVE [object [,object]...]

type

Is a data type specifier.

att-ls

Is an optional list of attribute specifiers.

object

Is the name of an object, or the name of a common block enclosed in slashes (/common-block-name/).

Rules and Behavior

In HP Fortran, certain variables are given the SAVE attribute, or not, by default:

To enhance portability and avoid possible compiler warning messages, HP recommends that you use the SAVE statement to name variables whose values you want to preserve between subprogram invocations.

When a SAVE statement does not explicitly contain a list, all allowable items in the scoping unit are saved.

A SAVE statement cannot specify the following (their values cannot be saved):

Even though a common block can be included in a SAVE statement, individual variables within the common block can become undefined (or redefined) in another scoping unit.

If a common block is saved in any scoping unit of a program (other than the main program), it must be saved in every scoping unit in which the common block appears.

A SAVE statement has no effect in a main program.

Examples

The following example shows a type declaration statement specifying the SAVE attribute:


SUBROUTINE TEST() 
  REAL, SAVE :: X, Y 

The following is an example of the SAVE statement:


SAVE A, /BLOCK_B/, C, /BLOCK_D/, E 

For More Information:

5.18 TARGET Attribute and Statement

The TARGET attribute specifies that an object can become the target of a pointer (it can be pointed to).

The TARGET attribute can be specified in a type declaration statement or a TARGET statement, and takes one of the following forms:

Type Declaration Statement:

  • type, [att-ls,] TARGET [,att-ls] :: object [(a-spec)] [,object [(a-spec)]]...

Statement:

  • TARGET [::] object [(a-spec)] [,object [(a-spec)]]...

type

Is a data type specifier.

att-ls

Is an optional list of attribute specifiers.

object

Is the name of the object. The object must not be declared with the PARAMETER attribute.

a-spec

Is an array specification.

Rules and Behavior

A pointer is associated with a target by pointer assignment or by an ALLOCATE statement.

If an object does not have the TARGET attribute or has not been allocated (using an ALLOCATE statement), no part of it can be accessed by a pointer.

Examples

The following example shows type declaration statements specifying the TARGET attribute:


TYPE(SYSTEM), TARGET :: FIRST 
REAL, DIMENSION(20, 20), TARGET :: C, D 

The following is an example of a TARGET statement:


TARGET :: C(50, 50), D 

For More Information:


Previous Next Contents Index