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
- Leave the caller alone.
mainFunctionstores the result ingreetingand prints it, which is the right division of work. - Replace
printwithreturn. The function should hand the text back, not show it. - Keep the text the same.
"Hey, " + namejoins 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
-> Stringinstead. The function would then compile, butlet 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
printandreturn. 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," + namereturnsHey,Andy. - Returning a different type.
return 5in a function declared-> Stringis 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")returnsHey, Mike.getGreeting("")returnsHey,, 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
returnto give a value back andprintonly to display it. - A function that returns a value lets the caller decide what to do with it.
- A
-> Stringfunction whose only line isprint(...)fails with a()toStringconversion error.
