Problem overview
The starting code prints the same three lines three times: Hello AlgoCademy!, Coding is amazing! and Functions are so useful!. That is nine println! calls, with three copies of the same block. The task is to write the block once, as a function called print_messages, and call it three times.
The output stays exactly the same. Only the way the program is written changes.
Why a function helps
Repeated code is a maintenance problem. If one message changes, you must find and edit every copy, and it is easy to miss one. A function gives the block a name. The three lines live in one place, and each call runs them again.
This function needs no input, so it has no parameters. It also gives nothing back, so it has no -> return type. In Rust, a function without a return type returns (), the empty value called the unit type.
Step by step
- Define the function. Write
fn print_messages()with empty parentheses, then a body in curly braces. Rust names functions in snake_case, so it isprint_messages, even though the comment in the starter code saysprintMessages. - Move the three
println!lines into the body. They stay in the same order, each ending with a semicolon. - Call it three times. Inside
mainFunction, under the comment, writeprint_messages();on three lines. The parentheses are required: the name alone refers to the function without running it.
The solution in Rust
// Define and implement the function printMessages here:
fn print_messages() {
println!("Hello AlgoCademy!");
println!("Coding is amazing!");
println!("Functions are so useful!");
}
fn mainFunction() {
// Call printMessages 3 times here:
print_messages();
print_messages();
print_messages();
}
Calling 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!
A few Rust details are worth knowing here:
- Order of definition does not matter. Rust lets a function call another one defined later in the file. Here
print_messagescomes first only because that is the clearer reading order. println!is a macro. The!marks a macro call. It prints its text and then a line break.- The name
mainFunctionis not snake_case. The compiler only warns about it, and the program still builds. Your own functions should use snake_case.
Reading the result
The function is a small unit you can reason about on its own. Each call to print_messages() produces the same three lines, and the three calls in mainFunction run one after another. Because the function has no parameters, every call behaves identically. Making the output differ between calls is the job of parameters, which come next in the chapter.
It also changes how you edit the program. If a message needs a different wording, you change one line inside print_messages and all three calls follow. If you need a fourth repetition, you add one more call line instead of three more println! lines.
Common mistakes
- Defining the function but not calling it. A definition only describes what to do. Without the three calls, nothing prints.
- Leaving out the parentheses in the call. Writing
print_messages;as a statement does not run the function. The compiler warns that it has no effect. - Forgetting the semicolon after a call. Each call on its own line is a statement and ends with
;. - Using a different name. Rust is case-sensitive, so
printMessagesandprint_messagesare two different names. Use the one the task gives,print_messages. - Copying a
println!intomainFunctionas well. That prints the line twice. Each message should appear in the program only once, inside the function.
