Functions
Functions are reusable pieces of code that can do calculations. Pretty much like functions in any other programming language.
Parameter types need to be specified explicitly, return type is automatically deduced.
Global Scope
The global scope for the most part, works the same as a function. All statements in the global scope will get executed when the program is executed. The only difference is that new types can be defined in the global scope.
Parameter packs
To allow passing an unlimited number of parameters to functions, parameter packs can be used.
Parameter packs can exclusively be used as arguments to other functions. Using them automatically expands them acting as if the contained values get passed to the other function individually.
The above function will print out all passed values in sequence by printing the first parameter and then removing it by not passing it on to the function during the next run.
Default parameters
Default parameters can be used to set a default value for parameters, if they weren't provided when the function got called.
Reference parameters
Reference parameters can be used to avoid unnecessarily copying lots of data around and are helpful when needing to pass custom types that do not have a fixed layout to a function. Instead of copying the given parameter value onto the function’s heap, a reference to the existing pattern is instead used.
Variables
Variables can be declared in a similar fashion as outside of functions but they will be put onto the function’s stack instead of being highlighted.
Custom types may also be used inside of functions
It is also possible to declare constants using the const
keyword
Control statements
If-Else-Statements
If, Else-If and Else statements work the same as in most other C-like languages. When the condition inside a if
head evaluates to true, the code in its body is executed. If it evaluates to false, the optional else
block is executed.
Curly braces are optional and only required if more than one statement is present in the body.
While-Loops
While loops work similarly to if statements. As long as the condition in the head evaluates to true, the body will continuously be executed.
For-Loops
For loops are another kind of loop similar to the while loop. Its head consists of three blocks (ex. i < 10
) that are separated by commas.
The first block is for declaring an iterating variable. This variable can only be used inside of the for loop. The value that this variable is declared as is the iteration that it starts at. In the given example, the first block is u8 i = 0
.
The second block is a condition used to determine when the loop should run again, and when it should stop. Every time the loop repeats, this condition is checked. In the given example, the second block is i < 10
. At the start of each loop, the current value of i
is checked to see if it is still less than 10
. When i
becomes 10
or greater, the loop will stop executing. As long as the statement in this block resolves to true
, the loop will continue.
The third block only executes after the body of the loop. It assigns the iterating variable a new number, one that is incremented by a specified amount - in short, it increments the iterating variable. In the example, the increment used is 1
, but it doesn't have to be 1
. If it's necessary to increment by 2
, 3
, etc., just use that number instead of 1
.
The assignment operator +=
can also be used for incrementing a for loop:
Loop control flow statements
Inside of loops, the break
and continue
keyword may be used to control the execution flow inside the loop.
When a break
statement is reached, the loop is terminated immediately and code flow continues after the loop. When a continue
statement is reached, the current iteration is terminated immediately and code flow continues at the start of the loop again, checking the condition again.
Return statements
In order to return a value from a function, the return
keyword is used.
The return type of the function will automatically be determined by the value returned.
Pattern views
When using the placement syntax inside of functions or function statements in the global scope (such as if
, for
, or while
statements), a view of that data is created instead.
A view acts very similar to a placed pattern - you can access and pass that value around as if it was a placed variable. However, it will not generate an output pattern like a regular placement would.
Last updated