real function tester(a)
real, intent (in), optional :: a
if (present(a)) then
tester = a
else
tester = 0.0
end if
end function
program main
interface
function tester(a)
real function tester(a)
real, intent (in), optional :: a
end function
end interface
print *, "[no args] tester() :", tester() ! yields: 0.0
print *, "[ args] tester(1.0):", tester(1.0) ! yields: 1.0
end program
real function tester(a)
real, intent (in), optional :: a
if (present(a)) then
tester = a
else
tester = 0.0
end if
end function
program main
interface
real function tester(a)
real, intent (in), optional :: a
end function
end interface
print *, "[no args] tester() :", tester() ! yields: 0.0
print *, "[ args] tester(1.0):", tester(1.0) ! yields: 1.0
end program
real function tester(a)
real, intent (in), optional :: a
if (present(a)) then
tester = a
else
tester = 0.0
end if
end function
program main
interface
function tester(a)
real :: tester ! this one!!
real, intent (in), optional :: a
end function
end interface
print *, "[no args] tester() :", tester() ! yields: 0.0
print *, "[ args] tester(1.0):", tester(1.0) ! yields: 1.0
end program
I checked the integer version of the above program:
integer function tester(a)
real, intent (in), optional :: a
if (present(a)) then
tester = a
else
tester = 0
end if
end function
program main
interface
function tester(a)
integer :: tester ! this one
real, intent (in), optional :: a
end function
end interface
print *, "[no args] tester() :", tester() ! yields: 0
print *, "[ args] tester(1.0):", tester(1.0) ! yields: 1
end program
Inadvertently or intentionally, someone at Wikibooks can confuse the heck out of a new reader and post an example like so:
module tester_m
interface
module function tester(a) result(r) bind(C, name="TESTER")
real, intent (in), optional :: a
real :: r
end function
end interface
end module tester_m
submodule(tester_m) tester_sm
contains
module function tester(a) result(r) bind(C, name="TESTER")
real, intent (in), optional :: a
if (present(a)) then
r = a
else
r = 0.0
end if
end function
end submodule tester_sm
program main
interface
function tester(a) result(r) bind(C, name="TESTER")
real, intent (in), optional :: a
real :: r
end function
end interface
print *, "[no args] tester() :", tester() ! yields: 0.0
print *, "[ args] tester(1.0):", tester(1.0) ! yields: 1.0
end program
And get an rating of “almost evil” with the duplication of interfaces and names and the “cheating” with the global name!