Swift Academy · Lesson

Return values & multiple returns via tuples

Return single values and multiple values using tuples; name elements, ignore with underscore, and use early returns.

Lesson 2 of 38 steps

Return values & multiple returns via tuples is a free Swift Academy lesson on CoddyKit. This is lesson 2 of 3. 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 3 lessons in total.

Intro

Functions can return a single value or a tuple of multiple values. This keeps APIs simple on mobile-sized code.

Single return

Declare the return type with -> and use return to send back a result.

func double(_ x: Int) -> Int {
    return x * 2
}
print(double(5)) // 10

Tuple return (unnamed)

Return multiple values as a tuple; access elements by index.

func minMax(_ a: Int, _ b: Int) -> (Int, Int) {
    if a < b { return (a, b) }
    else { return (b, a) }
}
let pair = minMax(7, 3)
print("min =", pair.0, "max =", pair.1)

Named tuple elements

Name tuple elements for readable access like result.count.

func stats(_ xs: [Int]) -> (count: Int, sum: Int) {
    var s = 0
    for x in xs { s += x }
    return (xs.count, s)
}
let result = stats([2, 3, 5])
print("count =", result.count, "sum =", result.sum)

Destructuring

Destructure tuples into variables and ignore unused values with _.

func coords() -> (x: Int, y: Int) { (3, 4) }
let (x, _) = coords()  // ignore y
print("x =", x)

Early returns

Early returns keep code flat and signal special cases, often using optionals.

func safeDivide(_ a: Int, _ b: Int) -> Int? {
    if b == 0 { return nil }       // early return for invalid input
    return a / b
}
print(safeDivide(6, 2) ?? -1)
print(safeDivide(6, 0) ?? -1)

Multiple return values

Quick check: How can a function return multiple values?

Recap

Recap: Return single values or tuples; name elements for clarity, destructure on assignment, and use early returns for special cases.

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 “Return values & multiple returns via tuples” lesson free?

Yes — any 3 lessons of the Swift Academy learning path, including “Return values & multiple returns via tuples”, 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 3 lessons in total.

What will I learn in “Return values & multiple returns via tuples”?

Return single values and multiple values using tuples; name elements, ignore with underscore, and use early returns. 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 2 of 3.

How long does the “Return values & multiple returns via tuples” 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. Defining functions, parameters, labels, default/variadic
  2. Return values & multiple returns via tuples
  3. In-out params & basic error handling (throws)
← Back to Swift Academy