The problem: repeated code
Suppose a teacher tells a student named Andy to write this text on the whiteboard three times:
This is Andy
Andy made a mistake
Andy suffers the consequences
Without functions, the program repeats the three lines three times:
println!("This is Andy");
println!("Andy made a mistake");
println!("Andy suffers the consequences");
println!("This is Andy");
println!("Andy made a mistake");
println!("Andy suffers the consequences");
println!("This is Andy");
println!("Andy made a mistake");
println!("Andy suffers the consequences");
Nine lines are manageable. But if the text were longer, or the teacher asked for 10 or 100 copies, the program would grow with every copy while doing the same few things over and over.
The fix: write it once, call it many times
A function holds the lines once. You then call it as often as you like:
fn print_message() {
println!("This is Andy");
println!("Andy made a mistake");
println!("Andy suffers the consequences");
}
print_message();
print_message();
print_message();
This prints the same three lines three times. A fourth copy is one more line, print_message();, instead of three more println! lines.
Change it in one place
Functions also make changes cheap. To change the student's name to Mike, edit the body of print_message once. Every call picks up the change:
fn print_message() {
println!("This is Mike");
println!("Mike made a mistake");
println!("Mike suffers the consequences");
}
In the repeated version, the same change means editing every copy, and missing one leaves an inconsistent program.
What functions give you
- Less repetition. The instructions are written once.
- One place to fix. A mistake or a change is fixed in the function body and applies to every call.
- Readable code. A name such as
introduce_yourselfsays what the code does, so a reader does not have to study its lines. - Smaller pieces. A big program splits into small functions that are easier to write, test and understand one at a time.
The five identical calls print_message(); in a row still look repetitive. Loops, which come later, remove that repetition too.
The exercise, step by step
The exercise gives a program skeleton. It has a place for a function and a function whose name is mainFunction, which already prints Sorry, I didn't hear you between two comments that say where the introductions go. The goal is to write the introduction once and use it twice.
- Define
introduce_yourself. Below the comment that marks where the function goes, writefn introduce_yourself()with empty parentheses and braces. No return type is written, so it returns nothing. The name is in snake_case. - Print the first line. Inside the body,
println!("My name is Andy");. - Print the second line. On the next line of the body,
println!("I love coding");. - Call it the first time. In
mainFunction, below the first comment, writeintroduce_yourself();. Both lines print. - Call it again. Below the last comment, after the
"Sorry, I didn't hear you"line, writeintroduce_yourself();once more. Both lines print again, without repeating their code.
The solution in Rust
// Function goes here:
fn introduce_yourself() {
println!("My name is Andy");
println!("I love coding");
}
fn mainFunction() {
// Introduce yourself the first time:
introduce_yourself();
println!("Sorry, I didn't hear you");
// Introduce yourself again:
introduce_yourself();
}
The program prints:
My name is Andy
I love coding
Sorry, I didn't hear you
My name is Andy
I love coding
Rust details:
- Two functions, two roles.
introduce_yourselfholds the reusable lines.mainFunctiondecides when they run and what comes between them. - The name
mainFunction. It is camelCase, but Rust style prefers snake_case. The compiler accepts it and only warns that it should have a snake case name. Keep the name as it is in the skeleton. - Calls are statements.
introduce_yourself();needs the parentheses and the semicolon. - Comments are ignored. The
//lines only mark where the code goes.
Common mistakes
- Defining the function but not calling it twice. The two call lines are what produce the two introductions.
- Calling it in the wrong place. The second call goes after
"Sorry, I didn't hear you", so the output order matches the story. - Putting both prints in
mainFunction. That repeats the code the function was meant to hold. - Misspelling the text. Output must match exactly, including capital letters.
Key takeaways
- A function lets you write instructions once and run them many times.
- Changing the function body changes every call.
- Good function names make code read like a description of what it does.
