Skip to reading
PCA Script Programming

10. Functions and parameters

Edition 0.86.4.1 HF1

0.86.4.1 Hotfix 1 edition. Earlier lessons and screenshots retain their original version context; revised references and supplements describe the newer behavior.

Download PDFView illustrations
Code example
function total_price(price: number, count: integer = 1): number
    return price * count
end
Code example
function run()
    let one = total_price(2.5)
    let three = total_price(2.5, 3)
    return [one, three]
end

A function packages a job under a name. Parameters are inputs. The returned value is its output. A default value makes the last parameter optional. Calls to ordinary functions use positional arguments in the declared order.

Declare functions at the top level, not inside another function. Use function run() as the normal entry point when the record is called directly. A script can also contain top-level instructions, but when run exists the default call selects that function. Keep initialization in declarations and the actual job in run to avoid ambiguity.

A function can call another function, including itself, but the runtime limits nested calls to 64. A loop is usually better than recursion for walking a list of inventory items. Type annotations describe supported value types; they do not turn a function into an exposed visual node by themselves.

Figure 4. Small named functions keep puzzle checks separate from the response that uses them.

Page 17 of 357Permalink
Go to page