| Global Constants and Values | Lesson 9 | Contents |
Assigning recognizable names to numbers is a convenient shortcut, as you've seen with named input parameters and local variables. But as you saw, both of those kinds of names are local, they apply only to a very limited section of the program, inside a definition. But Mops also has a provision called Value for assigning readily identifiable names to numbers such that they can be used throughout a program.
Your program can contain many different values because you define each value by giving it a unique name and a number that it is to hold.
You define a value like this:
25 value Jane
In other words, the value named Jane is holding the number 25. To recall a value's number, all you do is type the value name, and a copy of the number is placed on the parameter stack.
Type:
Jane
and the number 25 is placed on the stack.
A value is essentially a global version of a local variable, and responds to similar operations. To store a different number in a value, you use the store arrow, like this:
37 -> Jane
which writes a 37 over the original number, 25. Or you can increment or decrement the number stored in a value name with the ++> or --> operations, like this:
17 ++> Jane
4 --> Jane
which adds 17 to the 37 that is already stored there, then subtracts 4. Of course you can also do a subtraction by incrementing the value by a negative number:
-10 ++> Jane
If you want to define your values at the beginning of a program without placing specific numbers in them, you can place zeros in them all, and then store (->) numbers to them when necessary:
0 value Joe
0 value Nancy
:
:
Note that the initial numbers you specify for you values are set up when your program is loaded by Mops. If you restart your application without reloading it, your values will still contain whatever you last put in them, not their initial numbers.
Commenting Programs
Notice too, that we've started adding plain English remarks about the code as a way of documenting the program. There are three ways of specifying comments in Mops:
( this kind of comment continues to a )
\ This is another comment, which extends to the end of the line
(* This kind of comment
can go over several lines,
(* and can be nested *)
*)
Note that (, \, (* and *) are Mops words, and so must be followed by a white space character. Thus if you had
(this isn't a comment)
Mops would try to recognize a word (this , and wouldn't treat this line as a comment. You'd probably get an "undefined word" error message.
| Previous Chapter | Contents | Next Chapter |
|---|---|---|
| ⇧ | ||
| This page online: http://PowerMops.com/MopsManual/Lessons/Chapter9.html | ||