Skip to content

Commit ec81a1e

Browse files
authored
Merge pull request #263 from Tetralux/passedbyvalue
Clarify nature of calling convention
2 parents 3c06444 + 58f9c50 commit ec81a1e

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

content/docs/overview.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -735,9 +735,13 @@ multiply :: proc(x, y: int) -> int {
735735
fmt.println(multiply(137, 432))
736736
```
737737

738-
Continuing the C family tradition, everything in Odin is passed by value (rather than by reference, e.g. FORTRAN, Java, etc). However, Odin differs from the C/C++ tradition in that all procedure parameters in Odin are immutable values. This allows for numerous optimizations with the Odin [calling conventions](#calling-conventions) (`"odin"` and `"contextless"`) which would not be possible with the original C tradition of always passing a copy of the thing that has been passed.
738+
By default, Odin procedures use the `"odin"` calling convention. This calling convention is the same as C, however it differs in a couple of ways:
739+
- It promotes values to a pointer if that's more efficient on the target system, and
740+
- It includes a pointer to the current context as an implicit additional argument.
739741

740-
Passing a pointer value makes a copy of the pointer, not the data it points to. Slices, dynamic arrays, and maps behave like pointers in this case (Internally they are structures that contain values, which include pointers, and the "structure" is passed by value).
742+
The promotion is enabled by the fact that all parameters are immutable in Odin, and its rules are consistent for a given type and platform and can be relied on since they are part of the calling convention.
743+
744+
Passing a pointer value makes a copy of the pointer, not the data it points to. Slices, dynamic arrays, and maps have no special considerations here; they are normal structures with pointer fields, and are passed as such. Their elements will not be copied.
741745

742746
#### Shadowing Parameters
743747

0 commit comments

Comments
 (0)