Knowledge

Local variable

Source đź“ť

90:, though lexical (static) scoping is far more common. In lexical scoping (or lexical scope; also called static scoping or static scope), if a variable name's scope is a certain block, then its scope is the program text of the block definition: within that block's text, the variable name exists, and is bound to the variable's value, but outside that block's text, the variable name does not exist. By contrast, in dynamic scoping (or dynamic scope), if a variable name's scope is a certain block, then its scope is that block and all functions transitively called by that block (except when overridden again by another declaration); after the block ends, the variable name does not exist. Some languages, like 266:
value to a global (package) variable, which lasts until the end of the enclosing block. However, the variable is visible to any function called from within the block. To create lexically-scoped local variables, use the
58:-derived languages allow any number of nested levels of visibility, with private variables, functions, constants and types hidden within them, either by nested blocks or 98:, allow the programmer to choose static or dynamic scoping when defining or redefining a variable. Examples of languages that use dynamic scoping include 447:(while in perl all scalar values have a $ in front). Note that Ruby only provides built-in support for statically-scoped local variables like Perl's 166:
semantics allow the parameters to act as aliases of the values passed as arguments, allowing the subroutine to modify variables outside its own scope.
439:
as a language was inspired also by Perl, but in this case, the notation was made simpler: a global variable name must be preceded by a $ sign, like
154:
passed to it. In most languages, these local parameters are treated the same as other local variables within the subroutine. In contrast,
254:
is used for local lexically-scoped variables. Since dynamic scoping is less common today, the Perl documentation warns that "
594: 560: 138: 71: 130: 436: 99: 520: 263: 203: 179: 534: 202:
with local scope. In this case, recursive calls to the function also have access to the (single,
206:) variable. In all of the above languages, static variables are declared as such with a special 218: 63: 28: 488: 137:. Hence variables of this scope can be declared, written to, and read, without any risk of 47: 8: 198:) which allows a value to be retained from one call of the function to another – it is a 191: 118:)'s "local" declaration. Most other languages provide lexically scoped local variables. 87: 67: 43: 34: 500:(as of 2009). In particular, see section 6.2.4 “Storage durations of objects”, page 32. 469: 122: 455:. There is at least one library for Ruby that provides dynamically-scoped variables. 556: 156: 151: 107: 229: 51: 20: 133:
calls itself, local variables in each instance of the function are given distinct
16:
Computer programming, a variable only usable in a portion of a program (the scope)
550: 464: 199: 75: 59: 46:
in which it is declared overrides the same variable name in the larger scope. In
509: 222: 134: 221:, because their value remains in memory for the life of the program, but have 588: 146: 187: 162: 111: 443:, while a local variable has simply no $ sign in front of its name, like 246:
supports both dynamic and lexically-scoped local variables. The keyword
95: 50:
with only two levels of visibility, local variables are contrasted with
126: 103: 39: 150:
semantics provide a called subroutine with its own local copy of the
548: 383:
This happens since the global variable $ a is modified to a new
575:
Conrad Irwin. "LSpace: Dynamic scope for Ruby". December 2012
410:
variable would be limited to the static scope of the function
141:
to functions outside of the block in which they are declared.
576: 391:, but the global value is restored upon leaving the scope of 235:, which has several different meanings in various languages. 183: 55: 258:
isn't what most people think of as “local”.". Instead, the
250:
is used to define local dynamically-scoped variables, while
217:
Static locals in global functions have the same lifetime as
420:
Randal L. Schwartz and Tom Phoenix argue that the operator
243: 115: 91: 195: 70:: variables of local scope are used to avoid issues with 406:
would have printed 1 three times since in that case the
274:
To understand how it works consider the following code:
225:(not global scope), as with automatic local variables. 451:, not dynamically-scoped local variables like Perl's 178:
is available in many mainstream languages (including
549:Randal L. Schwartz and Tom Phoenix (2001-07-01). 535:perldoc.perl.org: perlsub: Private Variables via 586: 521:perldoc.perl.org: perlsub: Temporary Values via 86:Local variables may have a lexical or dynamic 174:A special type of local variable, called a 431: 238: 228:This is distinct from other usages of the 169: 121:In most languages, local variables are 587: 424:should have had a different name like 62:. Local variables are fundamental to 38:. A local variable reference in the 13: 144:Programming languages that employ 14: 606: 129:directly. This means that when a 569: 542: 528: 514: 503: 481: 1: 475: 114:, and the MirBSD Korn shell ( 7: 595:Variable (computer science) 555:. O'REILLY. paragraph 4.7. 458: 262:keyword gives a temporary, 10: 611: 577:http://cirw.in/blog/lspace 106:, and the shell languages 54:. On the other hand, many 552:Learning Perl 3rd edition 415: 411: 392: 388: 402:in this case instead of 276: 81: 510:perldoc.perl.org: local 432:Local variables in Ruby 387:(local) meaning inside 239:Local variables in Perl 219:static global variables 170:Static local variables 64:procedural programming 579:Retrieved 2013-10-16. 66:, and more generally 48:programming languages 489:"Current C standard" 204:statically allocated 74:that can occur with 123:automatic variables 68:modular programming 470:Non-local variable 377:this will output: 271:operator instead. 264:dynamically-scoped 131:recursive function 348:"$ a\n" 157:call by reference 602: 580: 573: 567: 566: 546: 540: 538: 532: 526: 524: 518: 512: 507: 501: 499: 495: 493: 485: 454: 450: 446: 442: 427: 423: 417: 414:and not seen by 413: 409: 405: 401: 394: 390: 373: 370: 367: 364: 361: 358: 355: 352: 349: 346: 343: 340: 337: 334: 331: 328: 325: 322: 319: 316: 313: 310: 307: 304: 301: 298: 295: 292: 289: 286: 283: 280: 270: 261: 257: 253: 249: 232: 213: 76:global variables 60:nested functions 52:global variables 21:computer science 610: 609: 605: 604: 603: 601: 600: 599: 585: 584: 583: 574: 570: 563: 547: 543: 536: 533: 529: 522: 519: 515: 508: 504: 497: 491: 487: 486: 482: 478: 465:Global variable 461: 452: 448: 444: 441:$ variable_name 440: 434: 425: 421: 419: 407: 403: 399: 381: 375: 374: 371: 368: 365: 362: 359: 356: 353: 350: 347: 344: 341: 338: 335: 332: 329: 326: 323: 320: 317: 314: 311: 308: 305: 302: 299: 296: 293: 290: 287: 284: 281: 278: 268: 259: 255: 251: 247: 241: 230: 211: 210:keyword (e.g., 200:static variable 172: 84: 17: 12: 11: 5: 608: 598: 597: 582: 581: 568: 561: 541: 527: 513: 502: 498:(3.61 MB) 479: 477: 474: 473: 472: 467: 460: 457: 433: 430: 379: 277: 240: 237: 223:function scope 171: 168: 125:stored on the 83: 80: 31:that is given 25:local variable 15: 9: 6: 4: 3: 2: 607: 596: 593: 592: 590: 578: 572: 564: 562:0-596-00132-0 558: 554: 553: 545: 539: 531: 525: 517: 511: 506: 490: 484: 480: 471: 468: 466: 463: 462: 456: 445:variable_name 438: 429: 396: 386: 378: 275: 272: 265: 245: 236: 234: 226: 224: 220: 215: 209: 208:storage class 205: 201: 197: 193: 189: 185: 181: 177: 176:static local, 167: 165: 164: 159: 158: 153: 149: 148: 147:call by value 142: 140: 136: 132: 128: 124: 119: 117: 113: 109: 105: 101: 97: 93: 89: 79: 77: 73: 69: 65: 61: 57: 53: 49: 45: 41: 37: 36: 30: 26: 22: 571: 551: 544: 530: 516: 505: 483: 435: 397: 384: 382: 376: 273: 242: 227: 216: 207: 188:Visual Basic 175: 173: 163:call by name 161: 155: 145: 143: 139:side-effects 120: 85: 72:side-effects 32: 24: 18: 96:Common Lisp 476:References 127:call stack 104:Emacs lisp 385:temporary 152:arguments 135:addresses 589:Category 459:See also 40:function 29:variable 523:local() 233:keyword 559:  496:  398:Using 380:1 2 1 231:static 212:static 192:VB.NET 33:local 492:(PDF) 453:local 422:local 404:local 345:print 303:local 260:local 256:local 248:local 88:scope 82:Scope 56:ALGOL 44:block 35:scope 27:is a 557:ISBN 537:my() 437:Ruby 426:save 244:Perl 194:and 160:and 116:mksh 112:dash 108:bash 100:Logo 94:and 92:Perl 23:, a 416:g() 412:f() 408:$ a 393:f() 389:f() 372:(); 366:(); 360:(); 333:sub 327:(); 312:$ a 306:$ a 291:sub 279:$ a 214:). 196:PHP 184:C++ 42:or 19:In 591:: 449:my 428:. 400:my 395:. 339:() 297:() 269:my 252:my 190:, 186:, 110:, 102:, 78:. 565:. 494:. 418:. 369:g 363:f 357:g 354:} 351:; 342:{ 336:g 330:} 324:g 321:; 318:2 315:= 309:; 300:{ 294:f 288:; 285:1 282:= 182:/ 180:C

Index

computer science
variable
scope
function
block
programming languages
global variables
ALGOL
nested functions
procedural programming
modular programming
side-effects
global variables
scope
Perl
Common Lisp
Logo
Emacs lisp
bash
dash
mksh
automatic variables
call stack
recursive function
addresses
side-effects
call by value
arguments
call by reference
call by name

Text is available under the Creative Commons Attribution-ShareAlike License. Additional terms may apply.

↑