赛派号

品牌箱包排名榜 Local variable

Computer programming, a variable only usable in a portion of a program (the scope) This article needs to be updated. Please help update this article to reflect recent events or newly ailable information. (November 2025)

In computer science, a local variable is a variable that is given local scope. A local variable reference in the function or block in which it is declared overrides the same variable name in the larger scope. In programming languages with only two levels of visibility, local variables are contrasted with global variables. On the other hand, many ALGOL-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 nested functions. Local variables are fundamental to procedural programming, and more generally modular programming: variables of local scope are used to oid issues with side-effects that can occur with global variables.

Scope[edit]

Local variables may he a lexical or dynamic scope, 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 Perl and Common Lisp, allow the programmer to choose static or dynamic scoping when defining or redefining a variable. Examples of languages that use dynamic scoping include Logo, Emacs lisp, and the shell languages bash, dash, and the MirBSD Korn shell (mksh)'s "local" declaration. Most other languages provide lexically scoped local variables.

In most languages, local variables are automatic variables stored on the call stack directly. This means that when a recursive function calls itself, local variables in each instance of the function are given distinct addresses. Hence variables of this scope can be declared, written to, and read, without any risk of side-effects to functions outside of the block in which they are declared.

Programming languages that employ call by value semantics provide a called subroutine with its own local copy of the arguments passed to it. In most languages, these local parameters are treated the same as other local variables within the subroutine. In contrast, call by reference and call by name semantics allow the parameters to act as aliases of the values passed as arguments, allowing the subroutine to modify variables outside its own scope.

Static local variables[edit]

A special type of local variable, called a static local, is ailable in many mainstream languages (including C/C++, Visual Basic, VB.NET and PHP) which allows a value to be retained from one call of the function to another – it is a static variable with local scope. In this case, recursive calls to the function also he access to the (single, statically allocated) variable. In all of the above languages, static variables are declared as such with a special storage class keyword (e.g., static).

Static locals in global functions he the same lifetime as static global variables, because their value remains in memory for the life of the program,[1] but he function scope (not global scope), as with automatic local variables.

This is distinct from other usages of the static keyword, which has several different meanings in various languages.

Local variables in Perl[edit]

Perl supports both dynamic and lexically-scoped local variables. The keyword local is used to define local dynamically-scoped variables, while my is used for local lexically-scoped variables. Since dynamic scoping is less common today, the Perl documentation warns that "local isn't what most people think of as “local”.".[2] Instead, the local keyword gives a temporary, dynamically-scoped 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.[3] To create lexically-scoped local variables, use the my operator instead.[4]

To understand how it works consider the following code:

$a = 1; sub f() { local $a; $a = 2; g(); } sub g() { print "$a\n"; } g(); f(); g();

this will output:

1 2 1

This happens since the global variable $a is modified to a new temporary (local) meaning inside f(), but the global value is restored upon leing the scope of f().

Using my in this case instead of local would he printed 1 three times since in that case the $a variable would be limited to the static scope of the function f() and not seen by g(). Randal L. Schwartz and Tom Phoenix argue that the operator local should he had a different name like se.[5]

Local variables in Ruby[edit]

Ruby 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 $variable_name, while a local variable has simply no $ sign in front of its name, like variable_name (while in perl all scalar values he a $ in front). Note that Ruby only provides built-in support for statically-scoped local variables like Perl's my, not dynamically-scoped local variables like Perl's local. There is at least one library for Ruby that provides dynamically-scoped variables. [6]

See also[edit] Global variable Non-local variable References[edit] ^ "Current C standard" (PDF). (3.61 MB) (as of 2009[update]). In particular, see section 6.2.4 “Storage durations of objects”, page 32. ^ perldoc.perl.org: local ^ perldoc.perl.org: perlsub: Temporary Values via local() ^ perldoc.perl.org: perlsub: Private Variables via my() ^ Randal L. Schwartz and Tom Phoenix (2001-07-01). Learning Perl 3rd edition. O'REILLY. paragraph 4.7. ISBN 0-596-00132-0. ^ Conrad Irwin. "LSpace: Dynamic scope for Ruby". December 2012 http://cirw.in/blog/lspace Retrieved 2013-10-16.

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至lsinopec@gmail.com举报,一经查实,本站将立刻删除。

上一篇 没有了

下一篇没有了