beginnerFunctions
Functions Basic
Define and call functions with parameters and return values.
Functions Basic
Define and call functions with parameters and return values.
Difficulty
Beginner
Code
rust
fn add(a: i32, b: i32) -> i32 {
a + b
}
fn greet(name: &str) {
println!("Hello, {}!", name);
}
fn main() {
let result = add(3, 5);
println!("3 + 5 = {}", result);
greet("Rust");
}Explanation
Functions defined with fn. Last expression without ; is the return value.
Key Concepts
- Read the code carefully and understand the data flow
- Try modifying the example to see how it changes behavior
- Run this code in the Rust Playground
Related Topics
Browse more examples in the functions category to build a complete understanding of this topic.