Start learning free

LESSON 45 OF 678 · FUNCTIONS I

Print Instead of Return in Swift: Fixing the Code

Practice Read the full explanation

We've written a program and expected it to print Hey, Andy but when we run it we get some errors.

Fix our code so that it works and prints what we want.

STEP 1 OF 1
    Ctrl + Enter to check

    SWIFT · FUNCTIONS I

    Buggy Code Print Instead Of Return in Swift, explained

    What the code is supposed to do

    The function getGreeting should build a greeting for name and give it back to its caller, and the caller prints it. The program should print Hey, Andy exactly once. The code fails to compile:

    func getGreeting(_ name: String) -> String {
        print("Hey, " + name)
    }
    
    
    // Do not change the code below:
    func mainFunction() {
        let greeting = getGreeting("Andy")
        print(greeting)
    }
    

    The compiler says:

    error: cannot convert return expression of type '()' to return type 'String'
    

    What goes wrong

    There are two different actions here, and the code mixes them up:

    • print(...) writes text on the screen. It gives nothing back. In Swift, "nothing" is the empty tuple ().
    • return ... hands a value to the code that called the function.

    The header says -> String, so every call must produce a string. The body, however, is one expression, print("Hey, " + name). Swift takes a single expression as the value to return, so it tries to return the () from print as a String and reports that this conversion is impossible.

    Even if the compiler allowed it, the design would be wrong. The caller already prints the result, so a function that printed too would show the greeting twice and give back nothing for greeting to hold.

    Fixing it step by step

    1. Leave the caller alone. mainFunction stores the result in greeting and prints it, which is the right division of work.
    2. Replace print with return. The function should hand the text back, not show it.
    3. Keep the text the same. "Hey, " + name joins the fixed text with the parameter. The space after the comma is inside the quotes, because + adds nothing between the pieces.

    The fixed code in Swift

    func getGreeting(_ name: String) -> String {
        return "Hey, " + name
    }
    
    
    // Do not change the code below:
    func mainFunction() {
        let greeting = getGreeting("Andy")
        print(greeting)
    }
    

    Now getGreeting("Andy") returns the string Hey, Andy, greeting holds it, and print(greeting) shows it once.

    Swift also accepts the shorter form without the word return, because the body is one expression: func getGreeting(_ name: String) -> String { "Hey, " + name }. Both do the same thing.

    Common mistakes

    • Removing -> String instead. The function would then compile, but let greeting = getGreeting("Andy") would no longer make sense, since there is no value to store. That part of the code must not change.
    • Keeping both print and return. This compiles if the print comes first, but then the greeting is printed twice: once inside the function and once by the caller.
    • Dropping the space. "Hey," + name returns Hey,Andy.
    • Returning a different type. return 5 in a function declared -> String is a compile error.

    Checking your fix

    Try other names with the fixed function. Each call returns a new string, and the caller decides what to do with it:

    • getGreeting("Mike") returns Hey, Mike.
    • getGreeting("") returns Hey, , with the trailing space, because an empty name is still a valid string.

    Because the value is returned, the same function could just as well feed another function, or be compared with a string, which a function that only prints can't do.

    Key takeaways

    • Use return to give a value back and print only to display it.
    • A function that returns a value lets the caller decide what to do with it.
    • A -> String function whose only line is print(...) fails with a () to String conversion error.
    Keep going with Swift650+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free