DEV Community

Gamya
Gamya

Posted on

Returning Values from Functions in Swift

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
Enter fullscreen mode Exit fullscreen mode

If you want your own functions to send back a value, you need two things:

  1. An arrow (->) followed by a data type before the opening brace — this tells Swift what kind of data will come back.
  2. The return keyword 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)
Enter fullscreen mode Exit fullscreen mode

-> 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 an Int. 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
}
Enter fullscreen mode Exit fullscreen mode

Breaking it down:

  • The function accepts two String parameters
  • It returns a Bool — so it must always send back true or false
  • 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()
}
Enter fullscreen mode Exit fullscreen mode

🪄 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 (becomes 13), "Naruto".count (becomes 6), or greet("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 an if check 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()
}
Enter fullscreen mode Exit fullscreen mode

Same goes for our dice roller:

func rollDice() -> Int {
    Int.random(in: 1...6)
}
Enter fullscreen mode Exit fullscreen mode

⚠️ 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
Enter fullscreen mode Exit fullscreen mode

This can also be boiled down to one line with return removed:

func pythagoras(a: Double, b: Double) -> Double {
    sqrt(a * a + b * b)
}
Enter fullscreen mode Exit fullscreen mode

🔀 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)"
    }
}
Enter fullscreen mode Exit fullscreen mode

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
    }
}
Enter fullscreen mode Exit fullscreen mode

🎯 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
}
Enter fullscreen mode Exit fullscreen mode

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
}
Enter fullscreen mode Exit fullscreen mode

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:

  1. A tuple, such as (name: String, age: Int)
  2. 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])
Enter fullscreen mode Exit fullscreen mode

This works, but it's risky:

  • What if some cultures write surnames first? Other developers might expect index 0 to 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"])
Enter fullscreen mode Exit fullscreen mode

Better — now order doesn't matter. But this still has problems:

  • character["First"] (capital F) would silently fail and return nil
  • 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
Enter fullscreen mode Exit fullscreen mode

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 first or last
  • ✅ No case-sensitive string keys to get wrong — character.first either compiles or it doesn't
  • ✅ No optionals — if a character like Zoro only has one name, last would simply be an empty string, not nil

Wrap Up 🎬

  • Use -> Type before a function's opening brace to declare what it returns, and return to send the value back
  • If a function body is a single expression, return can be skipped entirely — this works for plain expressions, and even for if/switch where 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)