Quote:
Originally Posted by Andrew Blair
...my temp variable is never changing...
Code:
int delta_distance(void)
{
int temp;
temp -= distance();
return (int) temp;
temp=distance;
}
|
The
temp variable in your code evaporates as soon as
delta_distance() returns. If you want to keep a local variable's value between calls to a function, you need to declare it
static:
Code:
int delta_distance(void)
{
static int last_distance = 0; // set to zero only on program start, retains value between function calls
int delta;
delta = distance()-last_distance; // compute difference between distances
last_distance = distance(); // remember this distance for next time
return delta;
}