59 lines
896 B
Plaintext
59 lines
896 B
Plaintext
---
|
|
title: Functions
|
|
description: Function declarations, self parameters, return types, and unsafe functions.
|
|
icon: Function
|
|
---
|
|
|
|
```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
|
|
}
|
|
```
|
|
|
|
Function syntax: `[pub] [void | <type>] <name>[<generics>](<params>) [override] { <body> }`
|
|
|
|
Functions use **Allman brace style** (opening brace on the next line).
|
|
|
|
### Self Parameter
|
|
|
|
Methods can take `self`, `&self`, `&mut self`, or `self` with lifetimes:
|
|
|
|
```mist
|
|
pub void set_value(&mut self, i32 v)
|
|
{
|
|
self.value = v;
|
|
}
|
|
```
|