mirror of
https://github.com/ziglang/zig.git
synced 2024-11-30 09:02:32 +00:00
21 lines
537 B
Zig
21 lines
537 B
Zig
const Point = struct {
|
|
x: i32,
|
|
y: i32,
|
|
};
|
|
|
|
fn foo(point: Point) i32 {
|
|
// Here, `point` could be a reference, or a copy. The function body
|
|
// can ignore the difference and treat it as a value. Be very careful
|
|
// taking the address of the parameter - it should be treated as if
|
|
// the address will become invalid when the function returns.
|
|
return point.x + point.y;
|
|
}
|
|
|
|
const expect = @import("std").testing.expect;
|
|
|
|
test "pass struct to function" {
|
|
try expect(foo(Point{ .x = 1, .y = 2 }) == 3);
|
|
}
|
|
|
|
// test
|