Problem overview
The starting code prints three messages, and then prints the same three messages two more times. It has nine println statements in one function:
def mainFunction(): Unit = {
println("Hello AlgoCademy!")
println("Coding is amazing!")
println("Functions are so useful!")
println("Hello AlgoCademy!")
println("Coding is amazing!")
println("Functions are so useful!")
println("Hello AlgoCademy!")
println("Coding is amazing!")
println("Functions are so useful!")
}
The task is to produce the same output in a better way: create a function named printMessages that prints the three messages, and call it three times.
Approach: extract the repeated block
The nine lines are one block of three lines, copied three times. When code repeats like this, move the block into a function, then replace every copy with a call. The output stays the same, but the three messages are written only once.
That gives the program two parts:
- a function
printMessagesthat holds the threeprintlnstatements; mainFunction, which callsprintMessagesonce per repetition.
Step by step
- Define
printMessagesabovemainFunction. Usedef, the name, empty parentheses and the result typeUnit, because the function prints and returns nothing useful. Finish the line with= {. - Put one copy of the three messages in the body. They go inside the braces, in the same order as before, with the exact text, including the capital letters and exclamation marks.
- Close the braces. The function is now defined, but defining it prints nothing.
- Call it three times in
mainFunction. EachprintMessages()runs the threeprintlnstatements once. Three calls produce the original nine lines, with noprintlnleft inmainFunction.
The solution in Scala
// Define & implement the function printMessages here:
def printMessages(): Unit = {
println("Hello AlgoCademy!")
println("Coding is amazing!")
println("Functions are so useful!")
}
def mainFunction(): Unit = {
// Call printMessages 3 times here:
printMessages()
printMessages()
printMessages()
}
Running mainFunction() prints:
Hello AlgoCademy!
Coding is amazing!
Functions are so useful!
Hello AlgoCademy!
Coding is amazing!
Functions are so useful!
Hello AlgoCademy!
Coding is amazing!
Functions are so useful!
The Scala details worth knowing:
- Parentheses on the call. In Scala 3, a method defined with
()must be called with(). WritingprintMessagesalone is rejected withmethod printMessages must be called with () argument. - Unit. It marks a function that returns no useful value. The last expression of the body is a
println, whose value is(), which matches. - Order of definitions. The function is defined above
mainFunction, which is the clear order for readers. - The comments. Lines starting with
//are ignored by the compiler. They are only there to mark where each part goes.
Why this is better
Compare the two versions when the requirements change:
- If the messages must be printed five times, the new version needs two more calls. The original needs six more lines.
- If one message needs different wording, the new version has one place to edit. The original has three copies of it.
The three identical calls are still repetition. A loop would remove it, and later lessons cover loops. The function you write here stays useful either way.
Edge cases and common mistakes
- Defining the function but calling it fewer than three times. The output would have too few lines. Count the calls.
- Keeping the original
printlnlines inmainFunction. They would print in addition to the calls, so the output would have too many lines. - Misspelling the name. The task names
printMessages, with a capital M. Scala names are case-sensitive. - Changing the text. The tests compare the exact output, so every character of the messages counts.
- Leaving out the parentheses in the calls. That doesn't compile.
Testing
Run mainFunction() and check that the output has 9 lines, made of the three messages repeated three times in order. If you call printMessages() once, you should see exactly the first three lines.
