A definition can be called simply be writing the name of the definition
to be called. Normally a definition is invisible during its own
definition. If you want to write a directly recursive definition, you
can use recursive
to make the current definition visible, or
recurse
to call the current definition directly.
recursive
( compilation – ; run-time – ) gforth-0.2 “recursive”
Make the current definition visible, enabling it to call itself recursively.
recurse
( ... – ... ) core “recurse”
Alias to the current definition.
For examples of using these words, See Recursion.
Programming style note:
I prefer using recursive
to recurse
, because calling the
definition by name is more descriptive (if the name is well-chosen) than
the somewhat cryptic recurse
. E.g., in a quicksort
implementation, it is much better to read (and think) “now sort the
partitions” than to read “now do a recursive call”.
For mutual recursion, use Defer
red words, like this:
Defer foo : bar ( ... -- ... ) ... foo ... ; :noname ( ... -- ... ) ... bar ... ; IS foo
Deferred words are discussed in more detail in Deferred Words.
The current definition returns control to the calling definition when
the end of the definition is reached or EXIT
is encountered.
EXIT
( compilation – ; run-time nest-sys – ) core “EXIT”
Return to the calling definition; usually used as a way of
forcing an early return from a definition. Before
EXIT
ing you must clean up the return stack and
UNLOOP
any outstanding ?DO
...LOOP
s.
Use ;s
for a tickable word that behaves like exit
in the absence of locals.
?EXIT
( – ) gforth-0.2 “?EXIT”
Return to the calling definition if f is true.
;s
( R:w – ) gforth-0.2 “semis”
The primitive compiled by EXIT
.