A function is a named unit of executable code. It can receive inputs, perform work, and return a result. In Rust, `fn` declares a function, `()` shows its arguments, and `{}` contains its code block. section 1.6
Rust functions make several parts of a program explicit:
- An empty `()` means the function accepts no arguments; values inside the parentheses are its inputs. section 1.6
- A return arrow such as `-> i32` declares the result type. section 1.6
- A final expression without a semicolon becomes the return value; adding a semicolon produces the unit type `()` instead. section 1.6
- Parameters can be typed and used inside the body, allowing one function to operate on different supplied values. section 1.6
Returning a calculated value
fn multiply(number_one: i32, number_two: i32) -> i32 { number_one * number_two } fn main() { let multiply_result = multiply(8, 9); println!("The two numbers multiplied are: {}", multiply_result); } The function receives two `i32` values and returns their product. The last line has no semicolon, so it supplies the result. section 1.6
fn multiply(number_one: i32, number_two: i32) { #1
let result = number_one * number_two;
println!("{} times {} is {}", number_one, number_two, result);
}
fn main() {
multiply(8, 9); #2
let some_number = 10; #3
let some_other_number = 2;
multiply(some_number, some_other_number); #4
}
Browse a complete function call with typed parameters, local variables, and two sets of arguments. section 1.6
Compiler feedback helps locate return mistakes: if a function promises `i32` but ends with `8;`, Rust reports that it implicitly returns `()` and suggests removing the semicolon. A function may also use `return`, though code after it is unreachable. section 1.6
Small helper functions can improve readability by moving repeated or overly long work out of a main function. Keep important information visible, because chains of helpers can make code harder to follow. section 23.4.3



















