Problem overview
The starting program prints the same three lines three times by writing out nine fmt.Println calls. The task is to say the same thing in a better way: define a function printMessages that prints the three lines once, and call it three times from mainFunction.
The output must be exactly:
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!
Approach: find the repeated block
Read the original code and look for a group of statements that appears more than once. Here the same three lines repeat back to back. That group is the body of the new function, and each repetition becomes a single call.
The result is shorter, and it changes in one place: edit one message in printMessages and all three repetitions follow.
Step by step
- Define
printMessagesabovemainFunction. It takes no parameters, so the parentheses are empty. It returns nothing, so no return type follows them. - Put one copy of the three prints in its body. Keep the order and the text exactly as the task shows.
- Call it from
mainFunction. Write the name followed by parentheses, once for each repetition. - Count the calls. Three calls mean three runs of the body, so nine lines of output.
The solution in Go
// Define & implement the function printMessages here:
func printMessages() {
fmt.Println("Hello AlgoCademy!")
fmt.Println("Coding is amazing!")
fmt.Println("Functions are so useful!")
}
func mainFunction() {
// Call printMessages 3 times here:
printMessages()
printMessages()
printMessages()
}
Notes on the Go involved:
- No return type. A function that only prints needs nothing after its parentheses.
- The parentheses make the call. A line that holds just
printMessagesis rejected by the compiler as a value that is not used. It does not run the function. - Each
Printlnends with a newline, so no\nis needed inside the strings. - Order of the definitions. Go would accept
printMessagesbelowmainFunctionas well. Writing helpers above the code that uses them is a readability habit. - A loop is another tool. Later you will repeat a call with
forinstead of writing it three times. This lesson asks for three separate calls.
Edge cases and common mistakes
- Two calls or four. The expected output has nine lines, which is exactly three runs of the body.
- A different order inside the body. The three lines must appear in the order shown, since each call repeats them in that order.
- A typo in a message. The capital letters and the exclamation marks are part of the expected output.
Hello Algocademy!is a different string. - Keeping some of the old
Printlnlines inmainFunction. They would add extra output. The nine original lines are replaced by the three calls. - Misspelling the function name. Calling
printMessage()gives anundefinederror, because Go looks for exactly the name you wrote.
Why this is better than nine lines
Count the cost of a change. Say the second message must become Coding is fun!. In the original program you would edit three separate lines and could easily miss one. With printMessages you edit one line, and every call prints the new text. The same goes for adding a message or fixing a typo.
The function also gives the repeated block a name that describes it. A reader of mainFunction sees three calls to printMessages and understands the program at a glance, without comparing nine lines to see that they come in three identical groups.
Testing
Run the code and compare the output with the nine lines at the top. If you change the function to print a fourth line, all three blocks of output should grow by that line. That confirms the change lives in one place.