Skip to content

Commit fb48626

Browse files
committed
update docs
1 parent 0eba7dc commit fb48626

1 file changed

Lines changed: 93 additions & 9 deletions

File tree

‎docs/user-guide/wrapping-functions.md‎

Lines changed: 93 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -58,26 +58,110 @@ class instances.
5858
Numeric explicit-shape, automatic-shape, allocatable, and supported pointer
5959
array results become NumPy arrays. Ordinary and allocatable results are detached
6060
Python-owned copies. Supported pointer results are snapshot copies, not live
61-
views of native targets. The complete `arrays.f90` source, build command, and
62-
asserted result are presented in [Arrays](arrays.md#complete-array-example).
61+
views of native targets.
62+
63+
Create `function_results.f90`:
64+
65+
```fortran
66+
module function_results_api
67+
implicit none
68+
contains
69+
function squares(size) result(values)
70+
integer(4), intent(in) :: size
71+
real(8) :: values(size)
72+
integer(4) :: index
73+
74+
values = [(real(index, 8) * real(index, 8), index = 1, size)]
75+
end function squares
76+
end module function_results_api
77+
```
78+
79+
Build it:
80+
81+
```bash
82+
python3 -m x2py function_results.f90 \
83+
--wrap \
84+
--out-dir build/function-results \
85+
--json
86+
```
87+
88+
Then assert the returned NumPy array:
89+
90+
```python
91+
import sys
92+
93+
import numpy as np
94+
95+
sys.path.insert(0, "build/function-results")
96+
import function_results
97+
98+
api = function_results.function_results_api
99+
result = api.squares(np.int32(4))
100+
np.testing.assert_array_equal(
101+
result,
102+
np.array([1.0, 4.0, 9.0, 16.0], dtype=np.float64),
103+
)
104+
```
63105

64106
Allocated zero-sized results are zero-sized arrays. An unallocated allocatable
65107
result or unassociated pointer result is `None`. Multidimensional results retain
66-
Fortran-oriented element ordering. See [Arrays](arrays.md) and
67-
[Allocatable Arrays](allocatable-arrays.md) before relying on result lifetime.
108+
Fortran-oriented element ordering. See [Arrays](arrays.md) for broader array
109+
validation and [Allocatable Arrays](allocatable-arrays.md) before relying on
110+
result lifetime.
68111

69112
## Functions With Output Arguments
70113

71114
If a function also has output dummies, Python returns a tuple. The direct
72115
function result is first, followed by projected output dummies in native
73-
argument order. The `outputs.f90` example in
74-
[Wrapping Subroutines](wrapping-subroutines.md#complete-output-example) shows
75-
the output-dummy part of this projection with complete source and results.
116+
argument order.
117+
118+
Create `function_outputs.f90`:
119+
120+
```fortran
121+
module function_outputs_api
122+
implicit none
123+
contains
124+
function sum_with_count(values, count) result(total)
125+
real(8), intent(in) :: values(:)
126+
integer(4), intent(out) :: count
127+
real(8) :: total
128+
129+
total = sum(values)
130+
count = size(values)
131+
end function sum_with_count
132+
end module function_outputs_api
133+
```
134+
135+
Build it:
136+
137+
```bash
138+
python3 -m x2py function_outputs.f90 \
139+
--wrap \
140+
--out-dir build/function-outputs \
141+
--json
142+
```
143+
144+
Then assert the tuple order:
145+
146+
```python
147+
import sys
148+
149+
import numpy as np
150+
151+
sys.path.insert(0, "build/function-outputs")
152+
import function_outputs
153+
154+
api = function_outputs.function_outputs_api
155+
source = np.array([4.0, -2.0, 7.0], dtype=np.float64)
156+
total, count = api.sum_with_count(source)
157+
assert total == np.float64(9.0)
158+
assert count == np.int32(3)
159+
```
76160

77161
Caller-provided output arrays remain arguments because the caller must allocate
78162
their storage. Their return projection, when present, refers to that same
79-
object. [Wrapping Subroutines](wrapping-subroutines.md) defines the common
80-
`intent(out)` and `intent(inout)` rules.
163+
object. The same `intent(out)` and `intent(inout)` projection rules apply to
164+
subroutines that have no direct function result.
81165

82166
## Call Limits
83167

0 commit comments

Comments
 (0)