Can't Access Global Variable Inside Function

Can't access global variables inside a function (Javascript)

that is because you are fetching the value before the user enters anything, because the input data is fetched once script starts running

why python can't access global variable value with function parameter name change?

There aren't any global variables in use here.

In the first example, z is a parameter to the function, not a global. Note that when you increment z, it does not change in the calling scope, because the z inside the function is a copy of the z you passed in from outside the function.

In the second example, there is no b inside the function (the parameter is z), which is why you get an error inside the function when you try to modify it.

To do what you're trying to do, you should make the parameter a mutable object that contains the value you're trying to mutate; that way you can modify the value inside the function and the caller will have access to the new value. You could define a class for this purpose, or you could simply make it a single-element list:

def a(z):
z[0] += 1
b = [5]
a(b) # b == [6]

Or, if possible, a better approach IMO is not to depend on mutability, and to just return the new value, requiring the caller to explicitly re-assign it within its scope:

def a(z)
return z + 1
b = 5
b = a(b) # b == 6

Cannot access global variable inside function

Inside main() you used the same name: config for a local variable. This will shadow the global variable. After this point, you can't refer to the global variable (for details, see Refer to constant or package level variable instead of function level variable). You load the config into this local variable, but the global config will remain unchanged (zero), and the UDProutine() function will read / print this global variable.

If you want to load the config to the global variable, don't create a local config variable. Just delete this line:

config := Config{}

Note:

The above line is a short variable declaration which creates a new variable.

You most likely just wanted to assign an empty Config{} struct value to config, but for that you have to use an assignment:

config = Config{}

But this is not needed in this case, as the global variable declaration:

var config Config

will initialize the config global variable to its zero value, which in case of structs is a struct value where all its fields are also initialized to the zero values of their types.

Can't access global variable inside function

You have to pass it to the function:

<?php
$sxml = new SimpleXMLElement('<somexml/>');

function foo($sxml){
$child = $sxml->addChild('child');
}

foo($sxml);
?>

or declare it global:

<?php
$sxml = new SimpleXMLElement('<somexml/>');

function foo(){
global $sxml;
$child = $sxml->addChild('child');
}

foo();
?>

If the variable isn't global but is instead defined in an outer function, the first option (passing as an argument) works just the same:

<?php
function bar() {
$sxml = new SimpleXMLElement('<somexml/>');
function foo($sxml) {
$child = $sxml->addChild('child');
}
foo($sxml);
}
bar();
?>

Alternatively, create a closure by declaring the variable in a use clause.

<?php
function bar() {
$sxml = new SimpleXMLElement('<somexml/>');
function foo() use(&$xml) {
$child = $sxml->addChild('child');
}
foo();
}
bar();
?>

Python unable to access global variable in simple grade program

In the getInput function, you declare that marks will be a global variable, but never actually assign it. Since the scope of the global keyword is only within that function, you actually never created a global variable marks.

Then in getGrades, you create another variable marks, which is local to getGrades, because you didn't declare marks as global in getGrades. This is why showGrades cannot find the variable marks; it is local to getGrades.

Try making declaring marks as a global inside of getGrades, and I'm pretty sure the problem will be fixed.

For more see: What are the rules for local and global variables in Python?


Edit: To confirm my thinking I decided to do an experiment: I created a function where I declared, but never assigned a global variable. We then run the function, and use the built in globals function to check if myglobalvariable actually exists in the global scope

>>> def globaltest():
global myglobalvariable

>>> globaltest()
>>> globals()

{'__name__': '__main__', '__doc__': None, '__package__': None, '__loader__': <class '_frozen_importlib.BuiltinImporter'>, '__spec__': None, '__annotations__': {}, '__builtins__': <module 'builtins' (built-in)>, 'globaltest': <function globaltest at 0x0000027E24B11EA0>}

We see from the output above that, as expected, myglobalvariable is not in the global scope.

can't reference global variable inside a function

global is used inside the function, not out side.

So at the top of your file you can do

box = []

and inside your function:

global box
# now change box the way you want

C - can't access global variable within user functions

The variable used is a duplicated name. In main the local used is accessed. But in checkData the global instance is used, but causes an error since you are dereferencing a NULL pointer (static variables are initialised to 0).

What are the situations where a function can't access global variables? And why?

The reason you can't get thing 1 is because you are calling for testing from within the same scope that testing has a local definition. All declarations are "hoisted" to the top of their enclosing scope, so even though you wrote:

var testing = 'thing 2';

after your first console.log(), the var testing gets hoisted and processed as the very first bit of code in the function, so testing is declared locally, but not initialized yet (that will happen at the line's original location).

So, your code processes like this:

var testing = 'thing 1';      // 1. Runs as soon as encountered

function func() {
var testing; // 3. testing is hoisted and declared, but still undefined

console.log(testing); // 4. undefined is logged

testing = 'thing 2'; // 5. local `testing` is now initialized

console.log(testing); // 6. local `testing` is logged "thing 2"
}

func(); // 2. func is invoked

It's perfectly OK (but sometimes confusing) to have multiple variables with the same identifier (name), but what you must understand is that the smaller scopes are always checked first, so from within func, when you ask for testing, you will get the local version that "hides" the global one for the duration of that function.

Globals are called globals because you can always access them. If you want the global version, you can always access it as a property of the global window object: