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.
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:
19 static var1, var2, var3;
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
32 static b = a1 + sin(a2);
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
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.
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
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
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
64 static mat table[3] = {5, 6, 7};
65 local obj point p1, p2;
67 When working with user-defined functions, the syntax for passing an
68 lvalue by reference rather than by value is to precede an expression
69 for the lvalue by a backquote. For example, if the function invert is
72 define invert(x) {x = inverse(x)}
74 then invert(`A) achieves the effect of A = inverse(A). In other
75 words, passing and argument of `variable (with a back-quote)
76 will cause and changes to the function argument to be applied to
77 the calling variable. Calling invert(A) (without the ` backquote)
78 assigns inverse(A) to the temporary function parameter x and leaves
81 In an argument, a backquote before other than an lvalue is ignored.
82 Consider, for example:
84 ; define logplus(x,y,z) {return log(++x + ++y + ++z);}
88 ; answer = logplus(eh, `mi, `17);
90 ; print eh, mi, answer;
93 The value of eh is was not changed because eh was used as
94 an argument without a back-quote (`). However, mi was incremented
95 because it was passed as `mi (with a back-quote). Passing 17
96 (not an lvalue) as `17 has not effect on the value 17.
98 The back-quote should only be used before arguments to a function.
99 In all other contexts, a backquote causes a compile error.
101 Another method is to pass the address of the lvalue explicitly and
102 use the indirection operator * (star) to refer to the lvalue in the
103 function body. Consider the following function:
105 ; define ten(a) { *a = 10; }
120 Passing an argument with a & (ampersand) allows the tenmore()
121 function to modify the calling variable:
127 Great care should be taken when using a pointer to a local variable
128 or element of a matrix, list or object, since the lvalue pointed to
129 is deleted when evaluation of the function is completed or the lvalue
130 whose value is the matrix, list or object is assigned another value.
132 As both of the above methods (using & arguments (ampersand) *value
133 (star) function values or by using ` arguments (back quote) alone)
134 copy the address rather than the value of the argument to the function
135 parameter, they allow for faster calls of functions when the memory
136 required for the value is huge (such as for a large matrix).
138 As the built-in functions and object functions always accept their
139 arguments as addresses, there is no gain in using the backquote when
140 calling these functions.
142 ## Copyright (C) 1999-2006 Landon Curt Noll
144 ## Calc is open software; you can redistribute it and/or modify it under
145 ## the terms of the version 2.1 of the GNU Lesser General Public License
146 ## as published by the Free Software Foundation.
148 ## Calc is distributed in the hope that it will be useful, but WITHOUT
149 ## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
150 ## or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
151 ## Public License for more details.
153 ## A copy of version 2.1 of the GNU Lesser General Public License is
154 ## distributed with calc under the filename COPYING-LGPL. You should have
155 ## received a copy with calc; if not, write to Free Software Foundation, Inc.
156 ## 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
158 ## @(#) $Revision: 30.1 $
159 ## @(#) $Id: variable,v 30.1 2007/03/16 11:10:42 chongo Exp $
160 ## @(#) $Source: /usr/local/src/cmd/calc/help/RCS/variable,v $
162 ## Under source code control: 1991/07/21 04:37:25
163 ## File existed as early as: 1991
165 ## chongo <was here> /\oo/\ http://www.isthe.com/chongo/
166 ## Share and enjoy! :-) http://www.isthe.com/chongo/tech/comp/calc/