Mercurial > octave-nkf
annotate doc/interpreter/var.txi @ 8566:da95767511f5
Remove descriptions of built-in variables from manual
author | sh@sh-laptop |
---|---|
date | Thu, 22 Jan 2009 11:06:56 -0500 |
parents | 68aa5abfd136 |
children | 674d00f5e072 |
rev | line source |
---|---|
7018 | 1 @c Copyright (C) 1996, 1997, 1999, 2000, 2002, 2003, 2004, 2005, |
2 @c 2006, 2007 John W. Eaton | |
3 @c | |
4 @c This file is part of Octave. | |
5 @c | |
6 @c Octave is free software; you can redistribute it and/or modify it | |
7 @c under the terms of the GNU General Public License as published by the | |
8 @c Free Software Foundation; either version 3 of the License, or (at | |
9 @c your option) any later version. | |
10 @c | |
11 @c Octave is distributed in the hope that it will be useful, but WITHOUT | |
12 @c ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
13 @c FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
14 @c for more details. | |
15 @c | |
16 @c You should have received a copy of the GNU General Public License | |
17 @c along with Octave; see the file COPYING. If not, see | |
18 @c <http://www.gnu.org/licenses/>. | |
3294 | 19 |
4167 | 20 @node Variables |
3294 | 21 @chapter Variables |
22 @cindex variables, user-defined | |
23 @cindex user-defined variables | |
24 | |
25 Variables let you give names to values and refer to them later. You have | |
26 already seen variables in many of the examples. The name of a variable | |
27 must be a sequence of letters, digits and underscores, but it may not begin | |
28 with a digit. Octave does not enforce a limit on the length of variable | |
29 names, but it is seldom useful to have variables with names longer than | |
30 about 30 characters. The following are all valid variable names | |
31 | |
32 @cindex job hunting | |
33 @cindex getting a good job | |
34 @cindex flying high and fast | |
35 @example | |
36 @group | |
37 x | |
38 x15 | |
39 __foo_bar_baz__ | |
40 fucnrdthsucngtagdjb | |
41 @end group | |
42 @end example | |
43 | |
44 @noindent | |
45 However, names like @code{__foo_bar_baz__} that begin and end with two | |
46 underscores are understood to be reserved for internal use by Octave. | |
47 You should not use them in code you write, except to access Octave's | |
48 documented internal variables and built-in symbolic constants. | |
49 | |
50 Case is significant in variable names. The symbols @code{a} and | |
51 @code{A} are distinct variables. | |
52 | |
53 A variable name is a valid expression by itself. It represents the | |
54 variable's current value. Variables are given new values with | |
55 @dfn{assignment operators} and @dfn{increment operators}. | |
56 @xref{Assignment Ops, ,Assignment Expressions}. | |
57 | |
8566
da95767511f5
Remove descriptions of built-in variables from manual
sh@sh-laptop
parents:
8519
diff
changeset
|
58 There is one built-in variable with a special meaning. The @code{ans} variable |
da95767511f5
Remove descriptions of built-in variables from manual
sh@sh-laptop
parents:
8519
diff
changeset
|
59 always contains the result of the last computation, where the output wasn't |
da95767511f5
Remove descriptions of built-in variables from manual
sh@sh-laptop
parents:
8519
diff
changeset
|
60 assigned to any variable. The code @code{a = cos (pi)} will assign the value -1 |
da95767511f5
Remove descriptions of built-in variables from manual
sh@sh-laptop
parents:
8519
diff
changeset
|
61 to the variable @code{a}, but will not change the value of @code{ans}. However, |
da95767511f5
Remove descriptions of built-in variables from manual
sh@sh-laptop
parents:
8519
diff
changeset
|
62 the code @code{cos (pi)} will set the value of @code{ans} to -1. |
3294 | 63 |
64 Variables in Octave do not have fixed types, so it is possible to first | |
65 store a numeric value in a variable and then to later use the same name | |
66 to hold a string value in the same program. Variables may not be used | |
67 before they have been given a value. Doing so results in an error. | |
68 | |
8566
da95767511f5
Remove descriptions of built-in variables from manual
sh@sh-laptop
parents:
8519
diff
changeset
|
69 @DOCSTRING(ans) |
da95767511f5
Remove descriptions of built-in variables from manual
sh@sh-laptop
parents:
8519
diff
changeset
|
70 |
6550 | 71 @DOCSTRING(isvarname) |
72 | |
7984
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7336
diff
changeset
|
73 @DOCSTRING(genvarname) |
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7336
diff
changeset
|
74 |
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7336
diff
changeset
|
75 @DOCSTRING(namelengthmax) |
bbaa5d7d0143
Some documentation updates
David Bateman <dbateman@free.fr>
parents:
7336
diff
changeset
|
76 |
3294 | 77 @menu |
78 * Global Variables:: | |
4686 | 79 * Persistent Variables:: |
3294 | 80 * Status of Variables:: |
81 * Summary of Built-in Variables:: | |
82 * Defaults from the Environment:: | |
83 @end menu | |
84 | |
4167 | 85 @node Global Variables |
3294 | 86 @section Global Variables |
87 @cindex global variables | |
88 @cindex @code{global} statement | |
89 @cindex variables, global | |
90 | |
91 A variable that has been declared @dfn{global} may be accessed from | |
92 within a function body without having to pass it as a formal parameter. | |
93 | |
94 A variable may be declared global using a @code{global} declaration | |
95 statement. The following statements are all global declarations. | |
96 | |
97 @example | |
98 @group | |
99 global a | |
4504 | 100 global a b |
101 global c = 2 | |
102 global d = 3 e f = 5 | |
3294 | 103 @end group |
104 @end example | |
105 | |
4504 | 106 A global variable may only be initialized once in a @code{global} |
107 statement. For example, after executing the following code | |
108 | |
109 @example | |
110 @group | |
111 global gvar = 1 | |
112 global gvar = 2 | |
113 @end group | |
114 @end example | |
115 | |
116 @noindent | |
6077 | 117 the value of the global variable @code{gvar} is 1, not 2. Issuing a |
6623 | 118 @samp{clear gvar} command does not change the above behavior, but |
6077 | 119 @samp{clear all} does. |
4504 | 120 |
3294 | 121 It is necessary declare a variable as global within a function body in |
122 order to access it. For example, | |
123 | |
124 @example | |
125 @group | |
126 global x | |
127 function f () | |
128 x = 1; | |
129 endfunction | |
130 f () | |
131 @end group | |
132 @end example | |
133 | |
134 @noindent | |
135 does @emph{not} set the value of the global variable @code{x} to 1. In | |
136 order to change the value of the global variable @code{x}, you must also | |
137 declare it to be global within the function body, like this | |
138 | |
139 @example | |
140 @group | |
141 function f () | |
142 global x; | |
143 x = 1; | |
144 endfunction | |
145 @end group | |
146 @end example | |
147 | |
148 Passing a global variable in a function parameter list will | |
149 make a local copy and not modify the global value. For example, given | |
150 the function | |
151 | |
152 @example | |
153 @group | |
154 function f (x) | |
155 x = 0 | |
156 endfunction | |
157 @end group | |
158 @end example | |
159 | |
160 @noindent | |
161 and the definition of @code{x} as a global variable at the top level, | |
162 | |
163 @example | |
164 global x = 13 | |
165 @end example | |
166 | |
167 @noindent | |
168 the expression | |
169 | |
170 @example | |
171 f (x) | |
172 @end example | |
173 | |
174 @noindent | |
175 will display the value of @code{x} from inside the function as 0, | |
176 but the value of @code{x} at the top level remains unchanged, because | |
177 the function works with a @emph{copy} of its argument. | |
178 | |
4029 | 179 @DOCSTRING(isglobal) |
3294 | 180 |
4686 | 181 @node Persistent Variables |
182 @section Persistent Variables | |
183 @cindex persistent variables | |
184 @cindex @code{persistent} statement | |
185 @cindex variables, persistent | |
8286
6f2d95255911
fix @seealso references to point to existing anchors
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8281
diff
changeset
|
186 @anchor{doc-persistent} |
4686 | 187 |
188 A variable that has been declared @dfn{persistent} within a function | |
189 will retain its contents in memory between subsequent calls to the | |
190 same function. The difference between persistent variables and global | |
191 variables is that persistent variables are local in scope to a | |
192 particular function and are not visible elsewhere. | |
193 | |
6899 | 194 The following example uses a persistent variable to create a function |
195 that prints the number of times it has been called. | |
196 | |
197 @example | |
198 @group | |
199 function count_calls () | |
200 persistent calls = 0; | |
7031 | 201 printf ("'count_calls' has been called %d times\n", |
202 ++calls); | |
6899 | 203 endfunction |
204 | |
205 for i = 1:3 | |
206 count_calls (); | |
207 endfor | |
208 | |
209 @print{} 'count_calls' has been called 1 times | |
210 @print{} 'count_calls' has been called 2 times | |
211 @print{} 'count_calls' has been called 3 times | |
212 @end group | |
213 @end example | |
214 | |
215 As the example shows, a variable may be declared persistent using a | |
216 @code{persistent} declaration statement. The following statements are | |
217 all persistent declarations. | |
4686 | 218 |
219 @example | |
220 @group | |
221 persistent a | |
222 persistent a b | |
223 persistent c = 2 | |
224 persistent d = 3 e f = 5 | |
225 @end group | |
226 @end example | |
227 | |
228 The behavior of persistent variables is equivalent to the behavior of | |
8481
00df69d7e698
[docs] capitalize Octave consistently
Brian Gough <bjg@gnu.org>
parents:
8347
diff
changeset
|
229 static variables in C. The command @code{static} in Octave is also |
6899 | 230 recognized and is equivalent to @code{persistent}. |
231 | |
232 Like global variables, a persistent variable may only be initialized once. | |
6896 | 233 For example, after executing the following code |
4686 | 234 |
235 @example | |
236 @group | |
237 persistent pvar = 1 | |
238 persistent pvar = 2 | |
239 @end group | |
240 @end example | |
241 | |
242 @noindent | |
6896 | 243 the value of the persistent variable @code{pvar} is 1, not 2. |
4686 | 244 |
6899 | 245 If a persistent variable is declared but not initialized to a specific |
246 value, it will contain an empty matrix. So, it is also possible to | |
247 initialize a persistent variable by checking whether it is empty, as the | |
248 following example illustrates. | |
249 | |
250 @example | |
251 @group | |
252 function count_calls () | |
253 persistent calls; | |
254 if (isempty (calls)) | |
255 calls = 0; | |
256 endif | |
7031 | 257 printf ("'count_calls' has been called %d times\n", |
258 ++calls); | |
6899 | 259 endfunction |
260 @end group | |
261 @end example | |
262 | |
263 @noindent | |
264 This implementation behaves in exactly the same way as the previous | |
265 implementation of @code{count_calls}. | |
266 | |
267 The value of a persistent variable is kept in memory until it is | |
268 explicitly cleared. Assuming that the implementation of @code{count_calls} | |
269 is saved on disc, we get the following behaviour. | |
270 | |
271 @example | |
272 @group | |
273 for i = 1:2 | |
274 count_calls (); | |
275 endfor | |
276 @print{} 'count_calls' has been called 1 times | |
277 @print{} 'count_calls' has been called 2 times | |
278 | |
279 clear | |
280 for i = 1:2 | |
281 count_calls(); | |
282 endfor | |
283 @print{} 'count_calls' has been called 3 times | |
284 @print{} 'count_calls' has been called 4 times | |
285 | |
286 clear all | |
287 for i = 1:2 | |
288 count_calls(); | |
289 endfor | |
290 @print{} 'count_calls' has been called 1 times | |
291 @print{} 'count_calls' has been called 2 times | |
292 | |
293 clear count_calls | |
294 for i = 1:2 | |
295 count_calls(); | |
296 endfor | |
297 @print{} 'count_calls' has been called 1 times | |
298 @print{} 'count_calls' has been called 2 times | |
299 @end group | |
300 @end example | |
301 | |
302 @noindent | |
303 That is, the persistent variable is only removed from memory when the | |
304 function containing the variable is removed. Note that if the function | |
305 definition is typed directly into the Octave prompt, the persistent | |
306 variable will be cleared by a simple @code{clear} command as the entire | |
307 function definition will be removed from memory. If you do not want | |
308 a persistent variable to be removed from memory even if the function is | |
309 cleared, you should use the @code{mlock} function as described in | |
310 @xref{Function Locking}. | |
311 | |
4167 | 312 @node Status of Variables |
3294 | 313 @section Status of Variables |
314 | |
6623 | 315 When creating simple one-shot programs it can be very convenient to |
316 see which variables are available at the prompt. The function @code{who} | |
317 and its siblings @code{whos} and @code{whos_line_format} will show | |
318 different information about what is in memory, as the following shows. | |
319 | |
320 @example | |
321 str = "A random string"; | |
322 who -variables | |
323 @print{} *** local user variables: | |
324 @print{} | |
325 @print{} __nargin__ str | |
326 @end example | |
3294 | 327 |
3361 | 328 @DOCSTRING(who) |
3294 | 329 |
4913 | 330 @DOCSTRING(whos) |
331 | |
332 @DOCSTRING(whos_line_format) | |
333 | |
6623 | 334 Instead of displaying which variables are in memory, it is possible |
335 to determine if a given variable is available. That way it is possible | |
336 to alter the behaviour of a program depending on the existence of a | |
337 variable. The following example illustrates this. | |
338 | |
339 @example | |
340 if (! exist ("meaning", "var")) | |
341 disp ("The program has no 'meaning'"); | |
342 endif | |
343 @end example | |
344 | |
3361 | 345 @DOCSTRING(exist) |
3294 | 346 |
6623 | 347 Usually Octave will manage the memory, but sometimes it can be practical |
348 to remove variables from memory manually. This is usually needed when | |
349 working with large variables that fill a substantial part of the memory. | |
350 On a computer that uses the IEEE floating point format, the following | |
351 program allocates a matrix that requires around 128 MB memory. | |
352 | |
353 @example | |
354 large_matrix = zeros (4000, 4000); | |
355 @end example | |
356 | |
357 @noindent | |
358 Since having this variable in memory might slow down other computations, | |
359 it can be necessary to remove it manually from memory. The @code{clear} | |
360 function allows this. | |
361 | |
362 @DOCSTRING(clear) | |
363 | |
8347
fa78cb8d8a5c
corrections for typos
Brian Gough<bjg@network-theory.co.uk>
parents:
8286
diff
changeset
|
364 Information about a function or variable such as its location in the |
6623 | 365 file system can also be acquired from within Octave. This is usually |
366 only useful during development of programs, and not within a program. | |
367 | |
3361 | 368 @DOCSTRING(type) |
3294 | 369 |
3361 | 370 @DOCSTRING(which) |
3294 | 371 |