What the code is supposed to do
The function getGreeting should build a greeting for name and give it back to the caller. mainFunction calls it with "Andy", stores the result in greeting and prints that variable. The program should print Hey, Andy once.
This is the starting code:
func getGreeting(name string) string {
fmt.Println("Hey, " + name)
}
// Do not change the code below:
func mainFunction() {
greeting := getGreeting("Andy")
fmt.Println(greeting)
}
The compiler rejects it with missing return.
What goes wrong
The header ends with string, which promises that getGreeting gives a string back. The body doesn't keep that promise. It builds "Hey, " + name and passes it to fmt.Println, which shows it on the screen. Showing a value and returning it are two different things. After the print, the function reaches its closing brace with nothing returned.
A Go function with a result type must return on every path through its body. Here no path does, so the program is rejected before it can run.
Imagine that the code did compile. The greeting would be printed inside the function, and then mainFunction would print the returned value as well. The caller already does the printing, so the function has to hand the text over instead.
Fixing it step by step
- Keep the header. One
stringparameter and astringresult are what the caller expects. - Replace the print with
return. The expression"Hey, " + namestays the same. Only the call around it changes. - Leave the caller alone. It stores the returned value in
greetingand prints it, and that is the only printing the program needs.
The fixed code in Go
func getGreeting(name string) string {
return "Hey, " + name
}
// Do not change the code below:
func mainFunction() {
greeting := getGreeting("Andy")
fmt.Println(greeting)
}
Details worth knowing:
- The space after the comma is part of the string.
"Hey, "ends with a space, because+inserts nothing between the pieces. Without it the output would beHey,Andy. - No
fmtneeded in the function now. The package is still used bymainFunction, so the import stays valid. - Returning keeps the function reusable. A function that returns text can feed a print, a file, a longer message or a test. A function that only prints can do just one thing.
- Printing and returning together. If you wanted both, you would print and then also write
return. The program would then show the greeting twice, which this task does not want.
Common mistakes
- Adding
returnbut keeping the print. The code compiles, butHey, Andyappears twice, once fromgetGreetingand once frommainFunction. - Removing the result type. Without
stringin the header, the linegreeting := getGreeting("Andy")in the unchangeable code fails to compile. - Returning a different value.
return nameprintsAndy, notHey, Andy. - Changing the caller. The task marks it as unchangeable, and it was never the problem.
Reading the error
The message missing return points at the closing brace of getGreeting. That position is a clue: the compiler walked through the whole body, reached the end, and found that the function can finish without handing back a string. Whenever you see this message, look for a path through the function that ends without a return, such as a body that only prints, or an if branch that returns while the rest of the function does not.
Checking your fix
Run the code in the editor above. A correct fix compiles and prints exactly one line, Hey, Andy. If you see the line twice, a fmt.Println is still left inside getGreeting.
