proc main(): void {
echo grade(7)
echo factorial(5)
}
func grade(frames: Int): Str {
if frames > 10 {
return "strong"
} else if frames > 3 {
return "growing"
}
return "weak" // without this, a path would end with nothing to answer
}
func factorial(n: Int): Int {
if n <= 1 {
return 1
}
return n * factorial(n - 1)
}func sign(n: Int): Int {
if n > 0 {
return 1
} else if n < 0 {
return -1
}
// assert n == 0 // would not do: a holding assert carries on
return 0
}
func settings(path: Str): Str {
if path != "" {
return path
}
echo "no settings file given"
hive.term.exit(2) // ends the program, so it ends this path too
}
proc main(): void {
echo sign(-4)
echo settings("hive.toml")
}A callable is declared with func or proc, a name, its parameters as name: Type, and the type it answers with — void when it answers with nothing, since the return type is always written. They may be declared anywhere in a file, in any order, and may call themselves. A parameter is immutable inside the body, and a call waits for the callee to finish.
The two keywords differ in one thing only, which is what a callable may write to that its caller can see; the slide on func and proc covers it. Until then, write func, and proc for main.
A callable that answers with a value must end every path through its body. A path ends with return, with panic, with a call to hive.term.exit — which ends the whole program — or with an if/else whose every branch ends. An assert does not end one: an assertion that holds carries on past it, so a function ending on assert still has a path that returns nothing, and is refused.