| www.delorie.com/gnu/docs/gforth/gforth_27.html | search |
![]() Buy GNU books! | |
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
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/anslocals.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 controll 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.
Reference: 5.19 Locals.
| webmaster donations bookstore | delorie software privacy |
| Copyright © 2003 by The Free Software Foundation | Updated Jun 2003 |