Phantasmal MUD Lib for DGD

Phantasmal Site > DGD > DGD Reference Manual (legacy) > Variables and Arithmetic Expressions

3.2 Variables and Arithmetic Expressions

The code command gives you constants like ST_VERSION. It also gives you several other things, including a set of one-letter variables which it defines for you.

A variable is a named piece of storage space. A variable can hold a value, such as the integer 94 or the character string "Bob's your uncle!" (an integer is a whole number, either positive or negative, and a character string is a series of letters, numbers and punctuation like a word or a sentence). The variable has a name that you use to refer to that storage. The value of the variable may change over time. For instance, you may add 2 to the variable, giving 96 instead of 94. But the variable name will not change.

For instance, type code a = 94, a = a + 7, a. Make sure to type it as written, with the commas. You should see a result like $7 = 101. The commas are a special operator that means "do the things in order, but only worry about the value of the last one". So we did three things with that code statement. We set the variable a to have the value 94. Then we changed what a was equal to by setting it to its old value plus 7. Then, the very last part of the command, after the last comma, was a. Since comma means " only pay attention to the last one ", the value returned was a's new value of 94 + 7, or 101.

You can also separate statements with the semicolon. Due to strangeness in the way the code command works, if you use the semicolon operator with the code command at all, you must finish your code with a semicolon or an end-curly-brace. Otherwise you'll get very strange behavior indeed. Try typing code a = 94; a = a + 7; return a;. You should get 101, the same result as above. If you just get 94, you forgot the ending semicolon. Try it again. Note the 'return' statement at the end. When you don't use a semicolon at the end of your code command, the Kernel Library assumes you're just writing a little expression and you want its value. That's what you've been doing so far. If you end your code command with a semicolon or an end-curly-brace then you'll need to include your own return statement if you want anything returned. The return statement does what the last statement after the comma did in the last paragraph. Its value is what gets returned, and so that value is what gets assigned to a history variable like $9 or $26.

A variable in LPC isn't exactly like a mathematical variable. The equals sign above doesn't really mean equality in a mathematical sense. Instead, the equals is often called assignment. It is setting the variable to have a value. But unlike in mathematics, the variable can be repeatedly assigned, changing its value throughout your code. A variable's value is the most recent value that has been assigned to it.

You've already seen the + operator. There's also a minus, - which works the way you'd expect — it can be used to make a number or variable negative, or to subtract two numbers. The asterisk * means multiplication, and the forward-slash, / represents division. Remember that you can only add, multiply, subtract or divide integers with integers and floating point numbers (DGD speak for decimal numbers) with other floating point numbers. If you try to do otherwise, such as by typing code 7 + 4.1, you'll get an error. Try it and see.

Most of DGD's operations give exactly the answer you'd expect, but division of integers sometimes doesn't. If you divide, say, five by two, you'll find that DGD automatically rounds down to 2. DGD will always round toward zero when you divide integers. Test with the code command and see.

Integers are signed, and limited in size. If you try typing code 10000 * 10000 * 10000, you'll see a strange result — it's negative! That's because integers have a maximum value of a little more than two billion. If your numbers get larger than that, they'll wrap back around. Integer variables will wrap to negative numbers once they hit about two billion. If you're subtracting or multiplying and your integer variable gets smaller than the smallest allowed number (around negative two billion) it will wrap back into being positive. This wrapping is because DGD's basic numbers are of a fixed size, and there are only so many possible numbers that a single variable can represent. If you need larger or smaller integers than that, you'll find that DGD's Arbitrary-Precision Signed Numbers (called ASNs) are the way to go. They're explained in a later chapter.

The code command gives you a set of variables with names like a, b, c, et cetera. You can also declare your own new ones. Integer variables are declared to be of type int. So you might type code int sam; sam = 7; sam = sam + 4; return sam;. Note that you must declare the variables at the very beginning. If you try typing code a = 4; int sam; sam = 7;, you'll get an error. Variables can only be declared at the beginning of a block scope. What that means to you right now is that if you use variables in your code statements, put them right up front.

A note for users of the C language: in C, you could say int sam = 7; instead of int sam; sam = 7; and it would work fine. DGD doesn't allow you to declare and initialize a variable in that way, so don't.

Not every value or variable is an integer, so not every variable is declared using int. Also, unlike in C, there is no such thing as an unsigned integer in DGD. This will give you a big fat syntax error if you try.

Remember that decimal numbers or fractional numbers are called floating point numbers. A floating point variable called temperature would be declared as float temperature;. Remember that you can only add, subtract, multiply and divide floating point numbers with other floating point numbers, not with integers. That means that to double a floating point number, you'd need to multiply it by 2.0 rather than just 2.

<— Prev
Getting Started
Up
A Quick LPC Tutorial
Next —>
Looping and Iteration