Functions
There's (like variables) a lot to say about functions.
Note: all "%import libmc/print.uchc" will only work if you use the bundle provided + MinLang
Creation
Functions are defined using the fun keyword, followed by the function name, parameters (if any), and a colon (:). The function body is indented below the function definition.
fun MyFunction():
Print()<"Hello, World!">
MyFunction
Closing a Function
A function is closed by writing its name again on a new line, without any indentation.
fun MyFunction():
Print()<"Hello, World!">
MyFunction
Function Parameters
Function parameters are defined within the parentheses following the function name. Each parameter must have a name and a type.
Parameters are separated by semicolons (;). You must end the function's declaration by ;
fun Test(Int a; Int b;):
Print()<"1">
Test
Parameters can be of any type, including Void (yes).
No default values for parameters.
Accessing Parameters
A parameter is a local variable, so you can access it using the #:: prefix:
fun Test(Int a; Int b;):
#::a::Add()<1> // Adds 1 to the parameter 'a'
Test
Return Values
Not yet supported, ~~but will be in the future.~~ probably won't happen
Calling Functions
To call a function, simply write its name followed by parentheses and angle brackets. If the function has parameters, provide the arguments within the angle brackets, separated by semicolons (;).
%import libmc/print.uchc
fun CheckAge(Int age;):
If (#::age >= 18): Print()<"You are major">
If (#::age < 18): Print()<"You are minor">
CheckAge
fun Main():
// This import will only on MinLang + -Ilibmc
CheckAge()<18>
Main
If the function has no parameters, you can call it with empty angle brackets:
%import libmc/print.uchc
fun SayHello():
Print()<"Hello!">
SayHello
fun Main():
SayHello()<>
Main
Main Function
The Main function is a special function that serves as the entry point of the program. It is called automatically when the program starts.
Note: this function must be present, otherwise error: 17
%import libmc/print.uchc
fun Main():
Print()<"This is the main function!">
Main
You can have only one Main function in your program.
Recursive Functions
Functions cannot call themselves directly (no direct recursion). However, you can achieve recursion by using an intermediary function.
fun MainCall():
Main()<>
MainCall
fun Main():
MainCall()<>
Main
Function Naming
Function names must start with a letter (a-z, A-Z) or an underscore (_), followed by any combination of letters, digits (0-9), or underscores. Function names are case-sensitive.
fun myFunction():
Print()<"This is my function!">
myFunction
Function Flags
Functions can have flags that modify their behavior. Flags are specified before the fun keyword.
ascode
The ascode flag indicates that the function should copy it's code, and hardcode every parameters when called.
What does that mean?
When you call a function with the ascode flag, the function's code is copied and pasted in a new function (name[nbr_of_functions]), with all parameters replaced by their actual values.
Why?
As you know, Minecraft supports only Int local variables. So if you want to use a function with parameters other than Int (e.g. String), you need to use the ascode flag.
Limitation: You cannot change the value of a parameter inside an ascode function. (Because it's hardcoded), nor call it with a value type that is not literal or global variable.
ascode fun WhileGt(String var; Int value; String callback;):
If (#::{$::var} > $::value): Execute()<$::callback>
If (#::{$::var} > $::value): Execute()<$::self>
WhileGt
The self global variable
The self global variable is a function pointer to the current function. It can be used to call the current function recursively (indirect recursion only).
ascode fun WhileGt(String var; Int value; String callback;):
If (#::{$::var} > $::value): Execute()<$::callback>
If (#::{$::var} > $::value): Execute()<$::self>
WhileGt
$::self is the function pointer to the current function (WhileGt in this case).
(MinLang specific) the loop flag
The "loop" flag allows you to create a fast (repeating cmd block) loop that CANNOT be stopped and is started when the user launches the install command
Example:
%import libmc/print.uchc
loop fun Loop():
Print()<"Looped!">
Loop
fun Main():
Main
Note: No need to call Loop()<> to start the loop.