Skip to content

Procedures

A proc becomes a Scratch custom block.

proc double(n: num) -> num {
return n * 2;
}

Parameters and the return type are always annotated — there is no inference across a procedure boundary. Use -> void for a procedure that returns nothing.

Top level — copied into every sprite that calls it. Scratch custom blocks belong to a target, so a proc used by three sprites is emitted three times, and a sprite that can’t reach a proc doesn’t get one.

Inside a sprite — available only to that sprite.

proc report(label: str, value: num) -> void { ... } # any sprite can call
sprite Cat {
proc pounce(power: num) -> void { ... } # Cat only
}
proc greet(name: str, times: num = 1) -> void {
for (i, times) { looks.say(name, 1); }
}
greet("Cat"); # times = 1
greet("Cat", 3);

Arguments can be routed by name instead of position:

motion.goTo(y = 0, x = 0);

Handy when a call has several num parameters and the order is not obvious. Naming the same argument twice is an error.

A trailing comma after the last argument is allowed:

motion.goTo(
x = 0,
y = 0,
);

A list or dict can be passed, and writes reach the caller’s list. See Passing a list to a procedure.

proc fill(xs: list<num>, n: num) -> void { xs.add(n); }
fill(scores, 1);

A parameter becomes a Scratch input, and Scratch inputs cannot be changed. Assigning to one is an error:

proc countdown(n: num) -> void {
n -= 1; # error: Cannot assign to parameter 'n'
}

Copy it into a variable first if you need to change it:

proc countdown(n: num) -> void {
private left: num = n;
left -= 1;
}

Two procedures may share a name if their parameter lists differ. Dispatch is resolved by argument types:

proc say(msg: str) -> void { ... }
proc say(msg: str, seconds: num) -> void { ... }
looks.say("hi");
looks.say("hi", 2);

This is how the standard library offers motion.goTo(x, y) and motion.goTo(target) under one name.

The parameter types have to differ. Two overloads with the same types are a redeclaration, even when the parameter names are different:

proc move(steps: num) -> void { ... }
proc move(distance: num) -> void { ... } # error: 'move' is already declared in this scope with the same parameter types

A first parameter named self makes a procedure callable in receiver position:

proc contains(self: list<T>, item: T) -> bool { ... }
scores.contains(4); # method form ✅
list.contains(scores, 4); # namespace form — type-checks, fails codegen

The standard library declares list, dict, and str methods this way, and your own procedures can too. The receiver can be any type: num, str, bool, a list, a dict or a struct.

struct Point { x: num = 0, y: num = 0 }
proc total(self: list<num>) -> num { ... }
proc twice(self: num) -> num { return self * 2; }
proc sum(self: Point) -> num { return self.x + self.y; }
scores.total();
n.twice();
p.sum(); # the receiver's type is still checked: a Q here is an error
total(scores); # a user proc can still be called the plain way

Inside a sprite, self on its own means the sprite. A parameter named self is just an ordinary value.

User procedures run warped (without screen refresh) by default, which is usually what you want — the whole procedure completes in one frame. Override with @warp:

proc animate(@warp = false) -> void {
for (i, 10) {
motion.forward(5); # now visible frame by frame
}
}

Decorators go inside the parameter list, before any parameters. See Decorators.

Scratch custom blocks cannot return anything. Katnip works around it, and the workaround it picks depends on your call graph.

Before codegen, the compiler runs Tarjan’s strongly-connected-components algorithm over the resolved call graph. Procedures that are not part of a cycle get the cheap strategy; procedures that are get the correct one.

var — the default for non-recursive procs

Section titled “var — the default for non-recursive procs”

The procedure writes its result to a dedicated variable (double_ret), then control_stops. The caller reads the variable. One variable, no overhead.

The procedure pushes its result onto a per-proc Scratch list and the caller pops it. That costs a list operation per call, and it is the only thing that makes recursion return correct values — a plain return variable would be clobbered by the inner call.

proc fib(n: num) -> num {
if (n <= 1) {
return n;
}
return fib(n - 1) + fib(n - 2);
}

fib calls itself, so it lands in a cycle, so it gets vstack, so it works.

proc thing(@ret = "auto") -> num { ... } # or "var" or "vstack"

auto is the default and is almost always right. Forcing var on a procedure that is in a cycle is a hard compile error, not a warning — its failure mode is silently wrong values, which is the worst kind.

Parameter types decide the Scratch argument shape: num → %n, str → %s, bool → %b. A bool parameter gets a hexagonal slot in the custom block, as you would expect.

A procedure can return a list<T>. Each stored result is its own copy, and two calls in one statement don’t overwrite each other:

proc pair(n: num) -> list<num> { return [n, n * 10]; }
private xs: list<num> = pair(1); # a copy; changing xs leaves the next result alone
looks.say(Str(pair(2)[2])); # read straight from the call
for (x, pair(3)) { ... } # or walk it

A list return can pass through a list parameter, a slice, or another procedure’s result.

  • Dicts: Procedures cannot return 'dict' yet.
  • A list of structs: Procedures cannot return a list of structs yet.
  • A list from a recursive procedure. A list return can’t use vstack, so a procedure in a call cycle (or one marked @ret = "vstack") can’t return a list.

A tuple return must be unpacked into plain variables with a pattern. Reading one any other way stops the build:

proc bounds() -> (num, num) { return (1, 10); }
(lo, hi) = bounds();

Return values survive recursion. A variable declared inside a procedure does not — it is a mangled global, and the inner call overwrites it.

proc bad(n: num) -> num {
private half: num = n / 2; # clobbered by the recursive call below
if (n <= 1) { return 1; }
return bad(half) + half; # `half` is now the inner call's value
}

Pass state through parameters and returns in anything that recurses.