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
32
votes
3 answers

How to list local-variables in Ruby?

def method a = 3 b = 4 some_method_that_gives # [a, b] end
Cheng
  • 4,816
  • 4
  • 41
  • 44
31
votes
3 answers

Why don't instance fields need to be final or effectively final to be used in lambda expressions?

I'm practicing lambda expressions in Java. I know local variables need to be final or effectively final according to the Oracle documentation for Java SE 16 Lambda Body : Any local variable, formal parameter, or exception parameter used but not…
DamianGDO
  • 449
  • 4
  • 8
27
votes
6 answers

Why does local variable kill my global variable?

Sorry for this question, but this issue really screwed up my day. The following Code alerts 10 as it should: var globalId='10'; function check(){ alert(globalId); } check(); But this next code alerts undefined: var globalId='10'; …
Per Spjuth
  • 271
  • 1
  • 3
  • 3
27
votes
2 answers

"array initializer needs an explicit target-type" - why?

Following JEP 286: Local-Variable Type Inference description I am wondering, what the reason is for introducing such a restriction, as: Main.java:199: error: cannot infer type for local variable k var k = { 1 , 2 }; ^ (array…
Andremoniy
  • 34,031
  • 20
  • 135
  • 241
26
votes
1 answer

Exit code of command substitution in bash local variable assignment

How can I check the exit code of a command substitution in bash if the assignment is to a local variable in a function? Please see the following examples. The second one is where I want to check the exit code. Does someone have a good work-around or…
Vampire
  • 35,631
  • 4
  • 76
  • 102
26
votes
5 answers

At what moment is memory typically allocated for local variables in C++?

I'm debugging a rather weird stack overflow supposedly caused by allocating too large variables on stack and I'd like to clarify the following. Suppose I have the following function: void function() { char buffer[1 * 1024]; if( condition )…
sharptooth
  • 167,383
  • 100
  • 513
  • 979
24
votes
2 answers

Highlight Undefined Javascript Variables in Visual Studio Code

One of my faviourite mistakes in Javascript is to forget to define variables I am using locally in a function. As you all know, due to scope, this is not always an error. Is there any way in Visual Studio Code to highlight variables that are not…
Rewind
  • 2,554
  • 3
  • 30
  • 56
24
votes
10 answers

In C, does using static variables in a function make it faster?

My function will be called thousands of times. If i want to make it faster, will changing the local function variables to static be of any use? My logic behind this is that, because static variables are persistent between function calls, they are…
20
votes
5 answers

Any way to modify locals dictionary?

locals is a built in function that returns a dictionary of local values. The documentation says: Warning The contents of this dictionary should not be modified; changes may not affect the values of local variables used by the…
Casebash
  • 114,675
  • 90
  • 247
  • 350
19
votes
2 answers

Why are local variables also called "Automatic" in Java?

I read this in Kathy Sierra's book: "Local variables are sometimes called stack, temporary, automatic, or method variables, but the rules for these variables are the same regardless of what you call them." Why are the local variables called…
unj2
  • 52,135
  • 87
  • 247
  • 375
19
votes
1 answer

Where are Java final local variables stored?

Take the following example: public void init() { final Environment env = new Environment(); Runtime.getRuntime().addShutdownHook(new Thread() { public void run() { env.close(); } }); } Firstly, where is env…
Joel
  • 29,538
  • 35
  • 110
  • 138
18
votes
5 answers

Goto prior to a variable definition - what happens with its value?

Here is some question I wondered about. Given the following code, can we be certain about its output? void f() { int i = 0; z: if(i == 1) goto x; else goto u; int a; x: if(a == 10) goto y; u: a = 10; i = 1; goto z; y: std::cout <<…
Johannes Schaub - litb
  • 496,577
  • 130
  • 894
  • 1,212
18
votes
2 answers

When do you need to pass arguments to `Thread.new`?

Local variables defined outside of a thread seem to be visible from inside so that the following two uses of Thread.new seem to be the same: a = :foo Thread.new{puts a} # => :foo Thread.new(a){|a| puts a} # => :foo The document gives the…
sawa
  • 165,429
  • 45
  • 277
  • 381
17
votes
2 answers

Why can't eval find a variable defined in an outer function?

I am aware that the use of eval() usually means bad code, but I stumbled upon a weird behavior of the eval() function in internal functions that I could not understand. If we write: def f(a): def g(): print(eval('a')) return…
Ricardo
  • 335
  • 2
  • 8
17
votes
2 answers

How do "var" and raw types come together?

I came across an answer that suggests to use var list = new ArrayList(); I was surprised to find a raw type here, and I am simply wondering: does var use the <> "automatically? ( in the meantime, the answer was changed to use , but I am…
GhostCat
  • 137,827
  • 25
  • 176
  • 248
1 2
3
86 87