Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Functions

Sydrogen functions are declared with either:

  • Nunction for a function that does not return a value
  • A return type, such as Int, Float, Weld, Bool, Ore, or Materials, for a function that returns data

Furnace compiles each user-defined function as an independent native function. Calls use the function's declared parameter and return types.

Nunction

A Nunction is a function that does not return a value.

Nunction Tick()
{
    Print("tick");
}

It can be called with:

Tick();

Nunction calls do not return a value. They can take parameters and can call other functions.

Returning Functions

A returning function declares its return type before its name. It sends a value back to its caller with the Return keyword.

For example, this function declares an Int return type:

Int Add(Int A, Int B)
{
    Return A + B;
}

A returned value can be used like this:

Int Result = Add(10, 20);

The returned value can be assigned to a compatible variable or used directly in another expression.

Returning functions can also return collection data. The declared shape and element types must match the returned value:

Ore(Int Number, Weld Name) MakePerson()
{
    Return {14, "Den"};
}

The Return Keyword

Return ends the current function call and sends its expression back to the caller:

Weld Greeting()
{
    Return "hello";
}

Return is case-sensitive and must be written with a capital R.

A Nunction cannot return data because it has no return type. This is an error:

Nunction Bad()
{
    Return 42;
}

Furnace reports Void function Bad cannot return a value.

The returned expression must also be compatible with the function's declared return type. Returning a mismatched data type is an error:

Int Bad()
{
    Return "wrong";
}

Furnace reports Return type mismatch in Bad: expected Int, got Weld.

Function Parameters

The grammar accepts parameter declarations:

Nunction PrintNumber(Int Value)
{
    Print(\V"{Value}");
}

A call can be written as:

PrintNumber(42);

Furnace checks argument count and argument types during semantic analysis. Type aliases such as String and Weld, and Boolean and Bool, are treated as equivalent.

Native Function Calls

For example:

Nunction Tick()
{
    Print("tick");
}

Open Nunction Main()
{
    Tick();
}

The compiler compiles Tick as an independent Cranelift function and emits a call from Main.

The same calling convention supports parameterized functions, return values, calls inside If, While, and For, and recursive or mutually recursive functions.

See Function Calls for details on how calls are written, and Recursion for the current state of recursive functions.


← Previous Next →