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.

width
type_bound_area.f90
Replay: real traced execution (multi-file project)
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 = 4
    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 = 3
    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 = 6
    box = rectangle(width, 5)
    total = box%area()
    print '(I0)', total
end program type_bound_area_demo
  1. width ← 4

    24width = 425box = rectangle(width, 5)
    values this step4width
  2. box ← rectangle(4, 5)

    24width = 425box = rectangle(width, 5)26total = box%area()
    values this steprectangle(4, 5)box4width
  3. total ← 20

    25box = rectangle(width, 5)26total = box%area()27print '(I0)', total
    values this step20total4box.width5box.height
  4. print '(I0)', total

    26    total = box%area()27    print '(I0)', total28end program type_bound_area_demo
    output20
    values this step20total
  1. width ← 3

    24width = 325box = rectangle(width, 5)
    values this step3width
  2. box ← rectangle(3, 5)

    24width = 325box = rectangle(width, 5)26total = box%area()
    values this steprectangle(3, 5)box3width
  3. total ← 15

    25box = rectangle(width, 5)26total = box%area()27print '(I0)', total
    values this step15total3box.width5box.height
  4. print '(I0)', total

    26    total = box%area()27    print '(I0)', total28end program type_bound_area_demo
    output15
    values this step15total
  1. width ← 6

    24width = 625box = rectangle(width, 5)
    values this step6width
  2. box ← rectangle(6, 5)

    24width = 625box = rectangle(width, 5)26total = box%area()
    values this steprectangle(6, 5)box6width
  3. total ← 30

    25box = rectangle(width, 5)26total = box%area()27print '(I0)', total
    values this step30total6box.width5box.height
  4. print '(I0)', total

    26    total = box%area()27    print '(I0)', total28end program type_bound_area_demo
    output30
    values this step30total
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.