test:zig
This commit is contained in:
30
guide/language/functions.zig
Normal file
30
guide/language/functions.zig
Normal file
@@ -0,0 +1,30 @@
|
||||
const expect = @import("std").testing.expect;
|
||||
|
||||
fn addFive(x: u32) u32 {
|
||||
return x + 5;
|
||||
}
|
||||
|
||||
test "function" {
|
||||
const y = addFive(0);
|
||||
try expect(@TypeOf(y) == u32);
|
||||
try expect(y == 5);
|
||||
}
|
||||
|
||||
fn fibonacci(n: u16) u16 {
|
||||
if (n == 0 or n == 1) return n;
|
||||
return fibonacci(n - 1) + fibonacci(n - 2);
|
||||
}
|
||||
|
||||
test "function recursion" {
|
||||
const x = fibonacci(10);
|
||||
try expect(x == 55);
|
||||
}
|
||||
|
||||
// When recursion happens, the compiler is no longer able to work out the maximum stack size,
|
||||
// which may result in unsafe behaviour - a stack overflow.
|
||||
// Details on how to achieve safe recursion will be covered in the future.
|
||||
//
|
||||
// Values can be ignored using _ instead of a variable or const declaration.
|
||||
// This does not work at the global scope (i.e. it only works inside functions and blocks)
|
||||
// and is useful for ignoring the values returned from functions if you do not need them.
|
||||
// _ = 10; // ignore the value 10
|
||||
Reference in New Issue
Block a user