Functions

Wikipedia: Subroutine
Functions are central in Tengo. They are values that can be assigned to variables, passed as arguments, and returned from other functions.

fmt := import("fmt")
res := plus(1, 2)
fmt.println("1+2 = ", res)
plusPlus := func(a, b, c) {
    return a + b + c
}
res = plusPlus(1, 2, 3)
fmt.println("1+2+3 = ", res)
apply := func(f, a, b) {
    return f(a, b)
}
fmt.println("apply: ", apply(plus, 5, 3))

try it

1+2 = 3
1+2+3 = 6
apply: 8
loading…