Start learning free

LESSON 42 OF 415 · FUNCTIONS I

Missing Return in Swift: Why the Code Won't Compile

Quiz Read the full explanation

Quiz: What would this code produce if we were to copy-paste it in a code editor and run it?

func getDouble(_ n: Int) -> Int {
	let doubled = n * 2
}

func mainFunction() {
	let result = getDouble(10)
	print(result)
}

Important Note:

Do not use an actual code editor to get the answer! It would defy the whole purpose of the quiz!

Pick your answer

Next lesson

SWIFT · FUNCTIONS I

Quiz Forgotten Return in Swift, explained

The quiz

The quiz shows a function that promises an Int and a caller that prints the result:

func getDouble(_ n: Int) -> Int {
    let doubled = n * 2
}

func mainFunction() {
    let result = getDouble(10)
    print(result)
}

The options are: A, it prints 20; B, it produces errors; C, it prints 10. The answer is B.

Why it is an error

The header -> Int is a promise: whoever calls getDouble will receive an Int. Swift checks that promise when it compiles the code. It looks at the body, sees that no path ends in a return, and refuses to build the program with an error like this:

error: missing return in global function expected to return 'Int'

Inside a class, the message says "instance method" instead of "global function". Because the error comes at compile time, the program never starts, so neither 20 nor 10 is printed.

Why not 20 or 10?

  • Not 20. The line let doubled = n * 2 computes 20 and stores it in a local constant. That constant disappears when the function ends. A value reaches the caller only through return. Swift also warns that doubled is never used.
  • Not 10. Nothing in the code produces 10 as a result. It is only the argument. Swift has no hidden default value for a function that forgets to return.

Two ways to repair it

If the function should give back the double, return it:

func getDouble(_ n: Int) -> Int {
    return n * 2
}

If the function was never meant to return anything, remove the promise instead by deleting -> Int. Then the call let result = getDouble(10) in the caller is the thing that no longer fits, so the function and its caller must agree.

The answer in Swift

The lesson asks for the answer to be stored in a constant named answer:

let answer = "B"

/* Explanation:
Notice that inside the function we only compute n * 2 but do nothing with the result afterwards, we do not return it from the function.

A function with no return statement inside it is called a "void" function and should be defined accordingly, like this:
func getDouble(_ n: Int) {
    let doubled = n * 2
}

But because we defined our function as an "Int" function ( func getDouble(_ n: Int) -> Int ), Swift expects an Int to be returned from the function.
And so when Swift notices that there is no actual "return" statement inside the function, it throws an error.
*/

Related points

  • Every path must return. A function that returns inside an if but not after it still fails with "missing return", because the condition might be false.
  • The one-line shortcut. A body that is a single expression may leave out return: func getDouble(_ n: Int) -> Int { n * 2 } is valid. The quiz function has a declaration statement, let doubled = ..., not an expression, so the shortcut does not apply.
  • Void functions. A function without -> returns nothing, and that is fine for functions that only print or change something.

Reading a quiz like this

For "what does this code produce" questions, work in the order the compiler does. First ask whether the code is valid: do the types agree, is every name declared, does every path return? Only if it is valid do you trace what it prints. Here the first check already fails, so the later lines never matter. Also note that mainFunction is only a function definition in the quiz, so even valid code would print nothing until something calls it.

Key takeaways

  • A declared return type must be matched by a return on every path.
  • A value computed in a function and not returned is lost.
  • Swift reports a missing return before the program runs.
Keep going with SwiftAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
Start learning free