inout Keyword
This page was last reviewed on Jan 13, 2024.
Dot Net Perls
Inout, parameters. Most parameters are copied as values. They never affect the original call site. But inout parameters are different—they share a memory location.
With inout, changes in the called method are reflected in the variables at the call site. The variables are not separate. This enables some code techniques.
An example. Let us begin with this example. The size() method receives an inout parameter of type Int. We call size() by passing it a reference to the "x" variable.
And The size() func modifies the inout parameter to equal 10. After it returns, "x" still equals 10.
func size(x: inout Int) { // Set out parameter. x = 10 } // Value equals 0. var x = 0 print(x) // Call size with inout argument. size(x: &x) // Now variable equals 10. print(x)
0 10
Recursive method. Sometimes a complex problem may require recursion. With an inout argument, we can share a variable among many recursive calls.
Detail In this example the x() func use an inout depth argument. When depth reaches 10, no more calls occur.
Tip The memory location of the depth argument is shared among all calls of the "X" func.
func x(depth: inout Int) { // Increase depth value. depth += 1 if (depth < 10) { // Use recursion to increase depth again. x(depth: &depth) } } // Count depth of recursion. var depth = 0 x(depth: &depth) print(depth)
Immutable error. Here is an error that may occur with inout arguments. We cannot use an immutable let value as an argument when an inout argument is needed.
Note This makes sense. A constant cannot be modified—its memory location should not be available.
func test(name: inout String) { } // Cannot use let keyword with inout. let name = "" test(name: &name)
Cannot pass immutable value as inout argument: 'name' is a 'let' constant
Concepts. With inout parameters in Swift, we can use multiple return values. This is an alternative to returning a tuple or modifying a class argument.
Info It is unclear which approach for multiple return values is best. Try them all and decide.
A syntax note. We must use the ampersand when passing arguments to a method that receives an inout argument. The ampersand means "address of" as opposed to "value of."
Syntax, continued. For Swift 3, we place inout after the argument's label. In previous versions of Swift the inout keyword was before the label.
A summary. The inout keyword is powerful. It provides an alternative to tuples for complex methods with many return values. It may make some code more complex. Use it with care.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
This page was last updated on Jan 13, 2024 (edit link).
© 2007-2024 Sam Allen.