Djinn Lang

Functions

Declaration

Functions are declared with a return type, name, parameters, and body:

i32 sum(i32 a, i32 b) {
    return a + b;
}

The return type comes before the function name. Use void for functions that don't return a value:

void greet() {
    printf("hello!\n");
}

Parameters

Parameters are declared with their type followed by the name:

i32 multiply(i32 x, i32 y) {
    return x * y;
}

Variadic parameters are supported in extern declarations:

extern "C" {
    i32 printf(i8* format, ...);
}

Return Values

Use return to exit a function with a value. Multiple return paths are allowed:

i32 absolute(i32 x) {
    if (x < 0) {
        return 0 - x;
    }
    return x;
}

Entry Point

Every program needs a main function. It can return i32 or void:

i32 main() {
    return 0;
}

main can also be async:

async i32 main() {
    i32 val = await compute();
    return val;
}

See Async/Await for details.

constexpr Functions

A constexpr function can be evaluated at compile time when called with constant arguments, or at runtime otherwise:

constexpr i32 square(i32 x) {
    return x * x;
}

i32 main() {
    return square(5);  // may be evaluated at compile time
}

consteval Functions

A consteval function must be evaluated at compile time. Calling it with non-constant arguments is a compile error:

consteval i32 hash_seed(i32 x) {
    return x * 31 + 7;
}

See Compile-Time Evaluation for full details.

On this page