Parameters and return values
Quick answer
Parameters let a function receive different inputs. A return value lets its caller use the result in another expression instead of only printing it.
Function signatures
In fn total(a: i32, b: i32) -> i32, the parameters have explicit types and the arrow declares the result type. The final expression without a semicolon becomes the return value. Use return value; for an early exit. Without a result type the function returns unit ().
fn total(a: i32, b: i32) -> i32 { a + b }fn main() { println!("{}", total(7, 5)); }Expected stdout (not executed):
12The arguments 7 and 5 become the parameters and the expression produces 12.
fn nonnegative(n: i32) -> i32 { if n < 0 { return 0; } n}fn main() { println!("{} {}", nonnegative(-3), nonnegative(8)); }Expected stdout (not executed):
0 8Negative inputs leave the function early. Other inputs reach the final expression.
Common mistake
A trailing semicolon after the only result expression makes the block produce unit, causing a type mismatch for a declared i32 result.
Try it yourself
Write fn rectangle_area(width: i32, height: i32) -> i32 and test it with 4 and 6.
Static examples for Rust 1.85.0, edition 2024. Run them locally in a disposable project; ByteMind execution support and these outputs have not been verified.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Parameters and return values — practice quiz
Choose one answer per question, then review the explanation.
In `fn add(a: i32, b: i32) -> i32 { a + b }`, what does the arrow specify?
What is wrong with `fn value() -> i32 { 7; }`?
For `fn cap(n: i32) -> i32 { if n > 10 { return 10; } n }`, what is cap(4)?