0Pricing
Learn Rust Coding · Lesson

Defining Functions

Parameters and returns.

Defining Functions is a free Learn Rust Coding lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Learn Rust Coding learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Declaring a Function

You define a function with the fn keyword, a name, parentheses, and a body in curly braces.

Rust uses snake_case for function names by convention.

fn main() {
    greet();
}

fn greet() {
    println!("Hello from a function!");
}

The main Function

Every executable Rust program starts at fn main(). It is the entry point the runtime calls first.

fn main() {
    println!("Program starts here");
}

Parameters

Functions can take parameters. Each parameter must declare its type after a colon.

fn main() {
    print_number(7);
}

fn print_number(x: i32) {
    println!("The number is {}", x);
}

Multiple Parameters

Separate multiple parameters with commas. Each needs its own type annotation.

fn main() {
    describe("Rust", 2010);
}

fn describe(name: &str, year: i32) {
    println!("{} was released in {}", name, year);
}

Return Values

Declare a return type after an arrow ->. The function gives back a value of that type.

The last expression (with no semicolon) becomes the return value.

fn main() {
    let result = square(5);
    println!("square is {}", result);
}

fn square(n: i32) -> i32 {
    n * n
}

The return Keyword

You can also return early with the return keyword. This is useful for guard conditions.

fn main() {
    println!("{}", abs_value(-8));
}

fn abs_value(n: i32) -> i32 {
    if n < 0 {
        return -n;
    }
    n
}

Combining Parameters and Returns

A function commonly takes inputs and produces an output. Here we add two numbers.

fn main() {
    let total = add(3, 4);
    println!("total {}", total);
}

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

Functions Calling Functions

Functions can call other functions, letting you build larger behavior from small reusable pieces.

fn main() {
    println!("{}", double_then_add(5));
}

fn double(n: i32) -> i32 { n * 2 }

fn double_then_add(n: i32) -> i32 {
    double(n) + 1
}

Returning Other Types

Functions can return any type: strings, booleans, tuples, and more.

fn main() {
    let (sum, product) = compute(3, 4);
    println!("sum {} product {}", sum, product);
}

fn compute(a: i32, b: i32) -> (i32, i32) {
    (a + b, a * b)
}

Functions With No Return

A function without an arrow returns the unit type (). It does its work through side effects like printing.

fn main() {
    log_message("saving file");
}

fn log_message(msg: &str) {
    println!("[LOG] {}", msg);
}

Order Does Not Matter

Unlike some languages, in Rust you can call a function defined later in the file. The compiler sees all functions before running.

fn main() {
    println!("{}", helper());
}

fn helper() -> i32 {
    99
}

Quick Check

Recall how Rust functions return values.

Recap

Functions in Rust:

  • Defined with fn name(params) -> ReturnType { ... }.
  • Parameters need explicit types.
  • The last expression (no semicolon) is the return value, or use return early.
  • No return type means it returns ().
fn main() {
    let area = rectangle_area(4, 6);
    println!("area {}", area);
}

fn rectangle_area(w: i32, h: i32) -> i32 {
    w * h
}

Frequently asked questions

Is the “Defining Functions” lesson free?

Yes — the full text of “Defining Functions” is free to read here on the web, and the Learn Rust Coding course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Learn Rust Coding course, upgrade to CoddyKit PRO.

What will I learn in “Defining Functions”?

Parameters and returns. You practise Learn Rust Coding with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Learn Rust Coding?

No prior experience is required. Learn Rust Coding on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Defining Functions” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Learn Rust Coding lesson?

Yes. Every Learn Rust Coding lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Defining Functions
  2. Expressions and Statements
  3. Closures
  4. Fn, FnMut, FnOnce
← Back to Learn Rust Coding