
So far you have used the words SuperForth came with. 7 DUP * . squares seven, and it works, but you have to remember that DUP * means "square" every time you read it.
Forth's whole idea is that you should not have to. You give DUP * a name, and from then on the name is a word, exactly as good as DUP or +. Programming in Forth is this: adding words until the thing you wanted is one of them.
A definition starts with a colon, then the new word's name, then what it should do, and ends with a semicolon:
: SQUARE DUP * ;
Read it aloud as "colon SQUARE means DUP times, semicolon".
The colon and the semicolon are words, so each needs spaces round it.
In MAME, the colon is the ' key, to the right of ;.
When you press ENTER, SuperForth says ok and nothing else happens. Nothing was squared. You have not run anything; you have taught it something. Now run it:
7 SQUARE .
prints 49. SQUARE takes a number from the stack and leaves its square, which you can write as ( n -- n*n ), the same way as the words it came with.
Here is the part that matters. A new word can use your earlier words:
: CUBE DUP SQUARE * ;
CUBE copies the number, squares the copy, and multiplies. It does not care that SQUARE is yours and DUP is not. There is no difference between them.
: SQUARE DUP * ;
7 SQUARE .
12 SQUARE .
: CUBE DUP SQUARE * ;
3 CUBE .
Starting from
A fresh boot, with SuperForth just started.
What you should see
: SQUARE DUP * ;
ok
7 SQUARE .
49 ok
12 SQUARE .
144 ok
: CUBE DUP SQUARE * ;
ok
3 CUBE .
27 ok

A line you type must be under 40 characters. This is the rule that bites hardest in this section, because definitions are the first long lines you type.
SuperForth reads one row of the screen: the row the cursor is on when you press ENTER. The screen is 40 characters wide. Type more than that and your line wraps onto a second row, and only that second row is read. The start of your definition, colon and all, is ignored - and the tail end is then run as if you had typed it by itself, which usually ends with SuperForth complaining about a stray semicolon:
;? Compilation only, Use in Definition
A line of exactly 40 characters is worse. The cursor has just wrapped onto an empty row, the empty row is what gets read, and SuperForth says ok to it. No error, and no new word.
The cure is simple, because a definition can run over as many lines as you like. Press ENTER wherever it suits you. SuperForth will not say ok until it has seen the semicolon, and that missing ok is how you know it is still listening to the same definition:
: DOUBLE
DUP + ;
Anything between round brackets is a comment, and SuperForth skips it. The custom is to put the stack picture straight after the name:
: SQUARE ( n -- n*n ) DUP * ;
The opening bracket is a word, so it needs a space after it. It costs nothing when the word runs, and the day you come back to a word you wrote last month, it is the first thing you will look for.
In MAME, the round brackets are SHIFT and 8, and SHIFT and 9: one key to the left of where your own keyboard has them.
." works inside a definition, and this is where it becomes useful. The message is printed when the word runs, not when you define it:
: HI ." HELLO" ;
says ok and prints nothing. HI prints HELLO.
A word SuperForth does not know, inside a definition. It stops at that word and says so in the usual way, and the whole definition is thrown away. Nothing half-made is left behind. Fix the line and type it again.
The same name twice. SuperForth lets you, with a warning:
SQUARE Isn't Unique ok
From then on SQUARE means the new definition. But there is a catch, and it surprises everybody: words you had already built on the old SQUARE go on using the old one. CUBE was made from the SQUARE that existed when you defined CUBE, and it keeps it. To change what CUBE does you have to define CUBE again as well. S8 shows a tidier way to go about it.
Nothing is kept. Your words live in the machine's memory. Leave for DOS, or restart the Einstein, and they are gone. That is fine for now, since these are a line or two each. S9 is about keeping them.
SQUARE, then type SQUARE with nothing on the stack. What does SuperForth say? Look at the stack afterwards. Did anything notice?: FOURTH SQUARE SQUARE ; and try 3 FOURTH . Work out the answer first.SQUARE and CUBE as above, then redefine : SQUARE DUP + ;. Now try 7 SQUARE . and 3 CUBE . Explain both answers.: BAD QWERTY ; and then BAD. What did SuperForth keep?7.1 Define DOUBLE so that 21 DOUBLE . prints 42.
7.2 Define AVERAGE ( n1 n2 -- n ) so that 10 20 AVERAGE . prints 15.
7.3 Define GREET so that typing GREET prints HELLO with your name after it.
7.4 Define SUM-OF-SQUARES ( n1 n2 -- n ) using SQUARE, so that 3 4 SUM-OF-SQUARES . prints 25. You will need one word from S6.
Worked solutions are in Appendix II.
| Symptom | Cause |
|---|---|
;? Compilation only, Use in Definition | The line was over 40 characters, so its beginning - with the colon - was never read. Split the definition over two lines. |
ok, but the word you just defined is answered with a ? | The line was exactly 40 characters and was lost whole. Split it. |
No ok after a line | You are inside a definition and SuperForth is waiting for the ;. |
NAME Isn't Unique | You have defined that name before. It is a warning, not a refusal. |
| You fixed a word and the words built on it have not changed | They keep the version they were built with. Define them again too. |
:SQUARE? | No space after the colon. SuperForth read :SQUARE as one word and went looking for it. |
: NAME ... ; adds a word, and the new word is as good as any SuperForth came with, including as a part of other words. Nothing runs when you define. Keep typed lines under 40 characters and split long definitions across lines. Define a name twice and the new meaning applies from then on, but not to words already built on the old one. Nothing you define survives leaving SuperForth - yet.
S8, Words Made Of Words - building something bigger out of small words, seeing what SuperForth has in its dictionary, and taking words out again.