Step 2 of 6
Parameters
A function becomes much more useful when you can hand it values to work with. These inputs are parameters, declared inside the parentheses, each with a name and a type:
fn main() {
greet("Ferris");
greet("Corro");
show_score("Ferris", 120);
}
fn greet(name: &str) {
println!("Hello, {name}!");
}
fn show_score(player: &str, points: i32) {
println!("{player}: {points} points");
}
Hello, Ferris!
Hello, Corro!
Ferris: 120 points
name: &strdeclares a parameter callednameof type&str(text, like a string literal).- The values you pass in a call, like
"Ferris", are the arguments. Each argument becomes the value of its parameter for that one call:nameis"Ferris"in the first call and"Corro"in the second. - Several parameters are separated by commas, and the arguments are matched to them in order.
- Parameters are variables that belong to the function: immutable by default, and gone when the function ends.
Parameter types are required
Inside a function, Rust infers the types of variables. A function's parameters, though, always need written types. The first line of a function, its signature, is a contract: callers can see exactly what to pass without reading the body, and the compiler checks every call against it. When a call is wrong, the error points at that call, not somewhere deep inside the function.
fn greet(name) {
println!("Hello, {name}!");
}
fn main() {
greet("Ferris");
}
error: expected one of `:`, `@`, or `|`, found `)`
--> src/main.rs:1:14
|
1 | fn greet(name) {
| ^ expected one of `:`, `@`, or `|`
|
help: if this is a parameter name, give it a type
|
1 | fn greet(name: TypeName) {
| ++++++++++
Common mistakes
The number of arguments must match the number of parameters:
fn show_score(player: &str, points: i32) {
println!("{player}: {points} points");
}
fn main() {
show_score("Ferris");
}
error[E0061]: this function takes 2 arguments but 1 argument was supplied
--> src/main.rs:6:5
|
6 | show_score("Ferris");
| ^^^^^^^^^^---------- argument #2 of type `i32` is missing
- Wrong type:
show_score("Ferris", "120")iserror[E0308]: mismatched types(expectedi32, found&str). Quotes make"120"text, not a number. - Swapped arguments:
show_score(120, "Ferris")doesn't compile either, because the types don't line up. But when two parameters have the same type, likefn rectangle(width: u32, height: u32), swapping them compiles and quietly gives wrong results. Clear parameter names are your defense.
Your task
Write a function print_receipt(item: &str, price: u32, quantity: u32) that prints one line in the form 3 x apple = 150: the quantity, the item, and the total price. main already calls it twice; the program should print:
3 x apple = 150
2 x melon = 640
Challenge 2
Fill in the parameter types so the function compiles and the program prints Level 3: Lava Cave.
Challenge 3
Write a function print_area(width: u32, height: u32) that prints a line like 3 x 4 = 12 square meters. Call it from main for a 3 by 4 room and then a 5 by 2 balcony:
3 x 4 = 12 square meters
5 x 2 = 10 square meters
Previous: Defining and calling functions Next: Returning a value