02.07.2013 Views

HP Fortran Programmer's Reference

HP Fortran Programmer's Reference

HP Fortran Programmer's Reference

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Derived types<br />

Data types and data objects<br />

Derived types<br />

Derived types are user-defined types that are constructed from entities of intrinsic data types<br />

(see “Intrinsic data types” on page 25) or entities of previously defined derived types. For<br />

example, the following is a definition of a derived type for manipulating coordinates<br />

consisting of two real numbers:<br />

TYPE coord<br />

REAL :: x,y<br />

END TYPE coord<br />

x and y are the components of the derived type coord.<br />

The next statement declares two variables (a and b) of the derived type coord:<br />

TYPE(coord) :: a, b<br />

The next statement copies the values of a to b, as in any assignment statement:<br />

a = b<br />

The components of a and b are referenced as a%x, a%y, b%x, andb%y. By using the defined<br />

operation facility of <strong>Fortran</strong> 90, it is possible to extend the standard operators to work with<br />

derived types. For example, if the + and = operators were re-defined to operate on derived type<br />

operands, the following statement<br />

a = a + b<br />

would be equivalent to<br />

a%x = a%x + b%x; a%y = a%y + b%y<br />

The following sections describe:<br />

The syntax of defining a derived type<br />

Sequence types<br />

Structure constructors<br />

Referencing a structure component<br />

Alignment of derived type objects<br />

The last section provides an example program that illustrates different features of derived<br />

types.<br />

Defining a derived type<br />

The format for defining a derived type is:<br />

Chapter 3 41

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!