What the code is supposed to do
The program should print two lines, in this order:
Hey bro!
How are you doing?
The first line is meant to be printed from inside greetAndReturn. The second is the string that greetAndReturn returns, which mainFunction stores in question and prints.
Here is the starting code:
def greetAndReturn(): String = {
return "How are you doing?"
println("Hey bro!")
}
def mainFunction(): Unit = {
val question = greetAndReturn()
println(question)
}
It doesn't compile. The compiler reports a type mismatch: Found: Unit, Required: String.
What goes wrong
Two rules explain the error and the missing output.
- A block's value is its last expression. The body of
greetAndReturnends withprintln("Hey bro!"). A call toprintlnhas typeUnit, but the function promises aString. That is the mismatch the compiler reports. Thereturnon the line above doesn't change what the compiler thinks of the last line. returnleaves the function immediately. Even if the code compiled, theprintlnsits below thereturn, so the function would already be finished when it would run. "Hey bro!" could never be printed.
The line is correct. Its position is the bug.
Fixing it step by step
- Move the
printlnabove thereturn. Statements run top to bottom, so printing first guarantees the greeting appears before the function exits. - End the function with the
return. The last line of the body is now the string, of typeString, which matches the declared result type. The type error disappears. - Leave
mainFunctionalone. It already stores the returned string and prints it. That line comes second in the output, after the greeting printed insidegreetAndReturn.
The fixed code in Scala
def greetAndReturn(): String = {
println("Hey bro!")
return "How are you doing?"
}
def mainFunction(): Unit = {
val question = greetAndReturn()
println(question)
}
When mainFunction() runs, the call to greetAndReturn() prints Hey bro! first, then returns the question. The println(question) line then prints How are you doing?.
Scala details worth knowing:
returnis optional. The last expression of a block is its value, so the function could end with just"How are you doing?". Both forms behave the same here.- Order of effects. The greeting prints while
greetAndReturnis running, beforeval questionreceives its value. The returned string is printed later, bymainFunction. - Explicit result types. Because
: Stringis written on the function, the compiler can compare the body against it. That is how the mistake was caught.
Tracing the output
Following the fixed program line by line shows why the order is right:
mainFunctionstarts and evaluates the right side ofval question = greetAndReturn(), which callsgreetAndReturn.- Inside,
println("Hey bro!")runs, so the first output line appears. - The
returnsends"How are you doing?"back, andquestionis bound to it. Nothing has been printed for it yet. println(question)prints the second line.
If the two statements in greetAndReturn were swapped back, the order of the output would not be the only problem: the program would not even compile.
Common mistakes
- Deleting the
printlnline. The compile error goes away, but the output losesHey bro!. - Changing the return type to
Unit. That compiles, with a warning about the discarded string, butquestionthen holds(), the only value ofUnit, and the program prints()instead ofHow are you doing?. - Printing the greeting in
mainFunctioninstead. The lesson wants it insidegreetAndReturn, and both lines must come out in the right order. - Putting any code after the
return. It never runs, and the last expression of the block must stay the returned value.
