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

In Ruby, why after starting irb, foo.nil? says undefined error, and @foo.nil? gives "true", and @@wah.nil? gives error again?

Same in Ruby 1.8.7 and 1.9.2: $ irb ruby-1.8.7-p302 > foo.nil? NameError: undefined local variable or method `foo' for # from (irb):1 ruby-1.8.7-p302 > @bar.nil? => true ruby-1.8.7-p302 > @@wah.nil? NameError: uninitialized…
nonopolarity
  • 146,324
  • 131
  • 460
  • 740
13
votes
3 answers

Get value from string representing local variable

I have a local variable name as a string, and need to get its value. variable = 22 "variable".to_variable? How can I get the value 22 form the string?
Lasse Sviland
  • 1,479
  • 11
  • 23
13
votes
5 answers

What does "local variables at the outermost scope of the function may not use the same name as any parameter" mean?

I have been reading the C++ primer 5th edition. In the third paragraph of Function Parameter List of Chapter 6.1 . It writes "Moreover, local variables at the outermost scope of the function may not use the same name as any parameter". What does it…
LiuHao
  • 412
  • 1
  • 6
  • 16
13
votes
4 answers

Why is it not possible to get local variable names using Reflection?

If I have a code like this: public class Program { public static void Main() { string bar = ""; int foo = 24; } } I can get the local variables declared in Main using: var flag = BindingFlags.Static |…
Selman Genç
  • 100,147
  • 13
  • 119
  • 184
13
votes
2 answers

Why are there local variables in stack-based IL bytecode

In a stack-based intermediate language, such as CIL or Java bytecode, why are there local variables? One could just use only the stack. May not be so easy for hand-crafted IL, but a compiler can surely do it. But my C# compiler does not. Both the…
Daniel A.A. Pelsmaeker
  • 47,471
  • 20
  • 111
  • 157
12
votes
1 answer

how refer to a local variable share same name of a global variable in C?

for example #include int foo = 100; int bar() { int foo; /* local foo = global foo, how to implemented? */ return 0; } int main() { int result = bar(); return 0; } I think in the function bar, calling foo directly…
Alfred Zhong
  • 121
  • 1
  • 1
  • 4
12
votes
1 answer

Why doesn't Perl's foreach require its variable to be declared with my?

Today, I stumbled over something in Perl I was not aware of: it "localizes" the variable that the elements of the list iterated over is assigned to. This, of course, is documented in the Perl documentation - however I failed to remember or read…
René Nyffenegger
  • 39,402
  • 33
  • 158
  • 293
12
votes
3 answers

Where are .NET local variables stored?

In IL, you can define local variables using the .locals directive. Where are these variables stored, stack or heap?
thecoop
  • 45,220
  • 19
  • 132
  • 189
12
votes
1 answer

Reading a parent's scope in python

Suppose I have a hierarchy of functions, I want to be able to access (not change!) the parents scope. Here is an illustrative example. def f(): a = 2 b = 1 def g(): b = 2 c = 1 print globals() #contains a=1 and…
Andy Hayden
  • 359,921
  • 101
  • 625
  • 535
11
votes
4 answers

Variable sharing inside static method

I have a question about the variables inside the static method. Do the variables inside the static method share the same memory location or would they have separate memory? Here is an example. public class XYZ { Public Static int A(int value) …
Krishna
  • 1,936
  • 5
  • 22
  • 30
11
votes
2 answers

delphi - local variable and array of TPair - strange behavior of memory allocation

i have following code example compiled in delphi xe5 update 2. procedure TForm1.FormCreate(Sender: TObject); var i,t:Integer; buf: array [0..20] of TPair; begin t := 0; for i := Low(buf) to High(buf) do begin …
linluk
  • 1,650
  • 16
  • 33
11
votes
4 answers

Python local vs global variables

I understand the concept of local and global variables in Python, but I just have a question about why the error comes out the way it is in the following code. Python execute the codes line by line, so it does not know that a is a local variable…
Hanhan Li
  • 455
  • 5
  • 13
11
votes
4 answers

Lambda assigning local variables

Consider the following source: static void Main(string[] args) { bool test; Action lambda = () => { test = true; }; lambda(); if (test) Console.WriteLine("Ok."); } It should compile, right? Well, it doesn't. My question…
Spook
  • 25,318
  • 18
  • 90
  • 167
11
votes
3 answers

Rails partial locals not persisting when sent to another partial as its own local

I render a partial like so: <%= render :partial => 'widgets/some_partial, :locals => {:foo => 'bar'} %> So inside of _some_partial.html.erb I render two more partials like so: <% #foo.nil? #=> false %> <%= render :partial =>…
user94154
  • 16,176
  • 20
  • 77
  • 116
10
votes
2 answers

Scope of variable with out parameter

Example: I have the following code block: if (!int.TryParse("123", out var parsedNumber)) { return; } Console.WriteLine(parsedNumber); The output in console is: 123 Question: How is it possible, that the line Console.WriteLine(parsedNumber);…
Sean Stayns
  • 4,082
  • 5
  • 25
  • 35