Knowledge

Local variable

Source đź“ť

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

Index

Static local variable
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

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

↑