AutolangDocs

Functions

Functions in Autolang represent reusable units of logic. Declared with the fun keyword, functions support default arguments, named parameters, higher-order execution, and extension functions.

Declaration & Shorthand Syntax

Specify parameter types explicitly. Return types are declared after :. For single-expression functions, use = expression shorthand to infer or assign the return value directly.

// Standard function block fun calculateSubtotal(unitPrice: Float, quantity: Int): Float { return unitPrice * quantity } // Single-expression function shorthand fun applyTax(subtotal: Float, rate: Float = 0.08): Float = subtotal * (1.0 + rate) val subtotal = calculateSubtotal(120.0, 3) val totalWithTax = applyTax(subtotal) println("Final invoice amount: " + totalWithTax)

Default & Named Arguments

Parameters can declare default values. When calling functions, arguments can be provided positionally, by parameter name, or in any order when named. Any parameter with a default value can be omitted.

fun generateReport(title: String, format: String = "PDF", maxRecords: Int = 100): String { return "[" + format + "] " + title + " (limit: " + maxRecords + ")" } // Positional call with defaults println(generateReport("Quarterly Financials")) // Named arguments in arbitrary order println(generateReport(maxRecords = 500, title = "Inventory Summary", format = "CSV")) // Combining positional and named arguments println(generateReport("Audit Log", maxRecords = 25))

Higher-Order Functions & Function Pointers

Autolang treats functions as first-class citizens. Declare function types with (ParamType1, ...) -> ReturnType (or Void). You can pass named functions (function pointers) or inline lambdas interchangeably.

// Higher-order function accepting a transformation callback fun transformLedger(items: Array<Int>, transformer: (Int) -> Int): Array<Int> { val results: Array<Int> = [] for (item in items) { results.add(transformer(item)) } return results } // Named function pointer fun applyStandardDiscount(price: Int): Int = price - 10 val prices = [100, 250, 400] // Passing a function pointer val discounted = transformLedger(prices, applyStandardDiscount) println("Discounted prices: " + discounted.joinToString(", ")) // Trailing lambda syntax val doubled = transformLedger(prices) { item -> item * 2 } println("Doubled prices: " + doubled.joinToString(", "))

Extension Functions

Extension functions add capabilities to classes or built-in types without modifying their source code. Inside the extension function, this references the receiver instance.

class Money(val amount: Int, val currency: String) // Extension function on user-defined class fun Money.formatCurrency(): String { return this.currency + " " + (this.amount / 100) + "." + (this.amount % 100) } // Extension function on built-in String fun String.wrapQuotes(): String = "\"" + this + "\"" // Generic extension function on Array fun <T> Array<T>.secondElement(): T? { if (this.size() > 1) { return this[1] } return null } val balance = Money(4550, "USD") println("Formatted: " + balance.formatCurrency()) // USD 45.50 val code = "TX_99".wrapQuotes() println("Code: " + code) // "TX_99" val entries = ["first", "second", "third"] println("Second entry: " + entries.secondElement()) // second

Local Static Variables

Functions can declare static var inside their body. The variable retains its value across invocations throughout the runtime session.

fun getNextTransactionSequence(): Int { static var sequenceNumber = 1000 sequenceNumber += 1 return sequenceNumber } println("Seq 1: " + getNextTransactionSequence()) // 1001 println("Seq 2: " + getNextTransactionSequence()) // 1002