Files

55 lines
771 B
Plaintext

---
title: Functions
description: Function declarations, self parameters, return types, and unsafe functions.
icon: SquareFunction
---
```mist
// Return type is `void` (unit)
void greet(str& name)
{
println!("Hello, {name}");
}
// With return type
i32 add(i32 a, i32 b)
{
return a + b;
}
// Expression body (last expression is the return value)
i32 square(i32 x)
{
x * x
}
// Public function
pub i32 multiply(i32 a, i32 b)
{
a * b
}
// Generic function
T identity<T>(T value)
{
value
}
// Unsafe function
unsafe i32 dangerous()
{
42
}
```
### Self Parameter
Methods can take `self`, `mut self`, `&self`, `&mut self`, and `&'a self` / `&'a mut self` with lifetimes:
```mist
pub void set_value(&mut self, i32 v)
{
self.value = v;
}
```