Start learning free

LESSON 38 OF 414 · FUNCTIONS I

Fixing an Ignored Parameter in a Scala Function

Practice Read the full explanation

Inside the code editor we've tried to write a function that takes a person's name as argument and prints a tailored welcome message for that person.

Then, we welcomed Andy and John by calling the function for each of them.

So when we ran the code, we expected it to print:

Welcome, Andy!
Welcome, John!

but it seems like we made some mistakes because when we run our code, it prints:

Welcome, Andy!
Welcome, Andy!

Assignment:

Your task is to fix our code such that no errors will be produced and it will print the desired output.

STEP 1 OF 1
    Ctrl + Enter to check

    SCALA · FUNCTIONS I

    Buggy Code 5 in Scala, explained

    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

    1. Compare the output with the expectation. The two lines are identical, so the message does not depend on the argument.
    2. Look at the line that prints. It contains the name Andy inside the quotes. That is the part that should vary.
    3. Split the string around the part that changes. The fixed pieces are "Welcome, " before the name and "!" after it.
    4. Put the parameter between the pieces and join them with +. Note that name is 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!"). The s before the opening quote tells Scala to replace $name with the value of the parameter.
    • The + operator on strings joins them. When one side is a String, the other side is converted to text too, so "Age: " + 30 gives Age: 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 word name every time.
    • Forgetting the spaces and punctuation. "Welcome," + name gives Welcome,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 s prefix when using interpolation. println("Welcome, $name!") prints Welcome, $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.

    Keep going with ScalaAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free