Functions
Like Lua, functions are first-class values in Ghost. That means that functions can be stored in variables, passed as arguments to other functions, and returned as results. This gives great flexibility to the language.
Defining Functions
You define functions using the function
statement, followed by a list of parameters, and a body:
function sum(a, b) {
print(a + b)
}
The body of a function is always a block. Inside it, you can return a value using a return
statement.
function sum(a, b) {
return a + b
}
Calling Functions
Once you have a function, calling it is as simple as passing the required parameters along with the function name:
value = sum(1, 2)
The assigned value is the result of either an explicit return
statement or the last value of the function's body.
Anonoymous Functions
Functions are first class in Ghost, which just means they are real values that you can get a reference to, store in variables, pass around, etc.
function addPair(a, b) {
return a + b
}
function identity(a) {
return a
}
print(identity(addPair)(1, 2)) // >> 3
Since function declarations are statements, you can declare local functions inside another function:
function outerFunction() {
function localFunction() {
print("I'm local!")
}
localFunction() // >> I'm local!
}
You can even combine local functions, first-class functions, and block scope:
function returnFunction() {
outside = "outside"
function inner() {
print(outside)
}
return inner
}
newFunction = returnFunction()
newFunction() // >> outside