Functions
Examples in
V
fn main() {
println(add(77, 33))
println(sub(100, 50))
}
fn add(x int, y int) int {
return x + y
}
fn sub(x, y int) int {
return x - y
}
A higher order function is a function that takes a function as an argument, or returns a function. Higher order function is in contrast to first order functions, which don’t take a function as an argument or return a function as output.
fn sqr(n int) int {
return n * n
}
fn run(value int, op fn(int) int) int {
return op(value)
}
fn main() {
println(run(5, sqr)) // "25"
}