Obviously, the stack is only suited for temporary storage. Furthermore, we have already seen that using the stack too much in our code can make it cryptic. It would be very nice if we had other places to keep things... pigeonholes with little nametages, perhaps?
Our simple-minded little helper MUF is very good at "put that there" and "get item X" sorts of tasks, so this does not seem too much to expect. In fact, MUF provides two types of variables: 'global' variables and 'local' variables. (Well, there's more to the story, as we shall find when we get to 'packages', but that's enough to explain just now.)
Global variables are so called because they are visible everywhere (in the current package, at least) and in particular are visible in every function (again, in the current package, at least).
Local variables, by contrast, exist only while the particular function they are confined within is actually running -- a tiny fraction of a second, usually -- and are visible only to that particular function. Such fleeting existence might seem almost pointless at first blush, but in fact it turns out to be quite handy for functions to have private storage which they can scribble all over without upsetting anyone else.
Let's start with global variables. Notice that the next few examples have arrows with two hyphens in their name!
Stack: "TestString" --> x Stack: x Stack: "TestString" x x x Stack: "TestString" "TestString" "TestString" "TestString"
--> operator takes one value off the stack, and
sticks it in the variable you specify. If the variable
doesn't exist, it is created. Thereafter, mentioning the
variable's name is equivalent to typing in the corresponding
You may change the value of a variable at any time using
Stack: 1 --> x Stack: x Stack: 1 2 --> x Stack: 1 x Stack: 1 2
(Later, we'll see that MUF has many things other than strings and numbers for us to store in variables: vectors, lists, objects ...)
Local variables work much like global variables, but they have one less hyphen in their name, and usually make sense only inside the definition of a word, since they live only as long as their word is running:
Stack: : double -> string string string join ; Stack: "trouble" double Stack: "troubletrouble" string **** Sorry: Unrecognized identifier: 'string'
double takes a string and returns one
twice as long, by joining two copies of it. It works by
first popping the given string off the stack and into a local
string, then by mentioning the variable
string twice and feeding the resulting two pointers to
Hint: It is a good idea to begin all your functions by copying all the stack values you intend to use into local variables: This lets you give them meaningful names, and usually makes the rest of the function much easier for humans to read.
Hint: Whenever you have a choice, it is better to use a
local variable than a global variable. You don't know what
other functions might be depending on the value of the
global variable, but you certainly know no other functions
depend on the value of a local variable. (Local variables
are also a bit faster.) This is why we give local variables
a slightly shorter assignment symbol (
->) than global
-->): humans are lazy and tend to use the
shortest-named function by preference.
Go to the first, previous, next, last section, table of contents.