OpenViSTAI · Agents · Blockchain · Crypto
Menu

OPENVIST LEARN · FREE LESSONS

Functions and return values

Give a function typed parameters and return an expression.

Lesson 6 of 30 · About 20 minutes · Beginner · English

By the end of this lesson

Give a function typed parameters and return an expression.

Before you start

  • Rust 1.90.0 or later, a terminal and a plain-text editor. Examples were tested with Rust 1.90.0, Edition 2024 on Linux.
  • Complete lesson 5, or be comfortable explaining and running its examples.

Three words to know

Parameter
A named input in a function definition.
Return type
The type declared after ->.
Expression
Code that evaluates to a value.

1. Understand the idea

A function groups one operation behind a name. Parameters and return types make its contract visible. Rust blocks can evaluate to a value: a final expression without a semicolon becomes the block result. Adding a semicolon turns it into a statement and usually makes the block evaluate to (), the unit value. Keep small calculations separate from printing so they are easy to test.

2. Save, run and observe

Save this complete program as lesson.rs in a fresh practice folder. It replaces that lesson file only; keep earlier examples separately. Compile it with rustc, then run the executable. Rust source goes in the editor; rustc and cargo commands go in the terminal.

A reusable totalRUST · save as lesson.rs
fn total(open: u32, closed: u32) -> u32 {
    open + closed
}
fn main() {
    println!("total={}", total(2, 1));
}
What you should see · example output
total=3
Compile and run on macOS/LinuxTERMINAL · Bash or Windows PowerShell
rustc --edition=2024 lesson.rs -o lesson
./lesson

3. Explain the result

The call supplies two u32 values. The final expression returns their sum. This beginner calculation assumes small inputs; it does not replace the checked arithmetic policy from lesson 4 for arbitrary values. Printing belongs in main rather than inside total.

YOUR TURN

A small check before you continue

What changes if you put a semicolon after open + closed?

Show a suggested answer

The function body no longer returns the u32 expression; the declared return type conflicts with (). Remove the semicolon or use return explicitly.

Sources and scope

Documentation and examples reviewed 2026-10-18. Provider screens, package versions and output wording can differ. Examples show expected outcomes; they are not a claim that OpenViST connected to your server or verified your setup.