Functions
A function is a reusable block of code that performs a specific task. Use the func keyword to declare one.
Creating a Function
Define a function with func, then call it by name.
fun greet() {
println("Hello world")
}
greet()Parameters
Pass data into a function via parameters. Function parameters are immutable by default. Parameters can have default values — if a function is called without an argument for that parameter, the default is used.
fun greet(name: String) {
println("Hello " + name)
}
greet("John")
// Default parameter values
fun add(a: Int = 1, b: Int = 2): Int {
return a + b
}
println(add()) // 3 (both defaults)
println(add(5)) // 7 (default b=2)
println(add(5, 10)) // 15Return Values
Declare the return type after :. Use the return keyword to return a value. For single-expression functions, use the shorthand = expression — the result is returned automatically.
// Full form
fun multiply(a: Int, b: Int): Int {
return a * b
}
println(multiply(3, 5)) // 15
// Single-expression shorthand
fun plus(a: Int, b: Int): Int = a + b
println(plus(2, 3)) // 5Function Types & Callbacks
Autolang supports Higher-Order Functions. To declare a parameter that accepts a function, use the type syntax (ParamType1, ...) -> ReturnType. If the function returns nothing, use Void. You can pass either a named function (function pointer) or a closure as a callback — both are fully supported.
// 1. Define a higher-order function that accepts a function
fun calculate(a: Int, b: Int, operation: (Int, Int) -> Int): Int {
return operation(a, b)
}
// 2a. Pass a named function by its name (function pointer)
fun add(x: Int, y: Int): Int = x + y
fun multiply(x: Int, y: Int): Int = x * y
println(calculate(4, 5, add)) // Output: 9
println(calculate(4, 5, multiply)) // Output: 20
// 2b. Or pass a closure (lambda) inline
println(calculate(4, 5) {|a, b| a - b}) // Output: -1
println(calculate(10, 3, {|a, b| a * b})) // Output: 30Extension Functions
Extension functions let you extend a class with new functionality without modifying its original definition. Declare them by prefixing the function name with the target class, and use this inside the function body to access the instance.
class Point(val x: Int, val y: Int)
// 1. Extension function without parameters
fun Point.sum(): Int {
return this.x + this.y
}
// 2. Extension function with parameters
fun Point.multiply(factor: Int): Int {
return (this.x + this.y) * factor
}
// 3. Static extension function
static fun Point.createDefault(): Point {
return Point(0, 0)
}
// 4. Extension function for built-in classes (e.g. String)
fun String.exclaim(): String {
return this + "!"
}
val p = Point(3, 4)
println(p.sum()) // Output: 7
println(p.multiply(2)) // Output: 14
val p2 = Point.createDefault()
println(p2.x) // Output: 0
val text = "hello"
println(text.exclaim()) // Output: hello!⚠️ Declaration Order
Autolang is executed sequentially from top to bottom. You must declare an extension function before you call it. Calling an extension function on an object before its definition line has been executed will result in a runtime error.
