focus expression text field at startup -> version 1.2
[LeanCalc.git] / help / VARIABLE
1 Variable declarations
2
3     Variables can be declared as either being global, local, or static.
4     Global variables are visible to all functions and on the command
5     line, and are permanent.  Local variables are visible only within
6     a single function or command sequence.  When the function or command
7     sequence returns, the local variables are deleted.  Static variables
8     are permanent like global variables, but are only visible within the
9     same input file or function where they are defined.
10
11     To declare one or more variables, the 'local', 'global', or 'static'
12     keywords are used, followed by the desired list of variable names,
13     separated by commas.  The definition is terminated with a semicolon.
14     Examples of declarations are:
15
16             local       x, y, z;
17             global      fred;
18             local       foo, bar;
19             static      var1, var2, var3;
20
21     Variables may have initializations applied to them.  This is done
22     by following the variable name by an equals sign and an expression.
23     Global and local variables are initialized each time that control
24     reaches them (e.g., at the entry to a function which contains them).
25     Static variables are initialized once only, at the time that control
26     first reaches them (but in future releases the time of initialization
27     may change).  Unlike in C, expressions for static variables may
28     contain function calls and refer to variables.  Examples of such
29     initializations are:
30
31             local       a1 = 7, a2 = 3;
32             static      b = a1 + sin(a2);
33
34     Within function declarations, all variables must be defined.
35     But on the top level command line, assignments automatically define
36     global variables as needed.  For example, on the top level command
37     line, the following defines the global variable x if it had not
38     already been defined:
39
40             x = 7
41
42     The static keyword may be used at the top level command level to
43     define a variable which is only accessible interactively, or within
44     functions defined interactively.
45
46     Variables have no fixed type, thus there is no need or way to
47     specify the types of variables as they are defined.  Instead, the
48     types of variables change as they are assigned to or are specified
49     in special statements such as 'mat' and 'obj'.  When a variable is
50     first defined using 'local', 'global', or 'static', it has the
51     value of zero.
52
53     If a procedure defines a local or static variable name which matches
54     a global variable name, or has a parameter name which matches a
55     global variable name, then the local variable or parameter takes
56     precedence within that procedure, and the global variable is not
57     directly accessible.
58
59     The MAT and OBJ keywords may be used within a declaration statement
60     in order to initially define variables as that type.  Initialization
61     of these variables are also allowed.  Examples of such declarations
62     are:
63
64             static mat table[3] = {5, 6, 7};
65             local obj point p1, p2;
66
67     There are no pointers in the calculator language, thus all
68     arguments to user-defined functions are normally passed by value.
69     This is true even for matrices, strings, and lists.  In order
70     to circumvent this, the '&' operator is allowed before a variable
71     when it is an argument to a function.  When this is done, the
72     address of the variable is passed to the function instead of its
73     value.  This is true no matter what the type of the variable is.
74     This allows for fast calls of functions when the passed variable
75     is huge (such as a large array).  However, the passed variable can
76     then be changed by the function if the parameter is assigned into.
77     The function being called does not need to know if the variable
78     is being passed by value or by address.
79
80     Built-in functions and object functions always accept their
81     arguments as addresses, thus there is no need to use '&' when
82     calling built-in functions.
83