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
54
votes
5 answers

Undefine variable in Ruby

Let's say I'm using irb, and type a = 5. How do I remove the definition of a so that typing a returns a NameError? Some context: later I want to do this: context = Proc.new{}.binding context.eval 'a = 5' context.eval 'undef a' # though this doesn't…
Peter
  • 127,331
  • 53
  • 180
  • 211
54
votes
3 answers

How to get local variables updated, when using the `exec` call?

I thought this would print 3, but it prints 1: # Python3 def f(): a = 1 exec("a = 3") print(a) f() # 1 Expected 3
ubershmekel
  • 11,864
  • 10
  • 72
  • 89
53
votes
4 answers

How to declare a variable in SQL Server and use it in the same Stored Procedure

Im trying to get the value from BrandID in one table and add it to another table. But I can't get it to work. Anybody know how to do it right? CREATE PROCEDURE AddBrand AS DECLARE @BrandName nvarchar(50), @CategoryID int, @BrandID int SELECT…
Nicklas
  • 561
  • 1
  • 4
  • 7
51
votes
3 answers

c#: static variable in a static method

Can you have a static variable in a static method? Would the value of this variable be preserved across all calls to the method? eg. public static void MyMethod() { static int x = 0; x++; }
Craig Johnston
  • 757
  • 2
  • 8
  • 9
46
votes
7 answers

How to declare a local constant in C#?

How to declare a local constant in C# ? Like in Java, you can do the following : public void f(){ final int n = getNum(); // n declared constant } How to do the same in C# ? I tried with readonly and const but none seems to work. Any help would…
missingfaktor
  • 90,905
  • 62
  • 285
  • 365
45
votes
4 answers

returning a local variable from function in C

#include int foo1(void) { int p; p = 99; return p; } char *foo2(void) { char buffer[] = "test_123"; return buffer; } int *foo3(void) { int t[3] = {1,2,3}; return t; } int main(void) { int *p; char…
Mark
  • 1,751
  • 3
  • 14
  • 14
43
votes
3 answers

Ruby local variable is undefined

I have the following Ruby code: local_var = "Hello" def hello puts local_var end hello I get the following error: local_variables.rb:4:in 'hello': undefined local variable or method 'local_var' for main:Object (NameError) from…
Sergii Shevchyk
  • 38,716
  • 12
  • 50
  • 61
43
votes
5 answers

In JavaScript, why should I usually prefer 'const' to 'let'?

Why most of the time should I use const instead of let in JavaScript? As we know if we use const then we can't reassign value later. Then why not use let instead of const?
Mohammed Saimon
  • 563
  • 1
  • 5
  • 10
42
votes
1 answer

Why is `a = a` `nil` in Ruby?

I watched this video. Why is a = a evaluated to nil if a is not defined? a = a # => nil b = c = q = c # => nil
Brian Hsu
  • 8,781
  • 3
  • 47
  • 59
39
votes
4 answers

Is it reasonable to synchronize on a local variable?

From the Java memory model, we know that every thread has its own thread stack, and that local variables are placed in each thread's own thread stack. And that other threads can't access these local variables. So in which case should we synchronize…
NingLee
  • 1,477
  • 2
  • 17
  • 26
36
votes
4 answers

Does using var with a literal result in a primitive or a primitive wrapper class?

After reading and talking about Java 10s new reserved type name var (JEP 286: Local-Variable Type Inference), one question arose in the discussion. When using it with literals like: var number = 42; is number now an int or an Integer? If you just…
sweisgerber.dev
  • 1,726
  • 17
  • 36
36
votes
3 answers

How to dynamically define a class method which will refer to a local variable outside?

class C end var = "I am a local var outside" C.class_eval do def self.a_class_method puts var end end I know, this is not correct, because the def created a new scope. I also know that use define_method can create a instance method…
Croplio
  • 3,433
  • 6
  • 31
  • 37
36
votes
8 answers

Use of final local variables in java

I was wondering is there any usability of using final local variables. Variables are not overridden anyway when inheritance comes into picture. For example a simple code as below public static void main(String args[]) { final String data =…
Aniket Thakur
  • 66,731
  • 38
  • 279
  • 289
34
votes
3 answers

Why a variable defined global is undefined?

I have here a simple function and a global variable. Why is myname undefined and not the string "global"? var myname = "global"; // global variable function func() { alert(myname); // "undefined" var myname = "local"; alert(myname); //…
J Rod
  • 621
  • 1
  • 8
  • 14
32
votes
5 answers

Dynamically set local variables in Ruby

I'm interested in dynamically setting local variables in Ruby. Not creating methods, constants, or instance variables. So something like: args[:a] = 1 args.each_pair do |k,v| Object.make_instance_var k,v end puts a > 1 I want locally variables…
Allyl Isocyanate
  • 13,306
  • 17
  • 79
  • 130
1
2
3
86 87