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.
8810 questions
128
votes
21 answers

How to return an array in bash without using globals?

I have a function that creates an array and I want to return the array to the caller: create_array() { local my_list=("a", "b", "c") echo "${my_list[@]}" } my_algorithm() { local result=$(create_array) } With this, I only get an expanded…
helpermethod
  • 59,493
  • 71
  • 188
  • 276
114
votes
5 answers

Using a global variable with a thread

How do I share a global variable with thread? My Python code example is: from threading import Thread import time a = 0 #global variable def thread1(threadname): #read variable "a" modify by thread 2 def thread2(threadname): while 1: …
Mauro Midolo
  • 1,841
  • 3
  • 14
  • 34
106
votes
8 answers

Declaring variables without var keyword

At w3schools there is written: If you declare a variable, without using "var", the variable always becomes GLOBAL. Is it useful to declare global variable inside the function? I can imagine to declare some global variables in some event handler,…
xralf
  • 3,312
  • 45
  • 129
  • 200
102
votes
4 answers

Why do I get a "referenced before assignment" error when assigning to a global variable in a function?

In Python, I'm getting the following error: UnboundLocalError: local variable 'total' referenced before assignment At the start of the file (before the function where the error comes from), I declare total using the global keyword. Then, in the…
Jeremy
  • 2,826
  • 7
  • 29
  • 25
101
votes
16 answers

PHP pass variable to include

I'm trying to pass a variable into an include file. My host changed PHP version and now whatever solution I try doesn't work. I think I've tried every option I could find. I'm sure it's the simplest thing! The variable needs to be set and evaluated…
user1590646
  • 1,011
  • 2
  • 7
  • 4
98
votes
7 answers

Global scss variables for Angular components without importing them everytime

I do already have SCSS variables defined in src/styles/settings/_variables.scss and I am importing them into src/styles.scss, but still these variables aren't available for every single component. Is there any way to make a global file which holds…
Volodymyr Humeniuk
  • 3,411
  • 9
  • 35
  • 70
94
votes
11 answers

Default values and initialization in Java

Based on my reference, primitive types have default values and Objects are null. I tested a piece of code. public class Main { public static void main(String[] args) { int a; System.out.println(a); } } The line…
93
votes
6 answers

Why does assigning to my global variables not work in Python?

I'm having terrible trouble trying to understand python scoping rules. With the following script: a = 7 def printA(): print "Value of a is %d" % (a) def setA(value): a = value print "Inside setA, a is now %d" %(a) print "Before…
Free Wildebeest
  • 7,272
  • 8
  • 38
  • 42
93
votes
6 answers

Are global variables in PHP considered bad practice? If so, why?

function foo () { global $var; // rest of code } In my small PHP projects I usually go the procedural way. I generally have a variable that contains the system configuration, and when I nead to access this variable in a function, I do…
KRTac
  • 2,767
  • 4
  • 22
  • 18
92
votes
3 answers

How to access the value of baseURL in Cypress

I'm just starting to learn JavaScript and have been using Cypress to automate some regression tests. The test I writing currently is meant to verify link's text and href in a header and footer. The issue I am having is that these tests need to be…
David Boydell
  • 1,053
  • 1
  • 8
  • 18
91
votes
5 answers

How to declare a global variable in a .js file

I need a few global variables that I need in all .js files. For example, consider the following 4 files: global.js js1.js js2.js js3.js Is there a way that I can declare 3 global variables in global.js and access them in any of the other 3 .js…
kp11
  • 2,055
  • 6
  • 22
  • 25
90
votes
13 answers

How to avoid global variables in JavaScript?

We all know that global variables are anything but best practice. But there are several instances when it is difficult to code without them. What techniques do you use to avoid the use of global variables? For example, given the following scenario,…
Josh Stodola
  • 81,538
  • 47
  • 180
  • 227
88
votes
7 answers

shared global variables in C

How can I create global variables that are shared in C? If I put it in a header file, then the linker complains that the variables are already defined. Is the only way to declare the variable in one of my C files and to manually put in externs at…
Claudiu
  • 224,032
  • 165
  • 485
  • 680
86
votes
3 answers

How to tell JSLint / JSHint what global variables are already defined

In my project we have some global variables that work as containers: MyProject.MyFreature.someFunction = function() { ... } So then I use that script across the site and JSLint / JSHint complains about that: 'MyProject' is not defined I know that…
Emiliano Zilocchi
  • 1,075
  • 1
  • 8
  • 12
84
votes
7 answers

Compiler error: "initializer element is not a compile-time constant"

When compiling this code, I get the error "initializer element is not a compile-time constant". Can anyone explain why? #import "PreferencesController.h" @implementation PreferencesController - (id)init { self = [super init]; if (self) { …