Exerpad
🔥1
10 XP
Lv.1 Beginner
Log In

Defining Functions

So far, all your code has lived inside fn main(). But what if you want to reuse a piece of code without copying and pasting it everywhere? That's where functions come in!

A function is like a mini-program inside your program. You give it a name, and then you can call it whenever you need it.

Your First Function

Here's how you define a simple function in Rust:

rust
fn say_hello() {
println!("Hello there!");
}

fn main() {
say_hello();
say_hello();
say_hello();
}

Let's break it down:

  • fn is the keyword that tells Rust "I'm making a function!"
  • say_hello is the name we chose for the function
  • () means the function doesn't need any extra information to do its job
  • The code inside {} is what runs when you call the function

We call the function by writing its name followed by (). In the example above, we called it 3 times — that's 3 hellos without writing println! three times!

Functions with Parameters

What if you want your function to work with different values each time? You can give it parameters — these are like little slots where you pass in information:

rust
fn greet(name: &str) {
println!("Hello, {}!", name);
}

fn main() {
greet("Alice");
greet("Bob");
greet("Charlie");
}

The name: &str part says: "This function needs a text value, and we'll call it name inside the function." The &str type is Rust's way of saying "a piece of text that we're borrowing."

You can have multiple parameters too — just separate them with commas:

rust
fn introduce(name: &str, age: i32) {
println!("{} is {} years old.", name, age);
}

fn main() {
introduce("Luna", 10);
introduce("Max", 12);
}

Each parameter needs its own type. Rust wants to know exactly what kind of data to expect!

Functions That Return Values

Some functions don't just do something — they give back a result. We use -> followed by the return type to say what kind of value the function returns:

rust
fn add(a: i32, b: i32) -> i32 {
a + b
}

fn main() {
let result = add(3, 5);
println!("3 + 5 = {}", result);
println!("10 + 20 = {}", add(10, 20));
}

Notice something interesting: we wrote a + b without a semicolon at the end. In Rust, the last expression in a function (without a semicolon) is automatically the return value. This is called an "implicit return."

You can also use the return keyword if you prefer to be explicit:

rust
fn multiply(a: i32, b: i32) -> i32 {
return a * b;
}

fn main() {
println!("4 x 7 = {}", multiply(4, 7));
}

Both styles work, but most Rust programmers prefer leaving off the semicolon for the last expression.

Functions That Return bool

Functions can return any type, including booleans! This is great for checking conditions:

rust
fn is_positive(n: i32) -> bool {
n > 0
}

fn main() {
println!("Is 5 positive? {}", is_positive(5));
println!("Is -3 positive? {}", is_positive(-3));
println!("Is 0 positive? {}", is_positive(0));
}

Summary

ConceptExample
Define a functionfn greet() { ... }
Call a functiongreet();
Parameterfn greet(name: &str)
Multiple parametersfn add(a: i32, b: i32)
Return typefn add(a: i32, b: i32) -> i32
Implicit returna + b (no semicolon)
Explicit returnreturn a + b;

Now let's practice writing your own functions!