returning a value from a function

i have a problem with returning a value from a function.

i have the main loop and a function for reading my accelerometer and inside the ‘readAccel()’ function i am able to read the values of x, y and z but i want to be able to transfer these values (in the example z is returned) from this loop and use them inside the main loop but it doesnt work as i just read 0 from void loop() and i cant seem to figure out where i am going wrong. Any guidance will be much appreciated.

void loop()
{
   readAccel(); // read the x/y/z tilt

  Serial.print(" z: ");
  Serial.println( z ); 
  
  delay(10); 

  
}


 float readAccel() {
  uint8_t howManyBytesToRead = 6;
  readFrom( DATAX0, howManyBytesToRead, _buff); //read the acceleration data from the ADXL345


  // each axis reading comes in 10 bit resolution, ie 2 bytes.  Least Significat Byte first!!
  // thus we are converting both bytes in to one int
  int x = (((int)_buff[1]) << 8) | _buff[0];   
  int y = (((int)_buff[3]) << 8) | _buff[2];
  int z = (((int)_buff[5]) << 8) | _buff[4];
 
  Serial.print("x: ");
  Serial.print( x );
  Serial.print(" y: ");
  Serial.print( y );
  Serial.print(" z: ");
  Serial.println( z );
 
  return z;
  }
  
}

:

Any guidance will be much appreciated.

Make sure braces match.
Make sure return types are the same.
Make sure you post all your code.

this is the proper use for a returned value

void loop()
{
  Serial.print(" z: ");
  Serial.println( readAccel() ); 
  delay(10); 
}

z is a local variable to readAccel().
you can either print it directly like above or assign it to another variable

int zz = readAccel();
Serial.println( zz );

mistergreen:
z is a local variable to readAccel().
you can either print it directly like above or assign it to another variable

int zz = readAccel();

Serial.println( zz );

thanks mistergreen, this was the source of my problem and now it works so cheers pal have a nice day :slight_smile:

this was the source of my problem

You don't think that posting incomplete code that had mismatched braces was partly to blame?

Or that returning an int, when the function is supposed to return a float was a contributing factor? Why does the function return a float, anyway?