Local Variables Losing Values

I use a local variable in a function to keep track of the number of times I read a temperature. The variable seems to keep resetting to the initial value it was assigned in the declaration. If I make the variable static everything is fine. Now I call some functions within the loop function, but I thought local variables should remain in scope until the function is exited? Or is the loop function continually being called from a hidden function main?

void loop()
{
  boolean enter_pressed;
  float temperature;
  unsigned long no_of_readings=0;
  
 
    if(Serial.available())
    {
	// read each incoming character	 
      enter_pressed = ReadInput();
      if(enter_pressed == true)
      {
	// user has pressed enter so extract the command
        if(ParseCommand() == true)
	// process the command
         ExecuteCommand();
	// display a prompt 
         Prompt();
      }
    }  
  } 

#ifdef TEMP_SENSOR_USED    
 // Read temperature
  temperature = ReadTemperature(tempsensor_pin);
  no_of_readings++;
  Serial.print(temperature,2);
  Serial.print(" ");
  Serial.println(no_of_readings);
#endif  
  
}

Or is the loop function continually being called from a hidden function main?

Yes, that's why it's called loop!

Mark

Your variable, no_of_readings, is initialized over and over each time the loop makes an iteration. If you wish to preserve it's value, you can either make it a global variable by defining it outside of either setup() or loop(), or you can define it using the static storage class:

static unsigned long no_of_readings = 0;

Because statics are defined and initialized only on program start, it "retains" its value between iterations. As a general rule, it's a good idea to minimize global variables, so I'd use the static alternative.

A variable doesn't have to be defined static to be global in extent - it just has to be defined outside of
any function.

static keyword hides the global variable from other source files - makes the scope to be file-local. normal
global variables have global scope (your whole program) as well as indefinite extent (live forever).

"extent" means the time duration for which a variable exists, "scope" means the subset of the program
code from which it can be named. local scope is not the issue here, its local extent.

econjack:
As a general rule, it's a good idea to minimize global variables, so I'd use the static alternative.

Why? It's always been a question of style to me.

cptdondo:
Why? It's always been a question of style to me.

Bigger programs tend to have more variables. The greater the number of variables, the greater the chance of naming conflict and accidentally setting the value of the wrong variables.

Arrch:
Why? It's always been a question of style to me.

You avoid the use of globals for the same reason medieval kings kept their daughters in the castle keep...to keep other people from messing around with them.

See also separation of concerns, encapsulation, information hiding.

MarkT:
static keyword hides the global variable from other source files - makes the scope to be file-local. normal
global variables have global scope (your whole program) as well as indefinite extent (live forever).

The static keyword does several different things, depending where it is used. The use being suggested here is to cause a variable defined inside a function to be stored in global data rather than on the stack. This means it will retain its value between calls to the function, but it will still only be visible within the function.