Skip to content
Functions

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

int a = 3
int b = 4
str hello_world = hello(a, b) // pass in `int a` and `int b` from above
@print(hello_world)

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`
_ = hello() // discarded
hello() // error: return value of function not used