You've learned how to create functions and pass values into them — but functions can also send data back to wherever they were called from. They do some work, then hand back the result.
Swift has tons of built-in functions that do this. For example sqrt() takes a number and sends back its square root:
let root = sqrt(169)
print(root)
// 13.0
If you want your own functions to send back a value, you need two things:
- An arrow (
->) followed by a data type before the opening brace — this tells Swift what kind of data will come back. - The
returnkeyword to actually send the value back.
🎲 A Simple Example — Rolling Dice
Imagine you're building a game where characters roll dice. Instead of writing Int.random(in: 1...6) everywhere, you could make it a function:
func rollDice() -> Int {
return Int.random(in: 1...6)
}
let result = rollDice()
print(result)
-> Int tells Swift this function always sends back an integer, and return is what actually sends it back.
The benefit? If your game later needs a 20-sided dice (say for a D&D style battle), you only change this one function — every place that calls rollDice() automatically uses the new dice. 🎲
Important: If you say a function returns
Int, Swift makes sure it always returns anInt. Forget to return something and your code simply won't build.
🌀 A More Complex Example — Comparing Letters
Let's check whether two strings contain the same letters, regardless of order. For example "naruto" and "toruna" should be considered the same because they contain the same letters.
The trick: calling .sorted() on a string returns a new string with the letters in alphabetical order. Sort both strings and compare with ==.
func areLettersIdentical(string1: String, string2: String) -> Bool {
let first = string1.sorted()
let second = string2.sorted()
return first == second
}
Breaking it down:
- The function accepts two
Stringparameters - It returns a
Bool— so it must always send backtrueorfalse - Inside, both strings are sorted and compared
✂️ Shortening the Function — Skipping the Temporary Constants
We don't actually need those first and second constants — we can compare the sorted results directly:
func areLettersIdentical(string1: String, string2: String) -> Bool {
return string1.sorted() == string2.sorted()
}
🪄 When Can You Skip the return Keyword?
Here's where it gets interesting. Swift lets us drop return entirely — but only in one specific situation: when the function contains a single expression.
Expression vs Statement
This matters because Swift draws a line between two kinds of code:
-
Expression — code that resolves to a single value. Examples:
5 + 8(becomes13),"Naruto".count(becomes6), orgreet("Sasuke")(might become"Hello, Sasuke!") -
Statement — code that performs an action rather than producing a value. Examples: creating a variable (
let name = "Otis"), starting a loop, or anifcheck that runs multiple lines
| Code | Type | Why |
|---|---|---|
5 + 8 |
Expression | Resolves to 13
|
isAdmin == true OR isOwner == true |
Expression | Resolves to true or false
|
let name = "Otis" |
Statement | Doesn't produce a value you can return |
A multi-line if block with print() calls |
Statement | Performs actions, not a single value |
When a function's body is just one expression, Swift already knows that expression is the value to send back — so return becomes optional:
func areLettersIdentical(string1: String, string2: String) -> Bool {
string1.sorted() == string2.sorted()
}
Same goes for our dice roller:
func rollDice() -> Int {
Int.random(in: 1...6)
}
⚠️ This only works when the function has exactly one line, and that line must directly produce the value you promised to return.
📐 A Third Example — Pythagoras' Theorem
Remember Pythagoras' theorem from school? For a right-angled triangle, the hypotenuse c can be found by squaring both other sides, adding them together, then taking the square root.
func pythagoras(a: Double, b: Double) -> Double {
let input = a * a + b * b
let root = sqrt(input)
return root
}
let c = pythagoras(a: 3, b: 4)
print(c)
// 5.0
This can also be boiled down to one line with return removed:
func pythagoras(a: Double, b: Double) -> Double {
sqrt(a * a + b * b)
}
🔀 Using if/else Without return
Swift is smart enough to treat if and switch as expressions too — as long as every branch directly produces a value rather than creating new variables.
This is allowed ✅ — both branches directly produce a String:
func greet(name: String) -> String {
if name == "Madara Uchiha" {
"Oh wow, the legendary villain!"
} else {
"Hello, \(name)"
}
}
This is not allowed ❌ — the else branch creates a new constant before returning it, making it a statement, not an expression:
func greet(name: String) -> String {
if name == "Madara Uchiha" {
"Oh wow, the legendary villain!"
} else {
let greeting = "Hello, \(name)"
return greeting
}
}
🎯 Assigning an if Result Directly
Because if can act as an expression, Swift even lets you assign its result straight to a constant:
func greet(name: String) -> String {
let response = if name == "Madara Uchiha" {
"Oh wow, the legendary villain!"
} else {
"Hello, \(name)"
}
return response
}
This might look strange at first, but it's basically the same idea as the ternary operator:
func greet(name: String) -> String {
let response = name == "Madara Uchiha" ? "Oh wow, the legendary villain!" : "Hello, \(name)"
return response
}
Returning Multiple Values from a Function 🎁
A Swift function can only declare one return type — but that doesn't mean it can only send back one piece of data. There are two common ways to return multiple values:
- A tuple, such as
(name: String, age: Int) - A collection, such as an array or dictionary
🚫 Attempt 1 — Returning an Array
Let's say we want a function that returns a character's first name and last name. We could try an array:
func getCharacter() -> [String] {
["Monkey", "Luffy"]
}
let character = getCharacter()
print(character[0])
This works, but it's risky:
- What if some cultures write surnames first? Other developers might expect index
0to be the last name - What if we add a middle name later? Everything after it shifts, breaking
character[1]
🚫 Attempt 2 — Returning a Dictionary
func getCharacter() -> [String: String] {
["first": "Monkey", "last": "Luffy"]
}
let character = getCharacter()
print(character["first"])
Better — now order doesn't matter. But this still has problems:
-
character["First"](capital F) would silently fail and returnnil - The dictionary might be missing a key entirely — what if a character like Zoro only goes by one name?
- Every value comes back as an optional, so we have to unwrap it every time we read it
✅ Attempt 3 — Returning a Tuple
Tuples solve all of this by letting us specify exactly what comes back: each value's name, type, order, and whether it's optional.
func getCharacter() -> (first: String, last: String) {
(first: "Monkey", last: "Luffy")
}
let character = getCharacter()
print(character.first)
// Monkey
Why this is better:
- ✅ First name always comes first, last name always second — guaranteed by the function signature
- ✅ Adding a middle name later won't shift the position of
firstorlast - ✅ No case-sensitive string keys to get wrong —
character.firsteither compiles or it doesn't - ✅ No optionals — if a character like Zoro only has one name,
lastwould simply be an empty string, notnil
Wrap Up 🎬
- Use
-> Typebefore a function's opening brace to declare what it returns, andreturnto send the value back - If a function body is a single expression,
returncan be skipped entirely — this works for plain expressions, and even forif/switchwhere every branch produces a value directly - A statement (like creating a new variable) can't be the thing Swift returns automatically — only expressions can
- To return multiple values, prefer a tuple over an array or dictionary — it gives you named, ordered, non-optional values that are safer and clearer to work with
Top comments (0)