Derived Type Procedures
Type-Bound Procedure
Area Method
A derived type can own procedures that operate on its values. Type-bound calls keep the data and behavior together.
Program
Play the program to choose a rectangle width and call the bound area method.
type_bound_area.f90
module shape_model
implicit none
type :: rectangle
integer :: width
integer :: height
contains
procedure :: area
end type rectangle
contains
function area(self) result(total)
class(rectangle), intent(in) :: self
integer :: total
total = self%width * self%height
end function area
end module shape_model
program type_bound_area_demo
use shape_model
implicit none
type(rectangle) :: box
integer :: width
integer :: total
width =
box = rectangle(width, 5)
total = box%area()
print '(I0)', total
end program type_bound_area_demo
module shape_model
implicit none
type :: rectangle
integer :: width
integer :: height
contains
procedure :: area
end type rectangle
contains
function area(self) result(total)
class(rectangle), intent(in) :: self
integer :: total
total = self%width * self%height
end function area
end module shape_model
program type_bound_area_demo
use shape_model
implicit none
type(rectangle) :: box
integer :: width
integer :: total
width =
box = rectangle(width, 5)
total = box%area()
print '(I0)', total
end program type_bound_area_demo
module shape_model
implicit none
type :: rectangle
integer :: width
integer :: height
contains
procedure :: area
end type rectangle
contains
function area(self) result(total)
class(rectangle), intent(in) :: self
integer :: total
total = self%width * self%height
end function area
end module shape_model
program type_bound_area_demo
use shape_model
implicit none
type(rectangle) :: box
integer :: width
integer :: total
width =
box = rectangle(width, 5)
total = box%area()
print '(I0)', total
end program type_bound_area_demo
type-bound procedure
`procedure :: area` attaches a procedure to the derived type.
class argument
`class(rectangle)` receives the object for the method call.
method call
`box%area()` calls the procedure through the value.