optval

可选参数的默认值

optval - 可选参数的回退值

状态

实验性

描述

如果存在 x,则返回 x,否则返回 default

此函数旨在在一个或多个 optional 参数的过程内调用,以便在 optional 参数不存在时方便地回退到默认值。

语法

result = optval (x, default)

参数

x:应为 integerrealcomplexlogical 类型,或 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