Functions
Like all other languages, a function is a block of code to perform a specific task. Currently, functions can only be declared in class bodies, i.e., they are methods. Top-level functions will be supported in the near future.
Functions are declared using the fn keyword.
class SomeClass {
fn someFunc() { somePrivFunc() }
priv fn somePrivFunc() { print("Hello Manul!") }
}Access Modifiers
Methods can include modifiers before the fn keyword to control visibility:
pub: Public methods. This is the default behavior and can be omitted.priv: Private methods. Accessible only within the same class.prot: Protected mathods. Accessible within the same package.
Naming Convention
The function name use camelCamel as the conventional style.
Parameters
Section titled “Parameters”Parameters are special variables that are part of the function’s signature. When functions are called, the caller needs to provide concret values (arguments) for these parameters.
Let’s a parameter to the previous example:
class SomeClass {
fn someFunc() { somePrivFunc("Hello Manul!") }
priv fn somePrivFunc(s: string) { print(s) }
}Return Values
Section titled “Return Values”If a function returns values, the return type must be specified using the -> type syntax following the parameter list.
class SomeClass {
fn funcWithReturn() -> int { return 1 }
}Statements
Section titled “Statements”The function body consists of statements—instructions that perform actions.
Here are examples of statements:
fn test() { // Variable declaration statement var a = 1
// Branch statement if (a > 0) { print("Positive") } else { print("Negative") }
// While loop while (a > 0) { a-- }
// A function call is a statement print("Hello")}