Scope is an enclosing context where values and expressions are associated. Use this tag for questions about different types of scope as well for questions where scope may be unclear.
Questions tagged [scope]
17524 questions
115
votes
8 answers
namespaces for enum types - best practices
Often, one needs several enumerated types together. Sometimes, one has a name clash. Two solutions to this come to mind: use a namespace, or use 'larger' enum element names. Still, the namespace solution has two possible implementations: a dummy…

xtofl
- 40,723
- 12
- 105
- 192
115
votes
6 answers
Why is it OK to return a 'vector' from a function?
Please consider this code. I have seen this type of code several times. words is a local vector. How is it possible to return it from a function?
Can we guarantee it will not die?
std::vector read_file(const std::string& path)
{
…

Pranit Kothari
- 9,721
- 10
- 61
- 137
111
votes
6 answers
Declaring and initializing variables within Java switches
I have a crazy question about Java switches.
int key = 2;
switch (key) {
case 1:
int value = 1;
break;
case 2:
value = 2;
System.out.println(value);
break;
default:
break;
}
Scenario 1 -…

ironwood
- 8,936
- 15
- 65
- 114
109
votes
2 answers
Return pointer to local struct
I see some code samples with constructs like this:
type point struct {
x, y int
}
func newPoint() *point {
return &point{10, 20}
}
I have C++ background and it seems like error for me. What are the semantic of such construct? Is new point…

demi
- 5,384
- 6
- 37
- 57
107
votes
2 answers
Accessing outside variable using anonymous function as params
Basically I use this handy function to processing db rows (close an eye on PDO and/or other stuff)
function fetch($query,$func) {
$query = mysql_query($query);
while($r = mysql_fetch_assoc($query)) {
$func($r);
}
}
With this…

dynamic
- 46,985
- 55
- 154
- 231
106
votes
7 answers
What is the 'global' object in NodeJS
I've just seen a weird behaviour of the this keyword in NodeJS environment. I'm listing them with code. I've run this code with NodeJS v6.x, with a single JavaScript file.
While testing with one line of code as follows, whether with or without the…

Arnab Das
- 3,548
- 8
- 31
- 43
105
votes
5 answers
Getting "global name 'foo' is not defined" with Python's timeit
I'm trying to find out how much time it takes to execute a Python statement, so I looked online and found that the standard library provides a module called timeit that purports to do exactly that:
import timeit
def foo():
# ... contains code I…

Kyle Cronin
- 77,653
- 43
- 148
- 164
103
votes
18 answers
Javascript function scoping and hoisting
I just read a great article about JavaScript Scoping and Hoisting by Ben Cherry in which he gives the following example:
var a = 1;
function b() {
a = 10;
return;
function a() {}
}
b();
alert(a);
Using the code above, the browser…

dev.e.loper
- 35,446
- 76
- 161
- 247
103
votes
3 answers
How should I use the "my" keyword in Perl?
I keep seeing the my keyword in front of variable names in example Perl scripts online, but I have no idea what it means. I tried reading the manual pages and other sites online, but I'm having difficulty discerning what it is for, given the…

FistOfFury
- 6,735
- 7
- 49
- 57
103
votes
10 answers
Access a global variable in a PHP function
According to the most programming languages scope rules, I can access variables that are defined outside of functions inside them, but why doesn't this code work?

Amin Gholibeigian
- 1,325
- 2
- 9
- 11
102
votes
6 answers
Giving my function access to outside variable
I have an array outside:
$myArr = array();
I would like to give my function access to the array outside it so it can add values to it
function someFuntion(){
$myVal = //some processing here to determine value of $myVal
$myArr[] =…

brett
- 1,749
- 3
- 16
- 13
101
votes
8 answers
Python: load variables in a dict into namespace
I want to use a bunch of local variables defined in a function, outside of the function. So I am passing x=locals() in the return value.
How can I load all the variables defined in that dictionary into the namespace outside the function, so that…

D R
- 21,936
- 38
- 112
- 149
100
votes
2 answers
Why is a variable value not available after add_subdirectory-ing a CMakeLists.txt that defines it? How can I make it so?
I have a CMakeLists.txt in my project root and one in my /src folder. The one in the /src folder only contains a variable with the .cpp files (set (SOURCEFILES main.cpp foo.cpp)) and in the root CMakeLists.txt I do add_subdirectory(src) and later I…

blubberbernd
- 3,641
- 8
- 35
- 46
99
votes
3 answers
ECMAScript 2015: const in for loops
Which of the two (or neither/ both) code fragments below should be working in a complete ECMAScript 2015 implementation:
for (const e of a)
for (const i = 0; i < a.length; i += 1)
From my understanding, the first example should work because e is…

adrianp
- 2,491
- 5
- 26
- 44
99
votes
8 answers
Setting dynamic scope variables in AngularJs - scope.
I have a string I have gotten from a routeParam or a directive attribute or whatever, and I want to create a variable on the scope based on this. So:
$scope. = "something".
However, if the string contains one or more dots I want to…

Erik Honn
- 7,576
- 5
- 33
- 42