I wrote a simple dot product function on an MBED LPC1768:
float dotProduct(float a[], float b[], int length){
float sum;
int i;
for (i = 0; i < length; i++){
sum = sum + a[i]*b[i];
}
return sum;
}
I have two calls to dotProduct in a row in my program that (for testing) dot two 128 element 1-vectors ({1,1,1...1}) and print them to serial. So the expected result should just be 128 and 128. The variable sum
seems to persist, however, because I get 128 and 256. Furthermore, if I change the float sum
declaration to float sum = 0
, I get the desired result of 128 and 128. The variable sum
is not static, so why does it persist between function calls? Thanks