Skip to content

Sub

sub {name}
...
return [value]

Also searchable as: Sub · subroutine · function · procedure

sub starts a sub: a named group of lines that gosub runs, ending with return. Parameters passed by gosub are in %1, %2, … and their number is in %0.

gosub say hello world
halt
sub say
display ok %0 words: %1 %2
return

When the normal flow of the script reaches a sub line, it does not run the sub: it jumps over it, to the line after the sub’s first return. A sub with an early return, such as one inside an if, is therefore not skipped completely: the script lands in the middle of it. Two ways to avoid that:

  • Put the subs after the main part of the script, after its halt or its final goto, where the flow never reaches them. This is the usual layout.
  • Or wrap the body in a block right after the sub line. The script then skips the whole block:
sub check
{
if %x = 0
return 0
return 1
}
set %x 0
gosub check
display ok #Result ; 0
halt
  • Names are not case-sensitive. When two subs have the same name, gosub uses the last one in the script.
  • Leave a sub with return, never with goto.

See also

Based on the EasyUO wiki, edited for EasyUO Reforged. This text is available under the GNU Free Documentation License 1.2.