Questions tagged [global-variables]

Global variables are variables that are accessible from all elements of a software component.

Global variables are variables that are accessible from all elements of a software component.

Global variables are used to share information between elements of a component and to store a part of the component's state. They therfore:

  • create a hidden coupling between all the elements that use them;
  • are a source of accidental bugs and inconsistencies, when changes and side-effects are not sufficiently controlled.
7914 questions
3378
votes
21 answers

Using global variables in a function

How can I create or use a global variable in a function? If I create a global variable in one function, how can I use that global variable in another function? Do I need to store the global variable in a local variable of the function which needs…
user46646
  • 133,483
  • 43
  • 73
  • 82
1087
votes
17 answers

How do I use extern to share variables between source files?

I know that global variables in C sometimes have the extern keyword. What is an extern variable? What is the declaration like? What is its scope? This is related to sharing variables across source files, but how does that work precisely? Where do I…
shilpa
623
votes
12 answers

How to unset a JavaScript variable?

I have a global variable in JavaScript (actually a window property, but I don't think it matters) which was already populated by a previous script but I don't want another script that will run later to see its value or that it was even defined. I've…
Guss
  • 24,799
  • 13
  • 87
  • 109
603
votes
17 answers

How to declare global variables in Android?

I am creating an application which requires login. I created the main and the login activity. In the main activity onCreate method I added the following condition: public void onCreate(Bundle savedInstanceState) { …
Niko Gamulin
  • 63,517
  • 91
  • 213
  • 274
396
votes
5 answers

Do DOM tree elements with ids become global variables?

Working on an idea for a simple HTMLElement wrapper I stumbled upon the following for Internet Explorer and Chrome: For a given HTMLElement with ID in the DOM tree, it is possible to retrieve the div using its ID as the variable name. So for a div…
KooiInc
  • 104,388
  • 28
  • 131
  • 164
364
votes
3 answers

Printing all global variables/local variables?

How can I print all global variables/local variables? Is that possible in gdb?
cpuer
  • 6,413
  • 13
  • 32
  • 38
353
votes
12 answers

Global variables in AngularJS

I have a problem where i'm initialising a variable on the scope in a controller. Then it gets changed in another controller when a user logs in. This variable is used to control things such as the navigation bar and restricts access to parts of the…
Lightbulb1
  • 12,674
  • 6
  • 19
  • 23
309
votes
14 answers

Android global variable

How can I create global variable keep remain values around the life cycle of the application regardless which activity running.
d-man
  • 53,999
  • 81
  • 200
  • 285
307
votes
6 answers

Python function global variables?

I know I should avoid using global variables in the first place due to confusion like this, but if I were to use them, is the following a valid way to go about using them? (I am trying to call the global copy of a variable created in a separate…
Akshat Shekhar
  • 3,101
  • 2
  • 12
  • 4
303
votes
10 answers

Use of "global" keyword in Python

What I understand from reading the documentation is that Python has a separate namespace for functions, and if I want to use a global variable in that function, I need to use global. I'm using Python 2.7 and I tried this little test >>> sub = ['0',…
nik
  • 7,259
  • 11
  • 33
  • 40
272
votes
28 answers

Are global variables bad?

In C/C++, are global variables as bad as my professor thinks they are?
timp
188
votes
8 answers

Don't understand why UnboundLocalError occurs (closure)

What am I doing wrong here? counter = 0 def increment(): counter += 1 increment() The above code throws an UnboundLocalError.
Randomblue
  • 98,379
  • 133
  • 328
  • 526
179
votes
10 answers

How to declare a global variable in php?

I have code something like this:
LIGHT
  • 5,190
  • 10
  • 33
  • 75
174
votes
14 answers

How do I turn off the mysql password validation?

It seems that I may have inadvertently loaded the password validation plugin in MySQL 5.7. This plugin seems to force all passwords to comply to certain rules. I would like to turn this off. I've tried changing the validate_password_length variable…
Alex Ryan
  • 3,181
  • 5
  • 20
  • 36
159
votes
3 answers

Global variables in R

I am poking into the manuals, I wanted to ask the community: How can we set global variables inside a function?
Alphaneo
  • 10,931
  • 20
  • 65
  • 87
1
2 3
99 100