1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
|
!> This test file makes sure that optional arguments
!> are passed correctly for ClassProcedure with/without nopass
module modules_module_function_without_nopass
implicit none
private
public :: calculator, SQUARE ! Expose both the type and the SQUARE function
type :: calculator
contains
procedure :: SQUARE ! Associate SQUARE with the calculator type
procedure :: AREA
end type calculator
contains
! Define SQUARE as a module procedure.
function SQUARE(this, x) result(square_result)
class(calculator), intent(in) :: this
integer, optional, intent(in) :: x
integer :: square_result
if (present(x)) then
square_result = x * x
else
square_result = 1
end if
end function SQUARE
function AREA(this, x, y) result(area_result)
class(calculator), intent(in) :: this
integer, optional, intent(in) :: x
integer, optional, intent(in) :: y
integer :: area_result
if (present(x) .and. present(y)) then
area_result = x * y
else
area_result = 1
end if
end function
end module modules_module_function_without_nopass
program module_function_without_nopass
use modules_module_function_without_nopass
implicit none
type(calculator) :: calc
integer :: number, result, area1, area2, area3
number = 4
result = calc%SQUARE(number)
if (result /= 16) error stop
area1 = calc%AREA()
if (area1 /= 1) error stop
area2 = calc%AREA(1, 2)
if (area2 /= 2) error stop
area3 = calc%AREA(1)
if (area3 /= 1) error stop
end program module_function_without_nopass
|