Skip to content

4. Functions

CoolElectronics edited this page Jul 3, 2022 · 1 revision

You've already seen a call to a builtin function, put, but there are many other builtin functions and ways to define your own global functions are called without parenthesis, and arguments are separated by spaces.

put "Hello, World"

List of builtin functions
put : Prints to standard output. takes 1 argument, the string to be printed
get : Waits for a line to be entered to the standard input and returns it as a variable. takes no arguments
exit : Immediately stops the program and exits with a status code of 0. takes no arguments.
assert : exits the program with a status code of 1 if the 2 arguments are not equal. takes 2 arguments
array : returns a new empty array with length 0. takes no arguments
object : returns a new empty object. takes no arguments
len : given an array, returns the length of that array. takes 1 argument, the array panic : used mostly internally for testing. exits the program with a status code of 1, indicating a failed test. takes no arguments

Defining your own functions: First put the name of the function, then the arguments it takes, and put the function body in curly braces.

here is an example that takes 2 numbers and returns the sum of them

add num1 num2{
    return num1 + num2
}

put add 1 1

due to a possible ambiguity, functions must be defined before anything uses them. i haven't found a workaround for this yet.

Clone this wiki locally