Questions tagged [local-variables]

Local variables have a limited scope, generally one function or one functional block.

Local variables have a limited scope, generally one function or one functional block.

Compare with global variables, which are accessible from any part of a program.

1302 questions
-2
votes
4 answers

How can a local function variable be accessible in another function

var objectTest= { test1: function( ) { val1 = 1; }, // hows accessing the test2: function( ) { alert( val1 ); } }; objectTest.test2( );
dave
  • 14,991
  • 26
  • 76
  • 110
-2
votes
2 answers

Does C++ have a way of identifiying local class variables?

In C++, how are the local class variables declared? I'm new to C++ but have some python experience. I'm wondering if C++ classes have a way of identifying their local variables, for example, in python your class' local variables are marked with a…
Nightdra
  • 3
  • 4
-2
votes
3 answers

Local Variable referenced before assignment python3

I tried to execute the following code, def dating_age (my_age): if my_age < 18 & my_age >= 13: girls_age = my_age/2+5 elif my_age <13: print ("You are ineligible to date") else: girls_age = my_age/2+9 return…
Animikh Aich
  • 598
  • 1
  • 6
  • 15
-2
votes
1 answer

How to change locals() result from other function?

I have some code like this: def f(): i = 5 g(locals()) print 'in f:', i, j def g(env): env['j'] = env['i'] + 1 print 'in g:', env['i'], env['j'] f() I get: in g: 5 6 in f:…
maple
  • 1,828
  • 2
  • 19
  • 28
-2
votes
1 answer

C++ - Global variable vs local variable read/write speed

Which is the fastest code among the following ones? void foo1 (int & a) { a = 10; } or void foo2 (void) { GLOBAL_VARIABLE.a = 10; } (where GLOBAL_VARIABLE is a global class element with field 'a') ? Is there any difference in variable…
user1403546
  • 1,680
  • 4
  • 22
  • 43
-2
votes
1 answer

Field to local or global variable

I have a doubt about which option would be a better one in order to have a more understandable code. I have a variable that will be used just inside one method but, as this variable is a configuration variable, I think that it would be nice that…
FVod
  • 2,245
  • 5
  • 25
  • 52
-2
votes
1 answer

Python 3.2 Error "UnboundLocalError: local variable referenced before assignment"

I am currently making a 2D game where there are different guns available to the player, each of which have different firing rates. When I tried to implement the different firing rates for each gun, I got this error: if currentTime - bulletGap >= 2…
-2
votes
1 answer

Why parameters are passed outside the parenthesis () in globals(), locals() and vars() in Python?

I'm very new in Py but I know the syntax function(param1,.. paramN) and I can't understand this: globals()['z'] = 8 Could not be more 'standar' ? globals('z') = 8 # or.. globals(param = 'z') = 8 EDIT: thanks to all for yours answers, I just have…
boctulus
  • 404
  • 9
  • 15
-2
votes
2 answers

Local variable address used as errno

#include struct a { void *ptr; unsigned long val; }; void main() { unsigned char errno; struct a *id; id = malloc(sizeof(*id)); func2(id); printf("After changing %d\n", id->val); } void func2(struct a…
shunty
  • 375
  • 2
  • 7
  • 24
-3
votes
1 answer

Where does a local variable get stored, and how can I define a getter/setter for one?

So in JavaScript, I know that when you define a variable in the global scope, like this, let a = 0; console.log(a); // > 0 it's (more or less) the same as defining a property on the window object: window.a = 0; console.log(a); // > 0, exactly the…
-3
votes
2 answers

How to change a variable type in Java?

I have two objects: Employee E PTEmployee PE I need to do the following manipulation EV=E EV=PE I believe I would need to do the following: var EV = E But then when I set EV = PE, I cannot access the Class PTEmployee methods, because the IDE…
-3
votes
1 answer

UnboundLocalError: local variable 'x' referenced before assignment for one variable whilst other works in Python

How come I get local variable referenced before assignment error for cur_max while i can use copy_matrix within the dfs function? class Solution: def longestIncreasingPath(self, matrix: List[List[int]]) -> int: rows, columns =…
Joey Joestar
  • 205
  • 1
  • 11
-3
votes
2 answers

How to change an element of a local NumPy array ( which is inside a function ) from outside?

I am working on a game project and want to create a board of game by using the NumPy array. So I defined a function board() for that. But when I try to change an element of the board, it doesn't change. CODE import numpy as np def board(): game…
Rishabh Semwal
  • 328
  • 1
  • 3
  • 12
-3
votes
2 answers

Python Error, Local Variable referenced before assignment

tries= 3 for i in range(3): username=input("What is your username ") if username =="student": pwd=input("Whats your password ") if pwd=="password": print("welcome") …
-3
votes
2 answers

Local variables in C and Java

I know C and Java uses lexical scoping and the scope of a variable is within the block where it was defined. See the code of Java: public class Main { public static void main(String[] args) { int j = 0; while(j==0){ …
Jithin M V
  • 175
  • 1
  • 1
  • 10