Re: Week 7 - Wednesday Discussion
Posted by: (IP Logged)
Date: November 13, 2007 01:56AM
13. What is a local variable? What is a global variable?
a local variable is a variable that is given local scope. Such variables are accessible only from the function or block in which it is declared. Local variables are contrasted with global variables.Local variables are special because in most languages they 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 separate memory address space. Hence variables of this scope can be declared, written to, and read, without any risk of side-effects to processes outside of the block in which they are declared.
a global variable is a variable that is accessible in every scope. Interaction mechanism with global variables are called global environment mechanisms. The global environment paradigm is contrasted with the local environment paradigm, where all variables are local with no shared memory.They are usually considered bad practice precisely because of their nonlocality: a global variable can potentially be modified from anywhere, and any part of the program may depend on it. A global variable therefore has an unlimited potential for creating mutual dependencies, and adding mutual dependencies increases complexity. See Action at a distance. However, in a few cases, global variables can be suitable for use. For example, they can be used to avoid having to pass frequently-used variables continuously throughout several functions.