DEV Community

Gamya
Gamya

Posted on

Returning Multiple Values from Functions in Swift

When you want to return a single value from a function, you write an arrow and a data type before the opening brace:

func isShinobi(name: String) -> Bool {
    name == "Naruto" || name == "Sasuke" || name == "Sakura"
}
Enter fullscreen mode Exit fullscreen mode

That compares a name against a list of known shinobi names, returning true or false.

But what if you need to return two or more values? Let's explore the options.


๐Ÿšซ Attempt 1 โ€” Using an Array

Say we want a function that sends back a character's first and last name:

func getCharacter() -> [String] {
    ["Monkey", "D. Luffy"]
}

let character = getCharacter()
print("Name: \(character[0]) \(character[1])")
Enter fullscreen mode Exit fullscreen mode

This is problematic โ€” it's hard to remember what character[0] and character[1] actually represent. If we ever change the order of the array, character[0] might suddenly become the last name instead of the first, and nothing would warn us.


๐Ÿšซ Attempt 2 โ€” Using a Dictionary

func getCharacter() -> [String: String] {
    [
        "firstName": "Monkey",
        "lastName": "D. Luffy"
    ]
}

let character = getCharacter()
print("Name: \(character["firstName", default: "Unknown"]) \(character["lastName", default: "Unknown"])")
Enter fullscreen mode Exit fullscreen mode

Now we have meaningful names โ€” firstName and lastName instead of 0 and 1. But look at that print() call: even though we know both keys will exist, Swift doesn't know that, so we still need to provide default values just in case.


โœ… Attempt 3 โ€” Using a Tuple

Tuples let us put multiple pieces of data into a single variable, but unlike arrays and dictionaries, tuples have a fixed size and can hold a mix of different types.

func getCharacter() -> (firstName: String, lastName: String) {
    (firstName: "Monkey", lastName: "D. Luffy")
}

let character = getCharacter()
print("Name: \(character.firstName) \(character.lastName)")
Enter fullscreen mode Exit fullscreen mode

Breaking that down:

  • The return type is (firstName: String, lastName: String) โ€” a tuple containing two strings
  • Each value in the tuple has a name, and these aren't in quotes โ€” they're specific labels, not arbitrary dictionary keys
  • Inside the function we send back a tuple matching exactly what we promised
  • When calling getCharacter(), we read the values using .firstName and .lastName

๐Ÿ†š Tuples vs Dictionaries โ€” What's the Real Difference?

They might look similar, but they behave very differently:

  • With a dictionary, Swift can't know ahead of time whether a key exists. We know character["firstName"] will be there, but Swift doesn't โ€” so we must provide a default value.
  • With a tuple, Swift knows exactly what's available because the tuple's type says so. character.firstName either compiles or it doesn't โ€” there's no chance of a typo like character["First Name"] silently returning nothing.
  • A dictionary could contain hundreds of other keys alongside firstName โ€” a tuple can't. It must contain exactly what its type says, nothing more and nothing less.

๐Ÿ’ก Three More Things About Tuples

1. You don't need to repeat the names in return

Since Swift already knows the names from the function's return type, this works exactly the same as the named version above:

func getCharacter() -> (firstName: String, lastName: String) {
    ("Monkey", "D. Luffy")
}
Enter fullscreen mode Exit fullscreen mode

2. Unnamed tuples use numerical indices

If a tuple's elements don't have names, access them with .0, .1, and so on:

func getCharacter() -> (String, String) {
    ("Monkey", "D. Luffy")
}

let character = getCharacter()
print("Name: \(character.0) \(character.1)")
Enter fullscreen mode Exit fullscreen mode

Numerical indices also work on named tuples, but using names is almost always clearer.

3. You can pull a tuple apart into separate constants

Here's the long way โ€” store the tuple, then copy each part out individually:

func getCharacter() -> (firstName: String, lastName: String) {
    (firstName: "Roronoa", lastName: "Zoro")
}

let character = getCharacter()
let firstName = character.firstName
let lastName = character.lastName

print("Name: \(firstName) \(lastName)")
Enter fullscreen mode Exit fullscreen mode

Or skip the middle step entirely and destructure the tuple straight from the function call:

let (firstName, lastName) = getCharacter()
print("Name: \(firstName) \(lastName)")
Enter fullscreen mode Exit fullscreen mode

And if you only need part of the tuple, use _ to ignore the rest:

let (firstName, _) = getCharacter()
print("Name: \(firstName)")
Enter fullscreen mode Exit fullscreen mode

When Should You Use an Array, a Set, or a Tuple? ๐Ÿค”

Now that we've covered arrays, sets, and tuples individually, here's how to pick the right one. Remember the key differences:

  • Arrays keep order and can have duplicates
  • Sets are unordered and can't have duplicates
  • Tuples have a fixed number of values, each with a fixed type

Here's how that plays out with some examples:

Scenario Best Choice Why
A list of all jutsu names a player has unlocked, where no jutsu can be unlocked twice and order doesn't matter Set No duplicates, order irrelevant
Episodes of a show a user has watched Set or Array Set if you only care whether they watched it; Array if the order watched matters
High scores in a game Array Order matters, and duplicate scores are possible if two players tie
Items on a to-do list Array Predictable order is important
A character's exact stats โ€” say, two integers for strength and speed, plus a Boolean for whether they've unlocked their final form Tuple A fixed number of values with fixed, specific types

Wrap Up ๐ŸŽฌ

  • Returning multiple values from a function? Skip arrays and dictionaries โ€” reach for a tuple
  • Tuples give you named, ordered, fixed-type values without the need for default values or string keys
  • Tuple names don't need repeating in return, unnamed tuples use .0, .1, etc., and tuples can be destructured directly into separate constants with let (a, b) = ...
  • When choosing between array, set, and tuple: think about whether order matters, whether duplicates are allowed, and whether you're dealing with a fixed, known number of values

Top comments (0)