Skip to content

Functions

Artsiom edited this page Jan 17, 2022 · 18 revisions

Functions

Function declarations

Function declaration has the next syntax (something in brackets [] means optional):

function_declaration: NAME "(" function_parameters? ")" function_return_type "{" statements_block "}"

Example:

    foo (a int, b int) int {
        ret a + b
    }

Each parameter must be explicitly typed and parameters must be separated using commas.

Function call

When calling a function the arguments must be provided in the proper sequence as they were declared

make_pretty (name str, age int) str {
    ret name + str(age)
}

# function call
make_pretty("Bob", 10)

Function parameters

Arguments are passed by reference except string, int, bool, float, they are passed by values.

# Function declaration
insert(from IntList, to IntList) IntList {
    for x in from {
        append(x, to)
    }
    ret to
}

# Pass by reference
let elements IntList = insert([4, 5, 6], [1, 2, 3])
# elements = [1, 2, 3, 4, 5, 6]

Each parameter is constant by default.

# Function declaration
foo (param int) {
    param = 10 # BAD, cannot reassign
}

Default functions

  • print(element: AnyType) -> None - it prints an element with a newline
  • append(element: AnyType, elements: AnyList) -> AnyList - add item to list
  • remove(element: AnyType, elements: AnyList) -> AnyList - remove item from list
  • len(elements: AnyList) -> AnyList - length of a list
  • range(from: int, to: int) -> Iterable - range from:to exclusive
Clone this wiki locally