A function names a calculation. The final expression with no semicolon is the return value.

Program

Play the program to send a subtotal through a function and back.

functions.rs
Replay: real traced execution (multi-file project)
fn main() {
    let subtotal = 25;
    let total = add_tax(subtotal);
    println!("{total}");
}

fn add_tax(price: i32) -> i32 {
    price + price / 10
}
  1. subtotal ← 25

    1fn main() {2    let subtota→ 25l = 25;3    let total = add_tax(subtota25l);4    println!("{total}");
  2. fn add_tax(price: i32) -> i32

    7fn add_tax(price: i32) -> i32 {8    pric25e + price / 109}
  3. total ← 27

    2    let subtotal = 25;3    let tota→ 27l = add_tax(subtota25l);4    println!("{total}");5}
    output27
fn `fn add_tax(price: i32) -> i32` declares inputs and output.
argument `add_tax(subtotal)` passes the current subtotal.
implicit return The last expression (no `;`) is returned.