Problem overview
The class Solution has a function getGreeting that should build a greeting for a name and give it back. mainFunction calls it with "Andy", stores the result in greeting and prints it. The expected output is one line:
Hey, Andy
The starting code doesn't compile. The compiler reports missing return statement, so nothing is printed.
class Solution {
fun getGreeting(name: String): String {
println("Hey, " + name)
}
// Do not change the code below:
fun mainFunction() {
val greeting = getGreeting("Andy")
println(greeting)
}
}
What goes wrong
The header fun getGreeting(name: String): String promises that each call gives back a String. The body doesn't keep that promise. It builds the text "Hey, " + name and passes it to println, which writes it on the console. Then the function reaches its closing brace without returning anything.
Printing and returning are different. Printing shows a value to a person reading the console, and the program can't use it afterwards. Returning hands the value to the code that called the function. The caller wrote val greeting = getGreeting("Andy") and expects a string to land in greeting. With no return, the compiler has no value to put there, and it refuses to build the program:
error: missing return statement.
}
^
The fix
Move the work from printing to returning:
- In
getGreeting, replaceprintln(...)withreturn, keeping the same expression"Hey, " + name. - Leave
mainFunctionas it is. It already prints the returned value.
The result is that the greeting is printed exactly once, by the caller. Had you kept the println and added a return as well, the greeting would appear twice, once from inside the function and once from the caller.
The solution in Kotlin
class Solution {
fun getGreeting(name: String): String {
return "Hey, " + name
}
// Do not change the code below:
fun mainFunction() {
val greeting = getGreeting("Andy")
println(greeting)
}
}
Some details about this code:
- The
+operator joins strings."Hey, " + namegivesHey, Andywhennameis"Andy". The space after the comma is inside the first string, because+adds nothing of its own. - The type of
greetingis inferred. The caller doesn't write: String, because Kotlin sees thatgetGreetingreturns aString. - A string template is another way to write it.
return "Hey, $name"returns the same text. - An expression body also works.
fun getGreeting(name: String): String = "Hey, " + namereturns the expression without the wordreturn.
Common mistakes
- Keeping the
printlnand adding areturn. The code compiles, but the output has the greeting twice. - Removing the return type. Without
: String, the function returnsUnit. Then the lineval greeting = getGreeting("Andy")compiles andprintln(greeting)printskotlin.Unit. - Editing the caller. The caller is fine, and the lesson marks it as code not to change.
- Changing the text. The output must be exactly
Hey, Andy: capital H, a comma, a single space and no exclamation mark.
Why returning is the better design
A function that returns its result can be used in more places than one that prints it. The caller in this lesson prints the greeting, but another caller could compare it with a string, join it with other text or store it in a list. A function that prints decides on the caller's behalf that the text goes to the console, and nobody else can use that text.
A good rule when you write a function: if its job is to compute or build a value, return it, and leave printing to the code that needs to show it. If its job is to show something, a function that prints and returns nothing is the right shape, and then no return type is needed.
The compiler also helps here. It checks that the header and the body agree, so a function that promises a String cannot silently end without one. The error appears at compile time, and it names the closing brace where the function ends too early.
Testing
After the fix, getGreeting("Andy") returns Hey, Andy and the program prints that line once. Calling getGreeting("Mia") returns Hey, Mia, because the name comes from the parameter.
