Derived Types

Structured Data

User Defined Types

•     all data considered so far is of intrinsic type

•     integer, real, complex, logical, character

•     arrays allow grouping of data of same type

•     all elements are of same type in an array

•     many times we need to define groups of data which may be of different types

•     give a name to such a group and define it as a new type of variable

Points in the Plane

•     points in the plane

•     defined by x and y coordinates

•     separate variables may be used for them

•     this ignores the fact that they are properties of the same point

•     better to group them into one variable and define a new type called point

•     improves readability of program and allows operations to be performed on point as a whole rather than each coordinate separately

Student Records

•     group all information about a student in a single variable

•     may contain data of different intrinsic type

•     character- name, address, roll-number

•     integer – hostel/room number

•     real – C.P.I, mess dues

•     define a new type containing data of all these types

Type Declaration

type :: point

real :: x, y

end type point

•     declares point to be a new type containing two components each of which is real

type(point) :: p

•     declares p to be a variable of type point

•     p is a variable which stores two real values

Type Declaration

type :: student_info

character(len=20) :: name

character(len=8) :: roll_number

integer :: hostel_no, room_no

real :: cpi, dues

end type student_info

•     a variable of type student_info contains six components, two character strings, two integers and two real numbers

Assignment

•     a variables may be assigned to another variable of the same derived type

•     corresponding components are assigned

•     values may be assigned to individual components separately

•     individual components accessed using % operator and name as given in type declaration

•     cannot be initialized in declaration ( parameter attribute cannot be given)

 

Example

type(point) :: p, q

p = q

p%x = 1.0

p%y = 2.0

! p%x denotes the component of p with name x of type real

! p%x is a real variable and can be used as one

! p = q is same as p%x = q%x and p%y = q%y

p = point(1.0,2.0)

! a type constructor used to create value of type point

! same name as type used

! each component specified by expression of appropriate type

Example

type(student_info) :: student

student%name = “Ajit Diwan”

student%roll_number = “790430”

student%hostel_no = 5

student%room_no = 70

student%cpi = 9.04

student%dues = 0.0

•      each component of variable student can be treated as a separate variable of the given type

Super Types

•     a type may contain a component of another user defined type (or same type but with pointer attribute, see later)

•     component type must be defined before use

type :: circle

type(point) :: center

real :: radius

end type circle

type(circle) :: c

! c%center%x is the x coordinate of the center

! improves readability of the program

! better than using 3 independent real variables

Arrays

•     possible to define arrays of user defined type

•     dimension attribute given in variable declaration

integer, parameter :: max_students = 500

type(student_info), dimension(max_students) :: &

student_data

•     student_data is an array each of whose elements is of type student info

•     student_data(i)%name is name of ith student

•     student_data(:)%name is a character array

Array Components

•     a component of a type can itself be an array

•     such arrays cannot be allocatable

integer, parameter :: max_size

type :: polygon

integer :: no_of_points

type(point), dimension(max_size) :: vertex

end type polygon

•     an element of an array may itself contain an array component

•     array sectioning can be used at only one level

Array Components

integer, parameter :: max_students=500,max_courses=100

type :: course

character(len=30) :: course_name

character(len=5) :: course_number

integer :: no_of_students

character(len=8), dimension(max_students) :: roll_numbers

end type course

type(course), dimension(max_courses) :: registration

!registration(i)%roll_numbers(: registration(i)%no_of_students)

! array containing roll_numbers of students in ith course

! registration(:)%roll_numbers(:) is invalid

Input Output

•     derived type variables can be read or written directly in read, print or write statement

•     should not contain a component with pointer type

•     components read/written in the order defined

•     if a component is a derived type its components are considered till intrinsic types are obtained

•     formats can be used according to this sequence

•     individual components may also be read/written

Functions and Subroutines

•     main advantage of user defined types

•     variables of such types can be passed as arguments

•     functions can return variables of such types

•     the type declaration must be “visible” in subprogram

•     defined in main program containing subprogram

•     usually, type declarations are in a separate module

•     same declaration repeated in subprogram would be treated as a different type

Example

function midpoint(p,q) result(r)

! computes the midpoint of line segment joining points p,q

! must be contained in program where type point is defined

! usually defined in a module used in main program

type(point), intent(in) :: p, q

type(point) :: r

r%x = (p%x+q%x)/2.0

r%y = (p%y+q%y)/2.0

end function midpoint

Example

function intersect(c_1, c_2) result(ins)

! determines whether two circles intersect

type(circle), intent(in) :: c_1, c_2

logical :: ins

ins = (distance(c_1%center, c_2%center) <= c_1%radius &

          +  c_2%radius)

end function intersect

function distance(p,q) result(d)

type(point), intent(in) :: p, q

real :: d

d = sqrt((p%x - q%x)**2 + (p%y - q%y)**2)

end function distance

Modules and Types

•     derived types are usually defined in a module

•     module includes all procedures for performing operations on variables of derived types

•     geometric module

–   includes definitions of points, lines, triangles, circles together with various operations

•     internal definitions are private to module

•     user of module can only perform operations on derived types and not access their components

•     allows safe usage and improves readability