HOOKSCRIPTv0.9.1

FUNCTIONS

In HookScript, functions are a way to create reusable code. A function starts with the func keyword, followed by the function name, then any parameters, its return type and a colon ( : ) to indicate the start of the function's body. Functions are terminated with the end keyword and use the return keyword to specify the value to return.

Named Function Statement

A function without types:

func add(x, y):
	return x + y
end

var value = add(10, 20)

A function with types:

func add(x int, y int) int:
	return x + y
end

var value int = add(10, 20)

Anonymous Functions

Functions are first class citizens in HookScript, allowing you to assign them to variables. An anonymous function is a convenient way to declare a function as an expression without specifying a name. To declare an anonymous function, simply omit the function's name.

var fn = func(x, y int): return x + y end
print(fn(10, 20))

Typed Functions (e.g., methods)

When declaring a named type you can attach a typed function that allows you to specify functionality only available to that type. This is analogous to methods in other programming languages. To declare a typed function, use a regular named function statement (see above), but attach the named type and an alias to the func keyword.

type cat = record<name text, age int>

func<c cat> meow():
	return "Meow, my name is " + c["name"]
end

var pj cat = ["PJ", 7]
print(pj.meow()) // prints "Meow, my name is PJ"

In the example above, we follow the func keyword with <c cat> indicating that we would like to attach the function to the cat type and refer to the value as c. In other programming languages, you might might use this or self in place of c to refer to the value.

Parameter Types

A function can have a mix of typed parameters and untyped parameters:

func add(x float, y)

If successive parameters have the same type you only need to declare the type for the last parameter:

func add(x float, y float)
func add(x, y float) // simplified version of the above function signature

To avoid successive parameters inheriting the type of the last parameter, you can use the any keyword to explicitly set the parameter types

function add(x any, y float)

Rest Parameter

A function's last parameter can be a rest parameter, allowing you to pass as many arguments to the function as you want. The parameter becomes a list when working inside the function's body

func add(n ...) // n becomes a list of "any" type
func add(n ...any) // same as above, but more explicit
func add(n ...int) // n becomes a list of "int" type
func add(myNumbers ...int) int:
	var total = 0
	var i = 0
	for i < len(myNumbers):
		total += myNumbers[i]
		i++
	end
	return total
end

var sum = add(10, 20, 30, 40, 50)

Loading Playground

Built with in Halifax, Nova Scotia by JW