Functions
Functions are defined with the fn keyword. The return type of the function must be declared if a function returns any value.
fn <name>( <type> arg_1, <type> arg_2, ... ) -> <return type> { // Arguments are immutable // Any variables declared inside are scoped to the function}Example function:
fn hello(int a, int b) -> str { a = 2; // Error mut int b = b; // Creates new scoped variable `b`, let's call it `bscope`. b = 2; // This will edit `bscope`, not the argument value `b`
return "Hello, world!";}// `bscope` is not available here
str hello = hello(a, b); // pass in `int a` and `int b` from aboveprint(hello);If a function returns a value, that value must be assigned to a variable, or discarded by assigning to _.
fn hello() -> str { return "Hello, world!";}
str hello = hello(); // assigned to `hello`str _ = hello(); // discardedhello(); // error: return value of function not used