ScratchScript 1.0 Help

Functions

Functions are reusable pieces of code that receive an input and produce an output (or modify the program's state).

Functions are actually internally called "procedures" in Scratch, but are named "functions" in ScratchScript for simplicity.

To define a function, use the function keyword:

function doNothing() { }

Inputs can be specified with or without the type:

function sum(a: number, b: number): number { return a + b; } // is the same as function sum(a, b) { return a + b; }

Unlike Scratch, one can change function arguments in-place without cloning them to local variables:

function subtractAndDouble(x: number): number { x--; x *= 2; return x; }

Member functions

ScratchScript supports creating extensions for types via the @extension attribute. This means that a function can be called on specific objects, unlike static functions.

function firstCharacter(str) { return str[0]; } on start { say(firstCharacter("hello!")) // will say "h" }
@extension(string) function firstCharacter(str) { return str[0]; } on start { say("hello!".firstCharacter()); // will also say "h", // but the code is a bit cleaner! }
Last modified: 02 мая 2024