Functions
A function is a reusable block of code that performs a specific task
You can pass data, known as parameters, into a function
Functions help you organize and reuse code by defining logic once and calling it multiple times
Use the func keyword to declare a function
Create a function
func functionName() {
// code to be executed
}Example
func greet() {
println("Hello world")
}
greet()Parameters
You can pass parameters into a function
func functionName(parameter1: type, parameter2: type, ...) {
// code to be executed
}Function parameters are immutable by default
Example
func greet(name: String) {
println("Hello " + name)
// name = "hi" -> wrong because parameters are immutable
}
greet("John")Default Parameter Values
Function parameters can have default values. If a function is called without an argument for that parameter, the default value is used.
func functionName(parameter: type = defaultValue) {
// code to be executed
}Example
func add(a: Int = 1, b: Int = 2): Int {
return a + b
}
// Uses default values for both 'a' (1) and 'b' (2)
println(add()) // Output: 3
// Overrides 'a' with 5, uses default for 'b' (2)
println(add(5)) // Output: 7
// Overrides both 'a' and 'b'
println(add(5, 10)) // Output: 15Return value
If a function returns a value, you must declare its return type
func functionName(parameter1: type, parameter2: type, ...): type {
// code to be executed
}Use the return keyword to return a value
func multiply(a: Int, b: Int): Int {
return a * b
}
val result = multiply(3, 5)
println(result)
//>> 15Single-expression function
If a function contains only a single expression, you can use the shorthand syntax.
The expression result will be returned automatically.
func functionName(parameter1: type, parameter2: type, ...) = expression
func functionName(parameter1: type, parameter2: type, ...): type = expressionExample
func plus(a: Int, b: Int): Int = a + b
println(plus(2, 3))
//>> 5Function Types & Callbacks
Autolang supports Higher-Order Functions by allowing you to pass functions as arguments to other functions. This is done using Function Pointers.
To declare a parameter that accepts a function, use the function type syntax: (ParamType1, ParamType2, ...) -> ReturnType. If the function returns nothing, use Void.
// A function that takes no parameters and returns nothing
func doSomething(action: () -> Void) { ... }
// A function that takes an Int and String, and returns a Bool
func process(callback: (Int, String) -> Bool) { ... }Since Autolang does not currently use lambdas or closures, you must define a named function and pass it by name. Under the hood, Autolang passes the memory address of the function to be executed later (a callback).
// 1. Define a higher-order function that accepts a function pointer
func calculate(a: Int, b: Int, operation: (Int, Int) -> Int): Int {
// Call the function pointer
return operation(a, b)
}
// 2. Define callback functions matching the signature (Int, Int) -> Int
func add(x: Int, y: Int): Int = x + y
func multiply(x: Int, y: Int): Int = x * y
// 3. Pass the functions by their name
println(calculate(4, 5, add)) // Output: 9
println(calculate(4, 5, multiply)) // Output: 20