All lessons

Lesson 4 of 11

Functions

Pass an argument into a function and trace the value it returns.

main.rs

fn main() {    let answer = double(4);    println!("{answer}");}fn double(n: i32) -> i32 {    n * 2}

Program state

Program
Ready
Step 1 of 6

The program is ready to call a function.

What’s happening

A function lets you name and reuse a piece of code. The parameter n: i32 tells callers what kind of value to pass. The arrow and type, -> i32, tell callers what they get back.

In double, n * 2 is the final expression. With no semicolon after it, its value becomes the function’s return value.

Read more in The Rust Book