Skip to content

Functions

Basic Functions

Functions declare their return type before the name:

int add(int a, int b)
{
return a + b;
}
Console.WriteLine(add(3, 4)); // 7

Block Bodies vs Expression Bodies

In Cera, any declaration or control-flow statement accepts either a block body { } or a single expression as its body. This applies to functions, if, while, for, and more.

// Block body — multiple statements, explicit return
int square(int x)
{
return x * x;
}
// Expression body — single expression, implicit return
int square(int x) x * x;
// This works for any return type
float average(float a, float b) (a + b) / 2.0;
string greet(string name) "Hello, " + name + "!";

The expression body form is especially useful for short methods and operators inside types.

Void Functions

Functions that don’t return a value use void:

void greet(string name)
{
Console.WriteLine("Hello, " + name + "!");
}

Default Parameters

Parameters can have default values:

void log(string msg, int level = 0)
{
if (level > 0)
Console.WriteLine("[WARN] " + msg);
else
Console.WriteLine("[INFO] " + msg);
}
log("Starting up"); // level defaults to 0
log("Low memory", 1); // level = 1

By-Reference Parameters

Use ref to pass values by reference, or in for immutable by-reference:

void increment(ref int x)
{
x = x + 1;
}
int n = 10;
increment(ref n);
Console.WriteLine(n); // 11
// 'in' passes by reference but prevents modification (sugar for 'immut ref')
int sum(in Point p)
{
return p.x + p.y;
}

Const Functions

Functions marked const can be evaluated at compile time:

int factorial(int n) const
{
int r = 1;
while (n > 1)
{
r = r * n;
n = n - 1;
}
return r;
}
const int f10 = factorial(10); // computed at compile time

Next Steps

Learn about control flow — loops, conditionals, and more.