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
- Spot the unreachable line. Any statement after
returnin the same block cannot run. - Move the print up. Put
print("Hey bro!")above thereturn, so it runs while the function is still working. - Keep
returnlast. 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:
mainFunctioncallsgreetAndReturn().- Inside it,
print("Hey bro!")writes the first line. returnsends"How are you doing?"back, andquestionholds it.print(question)writes the second line.
A few Swift details:
- The return type is part of the header.
-> Stringpromises that every path ends by returning aString, 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,
mainFunctioncallsgreetAndReturn()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-> Stringthat doesn't return a string fails to compile. - Printing the greeting from
mainFunctioninstead. 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.
