Functions
Functions
A piece of code can be combined as a function. A function can expect parameters and can return a value. The simplest function is the println
function to print a message to the output stream (for example the console).
The syntactic structure of a function:
A quick example:
When you want to pass arguments to a function, do it like this:
To give a return type, you put an colon before the block of code. If you don't provide a return type it will be automaticly deduces. If the function has multiple return statements in branches the compiler tries to find the nearest common type except for System.ValueType and System.Object.
You can also use a shorthand syntax to define a function that directly return its body:
This sample declares a discriminated union and automaticly deduces the type for the function deducingTest
. The return type will be deduces to Expr
.
For default values, just define them in the head of the function like so:
Generic arguments will be defined with the where
keyword after the arguments:
When you want to skip a default variable of a function, do it this way:
Optional arguments have to be after mandatory arguments.
A function can also be overloaded. That means an other function that has the same same but different parameter types.
Here is a simple example:
Delegate
A delegate declaration is a type safe function pointer.
Last updated