Start learning free

LESSON 49 OF 415 · FUNCTIONS I

Return Order in Swift: Fixing the Buggy Code

Practice Read the full explanation

We've written a program and expected it to print:

Hey bro!
How are you doing?

but we get a different output. Fix our code so that it prints what we want.

STEP 1 OF 1
    Ctrl + Enter to check

    SWIFT · FUNCTIONS I

    Buggy Code 6 in Swift, explained

    What the code is supposed to do

    The function greetAndReturn should print Hey bro! and then return the text How are you doing?. The method mainFunction stores that returned text and prints it, so the full output should be two lines:

    Hey bro!
    How are you doing?
    

    The starting code prints only How are you doing?:

    class Solution {
        func greetAndReturn() -> String {
            return "How are you doing?"
            print("Hey bro!")
        }
    
        func mainFunction() {
            let question = greetAndReturn()
            print(question)
        }
    }
    

    What goes wrong

    Swift runs statements from top to bottom. When it reaches return, the function ends and the value goes back to the caller. The print("Hey bro!") below it is never reached.

    The compiler notices this and reports a warning:

    warning: code after 'return' will never be executed
    

    It is only a warning, so the program builds and runs. It just lacks the first line of output. This kind of bug is easy to miss because nothing crashes.

    Fixing it step by step

    1. Spot the unreachable line. Any statement after return in the same block cannot run.
    2. Move the print up. Put print("Hey bro!") above the return, so it runs while the function is still working.
    3. Keep return last. The returned string is the final thing the function does.

    The fixed code in Swift

    class Solution {
        func greetAndReturn() -> String {
            print("Hey bro!")
            return "How are you doing?"
        }
    
        func mainFunction() {
            let question = greetAndReturn()
            print(question)
        }
    }
    

    Now the order of events is:

    1. mainFunction calls greetAndReturn().
    2. Inside it, print("Hey bro!") writes the first line.
    3. return sends "How are you doing?" back, and question holds it.
    4. print(question) writes the second line.

    A few Swift details:

    • The return type is part of the header. -> String promises that every path ends by returning a String, and the function does that on its last line.
    • Two different prints. The first happens inside greetAndReturn, while the second prints a value that has already come back. Both write to the same console, in the order they run.
    • Methods call each other by name. Inside the class, mainFunction calls greetAndReturn() directly, with no prefix.

    Why not just reorder the other way?

    Swapping the two lines in mainFunction would not help, because the missing line is not in mainFunction at all. The print that never runs lives in greetAndReturn, and the only way to run it is to put it before the return. When a line seems to be ignored, first ask whether the program can reach it, and only then ask whether it is correct.

    Testing the fix

    Run mainFunction on a Solution object and compare the console with the expected two lines. If Hey bro! is missing, the print is still below the return. If the lines come in the wrong order, check which of the two prints happens inside greetAndReturn and which in mainFunction: the one inside the function always runs first, because the second needs the returned value.

    The same rule applies to every function you write: do the work first, and make return the last statement on each path.

    Common mistakes

    • Putting the work after return. The code compiles with a warning and silently skips it.
    • Deleting the return. A function with -> String that doesn't return a string fails to compile.
    • Printing the greeting from mainFunction instead. That can give the same output but changes code the task didn't ask you to change.
    • Ignoring warnings. "Will never be executed" means exactly that.
    Keep going with SwiftAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free