5: Functions [✓] - royal-lang/rl GitHub Wiki
Functions in Royal are defined using the fn keyword. If there are no return type given to the signature then it will default to void as the return type.
Royal allows for functions to be nested within each other.
fn NAME()
{
...
}
fn RETURN_TYPE NAME()
{
...
}
fn NAME(TEMPLATE_PARAMS)()
{
...
}
fn NAME(PARAMS)
{
...
}
fn NAME(TEMPLATE_PARAMS)(PARAMS)
{
...
}
Example:
fn main(string[]:const args)
{
}
fn T add(T)(T x, T y)
{
return x + y;
}
To call a function you just specify its name and then the parameters you wish to pass to it.
FUNCTION_NAME();
FUNCTION_NAME(PARAMS);
FUNCTION_NAME(TEMPLATE_PARAMS)();
FUNCTION_NAME(TEMPLATE_PARAMS)(PARAMS);
Example:
writeln();
writeln("Hello World!");
var x = 100;
var y = 100;
var z = add(int)(x,y);
var integer = get(int)();