declaring variables in the loop? (a variable scope question)

Does it make sense to declare a variable in the Loop?
Would it create a new variable each time it loops through?

if you associate a value to that variable at the same time as declaring it, would it reset each loop?
i.e.

void Loop(){
int k = 10;

.
.
.
}

I realize that I can declare it as a global, but I then I get to wonder, why not declare all variables as global (other than it being sloppy memory wise)

Thx!

Chris

I believe that a variable declared inside a function is stored in stack memory space and will be removed when the function ends and restored (recreated, redeclared, who knows what hell to call it :wink: ) when the function is called again.

I understand the principles of variable scope (I read the white book years ago :wink: ) and it makes a lot of sense for large programs or projects where different people are writing different sections of a program. However in context of small microcontroller projects, I think the case against the overuse of global variables could be relaxed a little?

Most of my sketches are only a few pages long or shorter and I find it useful to program on the fly and having most of the variables declared back in the beginning of the sketch as I go. It seems to make it easier for me to write and test sections at a time as I slowly build my complete sketch. It's kind of like I build my circuits, a part at a time, testing each section and then integrating into the larger project.

I don't know if that makes sense, or If I described my (over) use of global declarations well enough, but it works for me and I don't have a boss to tell me to do it his way or the highway. 8)

Lefty

Well, one reason for me to try not to use all globals is; I chose the arduino over others (pic-axe etc...) because it uses C/C-like language Which I figure what I learn programming wise will carry me further into other environments. So programming using proper technique or close to it is important. Too, I actually find declaring local variables to be easier. This way, I only have to keep track of what is going on with any function I am dealing with at that time (working with little parts).

Thx,

Chris

It would be unusual to declare a varible as you did, but if you do then it will work and the varible will be set back to the initial value each time that line is executed.

More normal would be this:

for(int i=0; i++; i<10)
{
// do something with i or test it
}

Or maybe:

int i=0;
while(i<10)
{
// do something with or without i

}

neurascenic:
if you associate a value to that variable at the same time as declaring it, would it reset each loop?

Yes it does.

neurascenic:
I realize that I can declare it as a global, but I then I get to wonder, why not declare all variables as global (other than it being sloppy memory wise)

Memory-wise it won't make much difference. Either way it has to use a bit of your memory.

It's more the sloppiness of variable scope. Say you had a variable "count" which you accidentally used in more than one function. Especially if one called the other. Then you are getting confused, and you may find the variable contents change in ways you don't expect.

The major difference is if you use recursive functions. If you don't know what that means you probably don't need to worry. But briefly a recursive function is one which calls itself, directly or indirectly. Like this:

void foo (int a)
  {
  int b = a + 1;
  foo (b);
  }

In this example the variable b is newly created for every recursive call of foo, which wouldn't happen with a global variable. Also in this example it will eventually crash because it will recurse indefinitely.

For good programming style you should only use local variables unless you really need to keep the values around. Not to save memory but to avoid obscure programming bugs. If you start having a heap of global variables you start making a mess, and you have to remember what each one is used for, and not to use it for something else (like "count" or "i" or something).

Nick,

Thanks for the reply; What happens if I declare the variable in the Loop() but don't set the value in the declaration?
is the variable the same (i.e. the same address) or is it a new variable each time the loop such as is the case with recursion? Or does the value remain from the last iteration?

I am used to dealing with the primary function being the main() which doesn't loop. It is perfectly fair to declare variables there.

Thanks,

Chris

If you don't set the value it is undefined basically. Maybe it will be the same, I wouldn't rely on it.

You don't have to use "loop" - just never exit "setup".

Anyway, another approach is to use static, eg.

void loop ()
  {
  static int nomnom = 0;

  nomnom++;  // adds 1 each time
  }

The static variable has local scope but global lifetime, if you see what I mean.

1 Like

Static!!! Perfect!!! I do see!

That is exactly what I want!

Thank you oh so very much!

Chris