inout / Return Types / Multiple Return Values
| Since: | Swift 1.0(2014) |
|---|
Swift's inout parameters let you modify variables outside a function directly. You can also return multiple values at once using tuples, or suppress unused-return warnings with @discardableResult.
Syntax
// inout parameter (prefix the argument with & when calling)
func functionName(_ param: inout Type) { }
functionName(&variableName)
// Multiple return values (tuple)
func functionName() -> (Type1, Type2) {
return (value1, value2)
}
// Named tuple return
func functionName() -> (name1: Type1, name2: Type2) {
return (name1: value1, name2: value2)
}
// @discardableResult
@discardableResult
func functionName() -> Type { }
Syntax Reference
| Syntax | Description |
|---|---|
| inout Type | Passes the argument by reference, allowing the function to modify the original variable. |
| &variableName | Prefix the variable with & when passing it as an inout argument. |
| -> (Type1, Type2) | Returns multiple values as a tuple. |
| -> (name: Type, ...) | Returns multiple values as a named tuple. |
| let (a, b) = function() | Destructures a tuple return value into individual variables. |
| @discardableResult | Suppresses the compiler warning when the return value is not used. |
Sample Code
sample_inout_return.swift
// inout: modify the original variable inside the function
func doubleValue(_ value: inout Int) {
value *= 2
}
var number = 10
doubleValue(&number)
print(number) // 20
// Swap two values using inout
func swap<T>(_ a: inout T, _ b: inout T) {
let temp = a
a = b
b = temp
}
var x = "Hello"
var y = "World"
swap(&x, &y)
print(x, y) // World Hello
// Multiple return values (tuple)
func minMax(array: [Int]) -> (min: Int, max: Int) {
var currentMin = array[0]
var currentMax = array[0]
for value in array {
if value < currentMin { currentMin = value }
if value > currentMax { currentMax = value }
}
return (min: currentMin, max: currentMax)
}
let result = minMax(array: [3, 1, 7, 2, 9, 4])
print("Min: \(result.min), Max: \(result.max)")
// Destructure the tuple return value
let (minimum, maximum) = minMax(array: [5, 2, 8, 1, 6])
print("Min: \(minimum), Max: \(maximum)")
// @discardableResult
@discardableResult
func updateCache() -> Bool {
// Update cache
return true
}
updateCache() // No warning even though the return value is ignored
Running the above produces the following output:
swift inout_return.swift 20 World Hello
Overview
Swift function parameters are passed by value by default. To modify the original variable inside a function, use inout and prefix the argument with & at the call site. Swift's standard library swap(_:_:) function uses the same mechanism.
Named tuples are a convenient way to return multiple values from a function. You cannot pass a constant (let) or a literal as an inout argument — only variables (var) are allowed. Also, inout parameters cannot have default values.
For the basics of function definitions, see func / Argument Labels / Default Parameters.
If you find any errors or copyright issues, please contact us.