Parameters and Return Values
In this page:
fn name(param: Type) -> ReturnType {
// body
value
}
Function Parameters
Parameters let a function accept input values. Each parameter must be given an explicit type, since Rust never infers function signatures.
Example: Function Parameters
// Define the function `greet_person` taking `name`
fn greet_person(name: &str) {
// Print "Hello, {}!" to the console, substituting in `name` (with a trailing newline)
println!("Hello, {}!", name);
}
// Entry point: execution of the program starts here
fn main() {
greet_person("Ada");
}
Login to try C/C++/Java/PHP code in the editor
Returning a Value
A function's return type is declared after an arrow ->. The final expression in the function body, written without a trailing semicolon, becomes the returned value.
Example: Returning a Value
// Define the function `square` taking `n` and returning an integer
fn square(n: i32) -> i32 {
n * n
}
// Entry point: execution of the program starts here
fn main() {
// Print "Square of 6 is {}" to the console, substituting in `square(6)` (with a trailing newline)
println!("Square of 6 is {}", square(6));
}
Login to try C/C++/Java/PHP code in the editor
Multiple Parameters
Functions can accept several parameters, separated by commas, each with its own type. They are used inside the function body just like local variables.
Example: Multiple Parameters
// Define the function `add` taking `a`, `b` and returning an integer
fn add(a: i32, b: i32) -> i32 {
a + b
}
// Entry point: execution of the program starts here
fn main() {
// Print "3 + 4 = {}" to the console, substituting in `add(3, 4)` (with a trailing newline)
println!("3 + 4 = {}", add(3, 4));
}
Login to try C/C++/Java/PHP code in the editor
Early Return with the return Keyword
The return keyword immediately exits a function with a given value, which is useful for handling special cases early before the main logic of the function runs.
Example: Early Return with the return Keyword
// Define the function `absolute_value` taking `n` and returning an integer
fn absolute_value(n: i32) -> i32 {
// Check whether `n < 0`
if n < 0 {
// Return `-n` from this function
return -n;
}
n
}
// Entry point: execution of the program starts here
fn main() {
// Print "abs(-8) = {}" to the console, substituting in `absolute_value(-8)` (with a trailing newline)
println!("abs(-8) = {}", absolute_value(-8));
}
Login to try C/C++/Java/PHP code in the editor
- Adding a semicolon after the final expression in a function, which turns it from a returned value into a discarded statement.
- Forgetting to write
-> Typewhen a function is meant to return a value, causing a compile error at the return site. - Using the
returnkeyword everywhere out of habit when the idiomatic tail-expression style would be cleaner.
- Parameters are declared as
name: Typeinside the parentheses, and every parameter must have an explicit type. - A function's return type is written after
->, and the last expression (without a semicolon) is returned automatically. - The
returnkeyword can exit a function early with a value, useful inside conditionals. - A function with no return type implicitly returns the unit type
().
Chapter Quiz — Complete all 6 topics to unlock
0/6 topics done
Complete these topics first: