Skip to content

Document pass-by-reference behavior for large arguments #287

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions content/docs/overview.md
Original file line number Diff line number Diff line change
Expand Up @@ -744,6 +744,32 @@ odds := []int{1, 3, 5}
fmt.println(sum(..odds)) // 9, passing a slice as varargs
```

**Note:** Odin makes a compromise for the sake of performance in that large arguments (as determined by the compiler) are internally passed by reference (as opposed to always copying them to the stack, as C does) and this can have surprising consequences if you are not aware of it. Here's an example:
```odin
package main

import "core:fmt"

Data :: struct {
number: [1000]i64,
}

foo :: proc(alpha: Data, beta: ^Data) {
// Both `alpha` and `beta` reference the same data, but because of
// the size of `alpha`, it is passed by reference internally for speed.
//
// Uncomment the line below to override this behavior.
// alpha := alpha // This will make an explicit copy of the data.
beta.number[5] = 123
fmt.println(alpha.number[5]) // prints 123, unless explicitly copied
}

main :: proc() {
data: Data
foo(data, &data)
}
```

### Multiple results
A procedure in Odin can return any number of results. For example:
```odin
Expand Down