What the code is supposed to do
The function welcomeUser takes a person's name and prints a welcome message for that person. mainFunction calls it for Andy and then for John, so the expected output is:
Welcome, Andy!
Welcome, John!
The code you start with compiles and runs, but prints the wrong thing:
Welcome, Andy!
Welcome, Andy!
What goes wrong
Here is the starting code:
class Solution {
def welcomeUser(name: String): Unit = {
println("Welcome, Andy!")
}
def mainFunction(): Unit = {
welcomeUser("Andy")
welcomeUser("John")
}
}
This is not a compile error but a logic error, which is harder to notice because the compiler stays silent. The function declares the parameter name and the second call passes "John" into it. But the body never reads name. It prints the fixed text "Welcome, Andy!", so the argument arrives and is ignored. Both calls run the same println, which is why both lines say Andy.
A parameter only does something if the body uses it. The text inside quotes is a literal: Scala prints it exactly as written, and it does not look at the other names in scope.
Fixing it step by step
- Compare the output with the expectation. The two lines are identical, so the message does not depend on the argument.
- Look at the line that prints. It contains the name
Andyinside the quotes. That is the part that should vary. - Split the string around the part that changes. The fixed pieces are
"Welcome, "before the name and"!"after it. - Put the parameter between the pieces and join them with
+. Note thatnameis written without quotes: with quotes it would be the word "name", not the value.
The fixed code in Scala
class Solution {
def welcomeUser(name: String): Unit = {
println("Welcome, " + name + "!")
}
def mainFunction(): Unit = {
welcomeUser("Andy")
welcomeUser("John")
}
}
Now the first call prints Welcome, Andy! and the second prints Welcome, John!.
Two notes on the Scala involved:
- String interpolation reaches the same result with less punctuation:
println(s"Welcome, $name!"). Thesbefore the opening quote tells Scala to replace$namewith the value of the parameter. - The
+operator on strings joins them. When one side is aString, the other side is converted to text too, so"Age: " + 30givesAge: 30.
How to spot this kind of bug
By default the compiler says nothing here, because a function is allowed to ignore its parameters. Reading is the only defence. When a function takes input, ask of every parameter: where does the body use it? If the answer is "nowhere", the function will give the same result for every call. Another quick test is to call the function with two different arguments and compare the results, as this program does.
Common mistakes
- Quoting the parameter.
"Welcome, name!"prints the wordnameevery time. - Forgetting the spaces and punctuation.
"Welcome," + namegivesWelcome,Andy, with no space. The comma, the space and the exclamation mark are all part of the expected output. - Changing the calls instead. The calls are fine. Editing them cannot repair a function that ignores its input.
- Forgetting the
sprefix when using interpolation.println("Welcome, $name!")printsWelcome, $name!literally.
Checking your fix
Run the code in the editor above. A correct fix prints two lines, one for each name, and the second line mentions John.
