learning-zig/lang/functions.zig

22 lines
397 B
Zig
Raw Permalink Normal View History

2024-12-05 05:22:34 +00:00
const expect = @import("std").testing.expect;
fn addFive(x: u32) u32 {
return x + 5;
}
fn fibonacci(n: u16) u16 {
if (n == 0 or n == 1) return n;
return fibonacci(n - 1) + fibonacci(n - 2);
}
test "function" {
const y = addFive(0);
try expect(@TypeOf(y) == u32);
try expect(y == 5);
}
test "function recursion" {
const x = fibonacci(10);
try expect(x == 55);
}