Functions

Functions may exist directly inside a namespace. These free functions provide behavior without requiring a custom type or an object instance.

Free functions

A free function belongs to its namespace rather than to a struct. It has no implicit this value and may be called directly wherever its name is in scope.

namespace Xenon.Math;

public float Clamp(float value, float minimum, float maximum)
{
    if (value < minimum) return minimum;
    if (value > maximum) return maximum;
    return value;
}

From another namespace, import it with using Xenon.Math; or call it by its qualified name: Xenon.Math.Clamp(value, 0.0f, 1.0f).

Free functions are a natural fit for operations that do not belong to a particular object. Behavior tied to one struct instance is expressed as a method instead.

Declaration and calls

int Add(int a, int b)
{
    return a + b;
}

int result = Add(20, 22);

Arguments are checked against parameter types and parameters receive values from the caller. A struct parameter receives an independent copy of the struct's fields. Pointer fields are copied as pointer values; the referenced memory is not deep-copied.

Return values

A value-returning function uses return expression;. A void function completes with return; or by reaching the end of its body.

void Reset(int* value)
{
    *value = 0;
    return;
}

Program entry point

An executable starts at the namespace-level function int Main(). The compiler creates the native platform entry wrapper and returns the Xenon result to the operating system.

namespace Example;

int Main()
{
    return 0;
}