Functions
A function is a block of code that is only executed when it is called. You can pass data into a function and a function can return data aswell.
Function definition
func double(n) {
var n = n * 2
return n
}
Calling a function
Pansy> double(5)
@ 10
Arguments
You can pass information inside a function. Taking the function double presented before:
Pansy> double(2)
@ 4
Pansy> double(4)
@ 8
You can also define optional arguments:
func hey(firstName, age=18) {
print("Hey! I'm " + firstName + " and I'm " + to_str(age) + " years old!")
}
firstName is a mandatory argument. However, age isn’t. This means that you don’t need to pass a value for it. It will have a predefined value. There are two ways of calling this function:
Pansy> hey("John")
Hey! I'm John and I'm 18 years old!
Pansy> hey("John", age=35)
Hey! I'm John and I'm 35 years old!