Functions in Rust can have parameters, which are special variables part of a function's signature. Parameters allow you to provide concrete values (arguments) when calling a function. The type of each parameter must be declared in the function signature.
Consider the following example:
Rust
fn main() {
another_function(5);
}
fn another_function(x: i32) {
println!("The value of x is: {x}");
}
In this example, another_function takes an i32 parameter named x. When called with the argument 5, the output will be:
Rust
The value of x is: 5
Multiple parameters are declared by separating them with commas:
Rust
fn main() {
print_labeled_measurement(5, 'h');
}
fn print_labeled_measurement(value: i32, unit_label: char) {
println!("The measurement is: {value}{unit_label}");
}
Here, print_labeled_measurement has two parameters, value of type i32 and unit_label of type char.