Skip to content

Commit f6b730f

Browse files
committed
write spec for optval
1 parent eeb33d7 commit f6b730f

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed

src/stdlib_experimental_optval.md

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# Default values for optional arguments
2+
3+
## Implemented
4+
5+
* `optval`
6+
7+
## `optval` - fallback value for optional arguments
8+
9+
### Description
10+
11+
Returns `x` if it is PRESENT, otherwise `default`.
12+
13+
This function is intended to be called in a proceudre with one or more OPTIONAL arguments, in order to conveniently fall back to a default value if an OPTIONAL argument is not PRESENT.
14+
15+
### Syntax
16+
17+
`result = optval(x, default)`
18+
19+
### Arguments
20+
21+
`x`: Shall be of type `integer`, `real`, `complex`, or `logical`, or a scalar of type `character`.
22+
23+
`default`: Shall have the same type, kind, and rank as `x`.
24+
25+
### Return value
26+
27+
If `x` is PRESENT, the result is `x`, otherwise the result is `default`.
28+
29+
### Example
30+
31+
```fortran
32+
program demo_optval
33+
use stdlib_experimental_optval, only: optval
34+
implicit none
35+
print *, root(64.0)
36+
! 8.0
37+
print *, root(64.0, 3)
38+
! 4.0
39+
contains
40+
real function root(x, n)
41+
real, intent(in) :: x
42+
integer, intent(in), optional :: n
43+
root = x**(1.0/optval(n, 2))
44+
end function root
45+
end program demo_optval
46+
```

0 commit comments

Comments
 (0)