0Pricing
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.

Return values & multiple returns via tuples is a free Swift Academy lesson on CoddyKit — lesson 2 of 3. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Swift Academy learning path, one of 3 lessons in the course, and your progress syncs across the web and the CoddyKit app.

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.

Frequently asked questions

Is the “Return values & multiple returns via tuples” lesson free?

Yes — the full text of “Return values & multiple returns via tuples” is free to read here on the web, and the Swift Academy course includes 3 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Swift Academy course, upgrade to CoddyKit PRO.

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; this is — lesson 2 of 3, so you can start here or from the beginning and move at your own pace.

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