3.15 Local Variables

You can define local variables (locals) in a colon definition:

: swap { a b -- b a }
b a ;
1 2 swap .s 2drop

(If your Forth system does not support this syntax, include
compat/anslocal.fs first).

In this example { a b -- b a } is the locals definition; it
takes two cells from the stack, puts the top of stack in b and
the next stack element in a. -- starts a comment ending
with }. After the locals definition, using the name of the
local will push its value on the stack. You can leave the comment
part (-- b a) away:

: swap ( x1 x2 -- x2 x1 )
{ a b } b a ;

In Gforth you can have several locals definitions, anywhere in a colon
definition; in contrast, in a standard program you can have only one
locals definition per colon definition, and that locals definition must
be outside any control structure.

With locals you can write slightly longer definitions without running
into stack trouble. However, I recommend trying to write colon
definitions without locals for exercise purposes to help you gain the
essential factoring skills.