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
10
votes
1 answer

Why use local require in Lua?

What's the difference between this local audio = require "audio" and require "audio" Is there any advantage of the former?
Timmmm
  • 88,195
  • 71
  • 364
  • 509
10
votes
2 answers

`defined?` and `unless` not working as expected

I was expecting the following snippet: var = "Not Empty" unless defined? var var # => nil to return "Not Empty", but I got nil. Any insight into why this is happening?
Noman Ur Rehman
  • 6,707
  • 3
  • 24
  • 39
10
votes
4 answers

Use of unassigned local variable on finally block

When could i in this example be unassigned? int i; try { i = 2; } catch { i = 3; } finally { string a = i.ToString(); }
Diego
  • 16,436
  • 26
  • 84
  • 136
10
votes
3 answers

In Ruby, is there no way to dynamically define a local variable in the current context?

I'm wondering if there is a method which will allow me to dynamically define a previously undefined variable in the current context. For example: foo # => NameError: undefined method or local variable ... # Some method call which sets foo = 1 in the…
Peter Alfvin
  • 28,599
  • 8
  • 68
  • 106
10
votes
6 answers

Python global/local variables

Why does this code work: var = 0 def func(num): print num var = 1 if num != 0: func(num-1) func(10) but this one gives a "local variable 'var' referenced before assignment" error: var = 0 def func(num): print num var…
JJ Beck
  • 5,193
  • 7
  • 32
  • 36
9
votes
1 answer

gdb: printing a variable not in the current scope

I am using gdb and I was wanting to print a variable not currently in the scope. I am not sure what the exact name of the variable is so I would like to be able to change scopes rather than printing a specific variable in a specific file.
user972276
  • 2,973
  • 9
  • 33
  • 46
9
votes
2 answers

Defining variables within a makefile macro (define)

I am using define to create a macro. However, within a define construct, I am not able to create a variable. Assigning a variable doesn't cause an error, but when I try to use it a bit later, its value is empty. For…
Ruan Mendes
  • 90,375
  • 31
  • 153
  • 217
9
votes
2 answers

Is splitting assignment into two lines still just as efficient?

From a runtime efficiency perspective in python are these equally efficient? x = foo() x = bar(x) VS x = bar(foo()) I have a more complex problem that can essentially be boiled down to this question: Obviously, from a code length perspective the…
Adrix
  • 401
  • 1
  • 3
  • 11
9
votes
2 answers

What is the difference between a threadvar and a local variable

In my threads, I always declare local variables "normally", thus: procedure TMyThread.Execute ; var i : integer ; begin i := 2 ; etc, If I declare them thus: procedure TMyThread.Execute ; threadvar j : integer ; begin j := 2 ; how does…
rossmcm
  • 5,493
  • 10
  • 55
  • 118
9
votes
1 answer

How to find variables defined outside current scope in C#?

I'm using Visual Studio 2013. I'm reworking some horrible code left by someone else, which uses almost EXCLUSIVELY global variables, and trying to clean that up so I can have each function be properly encapsulated and not cause effects outside of…
eidylon
  • 7,068
  • 20
  • 75
  • 118
9
votes
2 answers

Does javascript implement lexical scoping?

Why does this return 2 instead of 1? Seems the second "var" is silently ignored. function foo() { var local = 1; { var local = 2; } return local; } foo() /* 2 */
Zdeněk Pavlas
  • 357
  • 2
  • 5
9
votes
4 answers

Why C# local variable should be assigned directly, even if it's default value?

If you look at next example: public void TestLocalValuesAssignment() { int valueVariable; // = default(int) suits fine string refType; // null suits fine as well try { valueVariable = 5; refType = "test"; } …
9
votes
9 answers

Can i declare a static variable inside static member function in Java?

private static int Fibonoci(int n) { static int first=0; static int second=1; static int sum; if(n>0) i am getting a error "Illegal Modifier" and if i remove static keyword there is no error and i need those variables to be static
Raghu
  • 313
  • 2
  • 7
  • 19
8
votes
3 answers

return a local pointer

struct node { Item item; node *l, *r; node(Item x) {item = x; l = 0; r = 0;} }; typedef node* link; link max(Item a[], int l, int r) { int m = (l+r)/2; link x = new node(a[m]); if (l==r) return x; // return a local pointer x->l…
Ke Li
  • 157
  • 4
  • 9
8
votes
2 answers

global or local variables in a jquery-plugin

How is it possible to give a jquery-plugin individual local variables, that are accessable in different plugin-functions? My script shows an alert with the content '123', but I am expecting 'abc'. So variable 't' exists only once and not twice for…
user1027167
  • 4,320
  • 6
  • 33
  • 40