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
17
votes
9 answers

Using function arguments as local variables

Something like this (yes, this doesn't deal with some edge cases - that's not the point): int CountDigits(int num) { int count = 1; while (num >= 10) { count++; num /= 10; } return count; } What's your opinion…
Rubys
  • 3,167
  • 2
  • 25
  • 26
17
votes
4 answers

ActionMailer pass local variables to the erb template

I know I could define instance variables e.g: def user_register(username, email) @username = username @email = email mail(:to => email, :subject => "Welcome!", :template_name => "reg_#{I18n.locale}") end But, is there a way to use local…
Dmitri
  • 2,451
  • 6
  • 35
  • 55
17
votes
6 answers

Javadoc for local variables?

Short question: Is it possible to create Javadoc for local variables? (I just want an explanation for my local variable when hovering over it in Eclipse) Thanks for any hint :-)
stefan.at.kotlin
  • 15,347
  • 38
  • 147
  • 270
15
votes
3 answers

access variables of outer class in Java

in Java android application how can i access variables of outer class from the inner anonymous class ? Example: ProgressDialog dialog = new ProgressDialog(this); ..... send.setOnClickListener(new View.OnClickListener() { …
user1246620
  • 151
  • 1
  • 1
  • 3
15
votes
2 answers

Why are local variables accessed faster than global variables in lua?

So I was reading Programing in Lua 2nd Ed and I came across this paragraph here: It is good programming style to use local variables whenever possible. Local variables help you avoid cluttering the global environment with unnecessary names. …
Jdc1197
  • 245
  • 3
  • 9
15
votes
11 answers

Can static local variables cut down on memory allocation time?

Suppose I have a function in a single threaded program that looks like this void f(some arguments){ char buffer[32]; some operations on buffer; } and f appears inside some loop that gets called often, so I'd like to make it as fast as…
pythonic metaphor
  • 10,296
  • 18
  • 68
  • 110
15
votes
3 answers

How to refer to a global variable which has the same name as a local variable in C++?

If there is a global variable and the function has a parameter with the same name, and desired result is the sum of the local and global variable, how can we refer the global function in this particular situation? I know its not good idea to do so.…
15
votes
5 answers

Why can a local variable be accessed in another thread created in the same class?

I couldn't really find anything on this exact topic, so please lead me toward the right direction, if a question already exists. From what I have learned about .NET, it is not possible to access variables across different threads (please correct me…
philkark
  • 2,417
  • 7
  • 38
  • 59
15
votes
2 answers

Declaring a useless local variable

So this is an odd one, I know the code itself is fairly useless, but what I'm wondering why I get the error: I was writing some code, I had written this: if(scan.hasNextInt()) int row = scan.nextInt(); Wasn't thinking about variable scope at…
Mike
  • 47,263
  • 29
  • 113
  • 177
14
votes
7 answers

How do I value-initialize a Type* pointer using Type()-like syntax?

Variables of built-in types can be value-initialized like this: int var = int(); this way I get the default value of int without hardcoding the zero in my code. However if I try to do similar stuff for a pointer: int* ptr = int*(); the compiler…
sharptooth
  • 167,383
  • 100
  • 513
  • 979
14
votes
5 answers

Why it says that "Cannot refer to a non-final variable i inside an inner class defined in a different method"?

I have button click listener and in onCreate() method I have a local variable like onCreate() { super.onCreate(); int i = 10; Button button = (Button)findViewById(R.id.button); button.setOnClickListener(new View.OnClickListener() { …
Khawar Raza
  • 15,870
  • 24
  • 70
  • 127
14
votes
1 answer

Why does symbol definition statement inside exec() sometimes have no effect on the local symbol table?

The following code snippet works as expected: def test(): print(f'local symbol table before exec : {locals()}') exec('a = 0') print(f'local symbol table after exec : {locals()}') test() # printed result: # local symbol table before…
Alan Xu
  • 141
  • 5
14
votes
3 answers

Behaviour Difference: 'null' initialized final static member, and 'null' initialized final local variable

I came across a behavior, I didn't knew of earlier, in the follow up code. Consider the 1st case: public static void main(String[] args) { final String str = null; System.out.println(str.length()); // Compiler Warning:…
Rohit Jain
  • 209,639
  • 45
  • 409
  • 525
14
votes
6 answers

Inner class and local variables

Why do I need to declare a local variable as final if my Inner class defined within the method needs to use it ? Example : class MyOuter2 { private String x = "Outer2"; void doStuff() { final String y = "Hello World"; final class MyInner…
AllTooSir
  • 48,828
  • 16
  • 130
  • 164
13
votes
1 answer

(GNU) Forth Local Variable Behavior

I was just learning about local variables for word definitions in Forth. I happen to use GNU Forth (gforth). I was looking at the question and answer, Forth local variable assigning variables and was struggling with the behavior of the given answer.…
lurker
  • 56,987
  • 9
  • 69
  • 103