Swift Academy · Lesson

In-Out Parameters and Multiple Returns

Mutating arguments with inout and returning multiple values via tuples.

Lesson 3 of 412 steps

In-Out Parameters and Multiple Returns is a free Swift Academy lesson on CoddyKit. This is lesson 3 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the Swift Academy learning path, and your progress syncs across the web and the CoddyKit app. The Swift Academy course includes 4 lessons in total.

Welcome

Swift functions normally work with copies of values. `inout` parameters let a function mutate the caller's variable directly. Tuples let functions return multiple values at once.

The inout Keyword

Mark a parameter `inout` to mutate the original: ```swift func doubleInPlace(_ value: inout Int) { value *= 2 } var x = 5 doubleInPlace(&x) print(x) // 10 ``` The `&` prefix at the call site signals the variable may be mutated.

inout with Structs

```swift struct Point { var x: Double; var y: Double } func translate(_ p: inout Point, dx: Double, dy: Double) { p.x += dx p.y += dy } var origin = Point(x: 0, y: 0) translate(&origin, dx: 3.0, dy: 4.0) // origin is now (3.0, 4.0) ```

inout Restrictions

• Constants (`let`) cannot be passed as `inout` • Literals cannot be passed as `inout` • Subscripts and computed properties can be `inout` if they have setters ```swift let c = 5 doubleInPlace(&c) // ❌ cannot pass let as inout ```

Returning Multiple Values with Tuples

Return several values by wrapping them in a tuple: ```swift func minMax(of array: [Int]) -> (min: Int, max: Int) { (array.min()!, array.max()!) } let result = minMax(of: [3, 1, 7, 2]) print(result.min, result.max) // 1 7 ```

Named vs Unnamed Tuple Elements

Named elements make tuples self-documenting: ```swift let named: (width: Int, height: Int) = (1920, 1080) print(named.width) // 1920 let unnamed = (1920, 1080) print(unnamed.0) // 1920 ``` Prefer names for tuples returned from functions.

Destructuring Tuple Returns

Unpack a tuple return into multiple variables: ```swift let (minimum, maximum) = minMax(of: [5, 3, 8]) print(minimum) // 3 print(maximum) // 8 ``` Use `_` to discard unwanted elements: `let (minimum, _) = minMax(of: arr)`.

Optional Tuple Returns

Return nil for failure cases: ```swift func parse(_ s: String) -> (Int, Int)? { let parts = s.split(separator: ",") guard parts.count == 2, let a = Int(parts[0]), let b = Int(parts[1]) else { return nil } return (a, b) } ```

swap With inout

The classic swap using inout: ```swift func swap(_ a: inout T, _ b: inout T) { let temp = a a = b b = temp } var m = 1, n = 2 swap(&m, &n) print(m, n) // 2 1 ``` Swift's stdlib provides `swap(_:_:)` already — no need to write your own.

When to Use inout vs Return

Prefer returning a new value over mutating inout: ```swift // Prefer: func normalised(_ v: SIMD3) -> SIMD3 { v / v.magnitude } // Use inout only when mutation is expected API contract, e.g. algorithms on large arrays func sort(_ arr: inout [Int]) { arr.sort() } ```

Quick Check

What prefix is required at the call site when passing a variable to an `inout` parameter?

Recap

Key takeaways: • `inout` allows functions to mutate caller variables; pass with `&` • Cannot pass `let` constants or literals as inout • Tuples return multiple values neatly; name elements for clarity • Destructure tuples: `let (a, b) = f()` • Prefer returning new values; use inout only when mutation is the API contract Next: functions as first-class values.
Free to start

Learn Swift with an AI tutor — free

Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.

Courses
122
Lessons
409

Frequently Asked Questions

Is the “In-Out Parameters and Multiple Returns” lesson free?

Yes — any 3 lessons of the Swift Academy learning path, including “In-Out Parameters and Multiple Returns”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The Swift Academy course includes 4 lessons in total.

What will I learn in “In-Out Parameters and Multiple Returns”?

Mutating arguments with inout and returning multiple values via tuples. You practise Swift Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Swift Academy?

No prior experience is required. Swift Academy on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 3 of 4.

How long does the “In-Out Parameters and Multiple Returns” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Swift Academy lesson?

Yes. Every Swift Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Argument Labels and Parameter Names
  2. Default and Variadic Parameters
  3. In-Out Parameters and Multiple Returns
  4. Functions as First-Class Values
← Back to Swift Academy