Next: Decompilation Tutorial, Previous: Comments Tutorial, Up: Tutorial [Contents][Index]

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 `squared`

. The
following comment describes its stack effect. The words `dup *`

are not executed, but compiled into the definition. `;`

ends the
colon definition.

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`nip`

,`tuck`

,`negate`

, and`/mod`

in 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.