Functions
Defining Functions
Functions are declared with a return type, name, and parameter list. Use Allman-style braces for block bodies:
string Greet(string name){ return "Hello, " + name + "!";}
int Add(int a, int b){ return a + b;}Expression Bodies
For single-expression functions, write the expression directly after the signature (no braces):
float Square(float x) x * x;
int Max(int a, int b) a > b ? a : b;Void Functions
Functions that return nothing use void:
void Log(string message){ Console.WriteLine(message);}Default Parameters
void Connect(string host, int port = 8080, int timeout = 30){ // ...}
Connect("localhost");Connect("localhost", 9000);Reference Parameters
Pass by reference using ref, or use in for immutable by-reference (equivalent to immut ref):
void Swap(ref int a, ref int b){ int tmp = a; a = b; b = tmp;}
int Sum(in Point p){ return p.x + p.y; // read-only access}Closures
Closures capture variables from their enclosing scope:
int(int) MakeAdder(int n){ return (int x) => x + n;}
int(int) add5 = MakeAdder(5);Console.WriteLine($"{add5(3)}"); // 8Const Functions
Mark a function with trailing const to indicate it can be evaluated at compile time:
int Factorial(int n) const{ if (n <= 1) { return 1; } return n * Factorial(n - 1);}
const int result = Factorial(5); // evaluated at compile timeImmut Methods
Mark a method immut to indicate it does not modify the receiver:
type Vector2{ float x; float y;
float Length() immut Math.Sqrt(x * x + y * y);}Overloading
Multiple functions can share a name if their parameter types differ:
void PrintVal(int x) { Console.WriteLine($"{x}"); }void PrintVal(float x) { Console.WriteLine($"{x}"); }void PrintVal(string x) { Console.WriteLine(x); }