are similar to procedures and functions in other programming languages.
: squared ( n -- n^2 ) dup * ; 5 squared . 7 squared .
: starts the colon definition; its name is
following comment describes its stack effect. The words
are not executed, but compiled into the definition.
; ends the
The newly-defined word can be used like any other word, including using it in other definitions:
: cubed ( n -- n^3 ) dup squared * ; -5 cubed . : fourth-power ( n -- n^4 ) squared squared ; 3 fourth-power .
Assignment: Write colon definitions for
/modin terms of other Forth words, and check if they work (hint: test your tests on the originals first). Don't let the ‘redefined’-Messages spook you, they are just warnings.
Reference: Colon Definitions.