`Consider the following sequence of code:
`

program dist implicit none real, external :: distance type Point real :: x,y,z end type Point type(Point) ::a,b a = point(1.,2.,3.) b = point(4.,6.,8.) print*, distance(a,b) end program dist ! real function distance(a,b) type Point real :: x,y,z end type Point type(Point), intent(in) :: a,b distance = sqrt( (a\%x-b\%x)**2 + & (a%y-b%y)**2 + (a%z-b%z)**2 ) end function distance \\

`The solution to this ``problem'' is to make the type definition accessible by
either
`

- USE association (the type is defined in a module, which is then used by both the main program and the distance function), or
- Host association (the distance function is contained in the main program).

`As a general rule, it is therefore preferable to always encapsulate type
definitions in modules, and make them visible by USE association. For example,
the module
`

module Pnt type Point reaL :: x,y,z end type Point end module Pnt

` Functions can return results of an arbitrary defined type.
For example, consider the type of ``double precision
complex numbers'', and the function that adds two double precision
complex numbers returns
a double precision complex result
`

module dpc type DP_Complex double precision :: re, im end type DP_Complex contains FUNCTION DPC_Add(a,b) type(DP_Complex), intent(in) :: a,b type(DP_Complex) :: DPC_Add DPC_Sum%re = a%re + b%re DPC_Sum%im = a%im + b%im end function DPC_Add end module dpc