Day 3: Functions
EasyFunctionsExpressionsReturn

Day 3: Functions

Functions are the building blocks of Rust programs. Let's master them!

What You'll Learn

  • Function syntax and parameters
  • Return types with ->
  • Expressions vs statements
  • Implicit returns

Function Syntax

fn function_name(param1: Type1, param2: Type2) -> ReturnType {
    // function body
}

Expressions vs Statements

This is crucial! In Rust:

  • Expressions return a value (no semicolon)
  • Statements perform an action (with semicolon)
fn add(a: i32, b: i32) -> i32 {
    a + b    // expression - no semicolon, this is returned
}

fn add_with_return(a: i32, b: i32) -> i32 {
    return a + b;  // explicit return (less idiomatic)
}

The Task

Convert Celsius to Fahrenheit using the formula: $F = C \times \frac95 + 32$

Requirements

  • Take a f64 (floating point) parameter
  • Return the Fahrenheit value as f64
  • 100.0°C should return 212.0°F

Hints

  • Be careful with integer vs float division
  • 9.0 / 5.0 for float division
  • Don't add a semicolon to your result expression!
Language: Rust
1
2
3
4
5
6
7
8
9
10
11
12
13
Output
Run to see the result here.
    Day 3: Functions · RUST Challenge | learn.sol