optval
- 可选参数的回退值实验性
如果存在 x
,则返回 x
,否则返回 default
。
此函数旨在在一个或多个 optional
参数的过程内调用,以便在 optional
参数不存在时方便地回退到默认值。
result =
optval (x, default)
x
:应为 integer
、real
、complex
或 logical
类型,或 character
类型的标量。
default
:应与 x
具有相同的类型、种类和秩。
如果 x
存在,则结果为 x
,否则结果为 default
。
program example_optval
use stdlib_optval, only: optval
implicit none
print *, root(64.0)
! 8.0
print *, root(64.0, 3)
! 4.0
contains
real function root(x, n)
real, intent(in) :: x
integer, intent(in), optional :: n
root = x**(1.0/optval(n, 2))
end function root
end program example_optval