LPC Basics Written by Descartes of Borg first edition: 23 april 1993 second edition: 17 june 1993
There are two methods in which translation is done: compilation and interpretation. These simply are differences betweem when the programming language is translated into computer language. With compiled languages, the programmer writes the code then uses a program called a compiler to translate the program into the computer's language. This translation occurs before the program is run. With interpreted languages however, the process of translation occurs as the program is being run. Since the translation of the program is occurring during the time of the program's running in interpreted languages, interpreted languages make much slower programs than compiled languages.
The bottom line is, no matter what language you are writing in, at some point this has to be changed into 0's and 1's which can be understood by the computer. But the variables which you store in memory are not simply 0's and 1's. So you have to have a way in your programming languages of telling the computer whether or not the 0's and 1's should be treated as decimal numbers or characters or strings or anything else. You do this through the use of data types.
For example, say you have a variable which you call 'x' and you give it the decimal whole number value 65. In LPC you would do this through the statement:
----- x = 65; -----You can later do things like:
_____ write(x+"\n"); /* \n is symbolically represents a carriage return */ y = x + 5; -----The first line allows you to send 65 and a carriage return to someone's screen. The second line lets you set the value of y to 70. The problem for the computer is that it does not know what '65' means when you tell it x = 65;. What you think of 65, it might think of as:
00000000000000000000000001000001But, also, to the computer, the letter 'A' is represented as:
00000000000000000000000001000001So, whenever you instruct the computer write(x+"\n");, it must have some way of knowing that you want to see '65' and not 'A'.
The computer can tell the difference between '65' and 'A' through the use of data types. A data types simply says what type of data is being stored by the memory location pointed to by a given variable. Thus, each LPC variable has a variable type which guides conversions. In the example given above, you would have had the following line somewhere in the code *before* the lines shown above:
----- int x; -----This one line tells the driver that whatever value x points to, it will be used as the data type "int", which is short for integer, or whole number. So you have a basic introduction into the reason why data types exist. They exist so the driver can make sense of the 0's and 1's that the computer is storing in memory.
void, status, int, string, object, int *, string *, object *, mixed *
Many drivers, but not all have the following important data types which are important to discuss:
float, mapping, float *, mapping *
And there are a few drivers with the following rarely used data types which are not important to discuss:
function, enum, struct, char
An int is any whole number. Thus 1, 42, -17, 0, -10000023 are all type int. A string is one or more alphanumeric characters. Thus "a", "we are borg", "42", "This is a string" are all strings. Note that strings are always enclosed in "" to allow the driver to distinguish between the int 42 and the string "42" as well as to distinguish between variable names (like x) and strings by the same names (like "x").
When you use a variable in code, you must first let the driver know what type of data to which that variable points. This process is called *declaration*. You do this at the beginning of the function or at the beginning of the object code (outside of functions before all functions which use it). This is done by placing the name of the data type before the name of the variable like in the following example:
----- void add_two_and_two() { int x; int y; x = 2; y = x + x; } -----Now, this is a complete function. The name of the function is add_two_and_two(). The function begins with the declaration of an int variable named x followed by the declaration of an in variable named y. So now, at this point, the driver now has two variables which point to NULL values, and it expects what ever values end up there to be of type int.
A note about the data types void and status: Void is a trivial data type which points to nothing. It is not used with respect to variables, but instead with respect to functions. You will come to understand this better later. For now, you need only understand that it points to no value.
The data type status is a boolean data type. That is, it can only have 1 or 0 as a value. This is often referred to as being true or false.