Topic
Functions
A function is a named block of code that takes inputs, does something with them, and can hand a value back. In Rust, every function that returns a value must declare its return type, and the last line of the function body is what gets returned.
Quick Reference
ConceptWhat it means
fn name()Declare a function named name
fn name(x: i32)Function with one parameter: x of type i32
fn name(x: i32, y: i32)Two parameters, each needs its own type
-> i32The function returns an i32 to whoever called it
CallerThe code that calls a function and receives what it returns
TermWhat it meansWhen you use it
fn name()Declare a function named nameAny time you want a named, reusable block of code
fn name(x: i32)Function with one parameter: x of type i32When a function needs an input value to work with
fn name(x: i32, y: i32)Two parameters, each needs its own type annotationWhen the function takes multiple inputs
-> i32The function returns an i32 to whoever called itAny function that hands a value back to the caller
CallerThe code that calls a function and receives what it returnsUnderstanding where a function's return value goes