Geas MUD, enter the Aventure!
Geas Home | Play the Game

[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

1.2.3 Variable declarations

A variable is a string of letters identifying an information 'box', a place to store data. The box is given a name consisting of a maximum of 32 characters, starting with an alphabetic letter. Custom and common sense dictate that all variables used inside a function consist of lowercase letters only. Global variables have the first letter in uppercase, the rest lowercase. No special character other than the '_' used to separate words is ever used. Variables should always be given names that reflect on their use. You declare variables like this:

 
<data type> <variable name>, <another variable>, ..., <last variable>;
e.g.
    int        counter;
    float      height, weight;
    mapping    age_map;

Variables must be declared at the beginning of a block (right after the first '{') and before any code statements. Global variables, variables that are available in all functions throughout the program, should be declared at the top of the file.

When the declarations are executed as the program runs, they are initially set to 0, NOT to their 'null-state' values. In other words for example mappings, arrays and strings will all be set to 0 and not to ([]), ({}) and "" as you might believe. It is possible to initialize variables in the declaration statement, and it's even a very good habit always to initialize arrays and mappings there:

 
<data type> <variable name> = <value>, etc.
e.g.
    int        counter = 8;
    float      height = 3.0, weight = 1.2;
    mapping    age_map = ([]);
    object     *monsters = ({});

The reason why arrays and mappings should be initialized in the declaration statement to their 'NULL' values (({}) and ([]) respectively) is that otherwise they are initialized to 0, which is incompatible with the proper type of the variable and might cause problems later on in the function they are part of.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

This document was generated by Ronny Wikh on July, 8 2003 using texi2html