Next: Operators
Prev: Frobs
C-- provides two kinds of variables, local variables and
object variables.
Local variables allow methods to temporarily store information.
To use a local variable in a method, declare the local variable's name
(an identifier) at the top of the method in a var declaration
(see Method Structure). The variable's name is then an expression
whose value is the contents of the variable. The variable initially
contains the integer 0.
To set the variable's contents, use an assignment statement, which has the following syntax:
variable = value;This assigns the value value to the variable variable. The values of a method's local variables are specific to the processing of a particular message call; when the method is invoked another time, all of its local variables begin with the value
0 again.
The following example method returns 6:
var a;Object variables store information for an indefinite period of time. You refer to object variables through parameters, which are defined on the current method's defining object (not the current object). As with local variables, you can retrieve the value of an object variable by writing the name of the parameter which refers to that variable, and you can assign to an object variable using an assignment statement. You can also use thea = 3; return a + 3;
get_var() and
set_var() functions to perform these operations. It is an error
of type ~paramnf to refer to a parameter which does not exist on
the current method's defining object. Object variables begin with the
value 0.