Functions
Functions are named, reusable blocks of code. Declare a function with the fun keyword.
Declaration
fun greet(name: String) {
print("Hello, \{name}!")
}
greet(name: "Alice")
Parameters
Each parameter has a name and a type. By default, the parameter name is also used as the argument label at the call site.
Default Parameter Values
A parameter can have a default value, making it optional at the call site:
fun greet(name: String, greeting: String = "Hello") {
print("\{greeting}, \{name}!")
}
greet(name: "Alice") // Prints "Hello, Alice!"
greet(name: "Alice", greeting: "Hi") // Prints "Hi, Alice!"
A parameter with a default value can appear anywhere in the parameter list. If a named parameter with a default is omitted, all other named parameters can still be provided in any order.
Custom Argument Labels
Write a label before the parameter name to use a different label at the call site:
fun clip(_ value: Int, from min: Int, to max: Int) -> (Int) {
if value < min { return min }
if value > max { return max }
return value
}
var clamped = clip(42, from: 0, to: 10)
Use _ as the label to omit it entirely, making the parameter positional.
Named Parameter Ordering
Positional parameters (declared with _) must appear in the order they are defined. Named parameters can be provided in any order.
clip(42, to: 10, from: 0) // valid — named params can be reordered
clip(from: 0, 42, to: 10) // invalid — positional param must come first
Return Types
Declare return types with -> followed by the type in parentheses:
fun double(_ x: Int) -> (Int) {
return x * 2
}
No Return Value
Functions that don't return a value can use -> () or omit the return type entirely:
fun log(_ message: String) {
print(message)
}
Multiple Return Values
Return multiple values by listing their types in the return type and separating the values with a comma after return:
fun min_max(_ values: [Int]) -> (Int, Int) {
var lo = values[0]
var hi = values[0]
for v in values {
if v < lo { lo = v }
if v > hi { hi = v }
}
return lo, hi
}
var low, high = min_max([3, 1, 5, 2])
Nested Functions
Functions can be defined inside other functions and have access to variables in the enclosing scope:
fun process(value: Int) -> (Int) {
fun clamp(_ x: Int) -> (Int) {
return x < 0 ? 0 : x
}
return clamp(value)
}
Functions as Values
Functions are first-class values. They can be stored in variables, passed as arguments, and returned from other functions.
fun apply(_ value: Int, transform: (Int) -> (Int)) -> (Int) {
return transform(value)
}
var result = apply(5, transform: fun (x) { return x * 2 })
A function can also return another function:
fun make_adder(by amount: Int) -> ((Int) -> (Int)) {
fun adder(_ x: Int) -> (Int) {
return x + amount
}
return adder
}
var add_ten = make_adder(by: 10)
print("\{add_ten(5)}") // Prints 15
Closures
A closure is an anonymous function expression. Closures capture variables from the surrounding scope.
var index = find_index(in: [10, 20, 30, 40], by: fun (value) {
return value == 30
})
When the target type is known — such as when passing a closure to a function parameter with a declared function type — parameter types and the return type can be omitted and are inferred automatically:
fun apply(_ value: Int, transform: (Int) -> (Int)) -> (Int) {
return transform(value)
}
// Parameter type and return type are inferred from the parameter type (Int) -> (Int)
var result = apply(5, transform: fun (x) { return x * 2 })
This also applies when assigning a closure to a variable with an explicit type annotation:
var double: (Int) -> (Int) = fun (x) { return x * 2 }
Any named function can also be coerced into a closure value:
fun greet(name: String, greeting: String = "Hello") {
print("\{greeting}, \{name}!")
}
// Called normally — labels and default value available
greet(name: "Alice") // ok
greet(name: "Alice", greeting: "Hi") // ok
// Coerced into a closure — labels and default value are lost
var fn = greet
fn("Alice", "Hi") // ok — all arguments must be provided positionally
fn(name: "Alice") // error — labels not available
fn("Alice") // error — default value not available