Sub topic 1
Defining and calling functions
Prerequisites
This chapter assumes you can build and run a program with cargo. It also assumes you know Rust's primitive types (integers, booleans, strings) and how let and let mut bind variables. Nothing else is required.
The shape of a function
A function in Rust starts with the fn keyword, followed by a name, a parameter list in parentheses, an optional return type after ->, and a body in braces. Names use snake_case by convention.
fn add(a: i32, b: i32) -> i32 {
a + b
}
fn main() {
let sum = add(2, 3);
println!("{sum}");
}Running this prints 5. Three details matter here:
- Every parameter needs an explicit type. The compiler does not infer types in function signatures.
- The return type follows
->. If you leave it out, the function returns the unit type(), which means "no useful value". - The last expression in the body,
a + b, has no semicolon. It becomes the function's return value.
Calling a function
To call a function, write its name and pass arguments in parentheses. Arguments are matched to parameters by position, and each argument must have the parameter's type. Rust has no named arguments and no default values.
The order of definitions in a file does not matter. main can call add even if add appears below it, because the compiler reads the whole module before checking calls.
Every executable program starts at main. It takes no parameters and usually returns ().
Execution flow of a call
When main calls add, control moves into add. The arguments are bound to the parameters, the body runs, and the final expression's value comes back to the caller.
Common errors
Most first mistakes with function signatures produce clear compiler messages. These are the ones you will meet most often.
| Mistake | Example | What happens | Column |
|---|---|---|---|
| Missing parameter type | `fn add(a, b) -> i32` | Compile error: expected one of `:`, `@`, or ` | ` |
| Wrong argument type | `add(2.0, 3.0)` | Mismatched types error, expected `i32` and found floating-point number | |
| Wrong argument count | `add(2)` | Error: this function takes 2 arguments but 1 argument was supplied | |
| Semicolon after the final expression | `a + b;` | Mismatched types error, because the body now evaluates to `()` |
The last mistake is the most common. Adding a semicolon turns the final expression into a statement, so the function body produces () while the signature promises i32. The compiler points at the semicolon and suggests removing it. The next chapter covers statements and expressions in detail.
fn add(a: i32, b: i32) -> i32 {
a + b // no semicolon: this value is returned
}
fn main() {
println!("{}", add(40, 2));
}A function with no return value
A function that only performs an action, such as printing, omits the -> part.
fn greet(name: &str) {
println!("Hello, {name}!");
}
fn main() {
greet("Ferris");
}Here greet returns (). Callers rarely use that value, but it is a real value of a real type, which keeps Rust's rules uniform.
Exercises
Exercise
Write a function square that takes an i32 and returns its square. Then explain why adding a semicolon after the final expression of its body causes a compile error.
Show answer
fn square(x: i32) -> i32 { x x }. With a semicolon, `x x; is a statement, so the body evaluates to (). The signature promises i32`, so the compiler reports mismatched types.
Exercise
Is it valid to call a function in main before the line where that function is defined in the file? Why?
Show answer
Yes. Rust resolves all items in a module before checking function bodies, so the order of function definitions does not matter.