Modules

Wikipedia: Modular programming A .tengo file becomes a module by ending with an export statement. Importing it runs the file once, caches the result, and returns the exported value — usually a map of functions — to every importer.

math_utils.tengo
// `math_utils.tengo` defines three helpers and exports them as a map.
// The module has no side effects on import — it only defines and exports.
square := func(x) { return x * x }
cube   := func(x) { return x * x * x }
abs    := func(x) { return x < 0 ? -x : x }

export {
    square: square,
    cube:   cube,
    abs:    abs,
}
fmt  := import("fmt")
math := import("math_utils")

fmt.printf("square(4) = %d\n", math.square(4))
fmt.printf("cube(3)   = %d\n", math.cube(3))
fmt.printf("abs(-7)   = %d\n", math.abs(-7))
stdlib := import("math")
fmt.printf("pi      = %.4f\n", stdlib.pi)
fmt.printf("sqrt(2) = %.4f\n", stdlib.sqrt(2.0))

try it

square(4) = 16
cube(3)   = 27
abs(-7)   = 7
pi      = 3.1416
sqrt(2) = 1.4142
loading…
main.tengo
math_utils.tengo