Return multiple values from a Swift function


In Swift programming, it is possible to return multiple values from a Swift function. We can do this by using tuples.

A tuple can store multiple values of different data types. So it is a group of different values. These values can be of different types or the same types.

To learn more about tuples, please read this tutorial – Tuple in Swift

Below is an example where you can see we have created a function that returns multiple values that are integer, string and double type:

func personData() -> (Int, String, Double) {
    return (35, "Female", 57.8 )
}

You can think of the values are the age, gender and weight of the person.

Let’s check the output if we call the above function inside the print method:

print( personData() )

Output:

(35, "Female", 57.8)

You can see that the output print the tuple values.

We can also access the individual values from the returned tuple. We can do it using the respective indexes of tuple items. Below is how we can do it:

let age = person.0
let gender = person.1
let weight = person.2

The complete example of returning multiple values from a Swift function using tuple

Now see the complete example below:

func personData() -> (Int, String, Double) {
    return (35, "Female", 57.8 )
}

// Store the function in a variable
let person = personData()

// Access individual tuple values by their indexes
let age = person.0
let gender = person.1
let weight = person.2

// Print individual values from returned tuple
print("Person age:",age)
print("Person gender:",gender)
print("Person weight:",weight)

Output:

Person age: 35
Person gender: Female
Person weight: 57.8

In the above example, we have created a function personData() and returned a tuple value containing the age, gender and weight values.

After that, we store the function in a variable and access each value from the returned tuple using their indexes. In the end, we print each value.

Naming tuple elements in function definition

Instead of accessing tuple values by index, we can give names of each tuple element that can be used to access the values from the tuple returned by the function. It will make more sense and increase the code readability when we give name for each tuple value.

Below is how we can do it:

func personData() -> (age: Int, gender: String, weight: Double) {
    return (35, "Female", 57.8 )
}

We can now access the values of the tuple by the name of the values:

// Store the function in a variable
let person = personData()

// Access individual tuple values by their name
let age = person.age
let gender = person.gender
let weight = person.weight

See the complete example

Below is the complete example:

func personData() -> (age: Int, gender: String, weight: Double) {
    return (35, "Female", 57.8 )
}

// Store the function in a variable
let person = personData()

// Access individual tuple values by their name
let age = person.age
let gender = person.gender
let weight = person.weight

// Print individual values from returned tuple
print("Person age:",age)
print("Person gender:",gender)
print("Person weight:",weight)

Output:

Person age: 35
Person gender: Female
Person weight: 57.8

You can see the same output as we see in our previous example. The only difference is that, in this case, we have provided the name for each tuple value at the time of function creation. We are using the names instead of index numbers to access individual tuple values from the tuple returned by the function.