0

Why does this throw the error: "void value not ignored as it ought to be

returning = printTemperature(returnLine);"

void printTemperature(DeviceAddress deviceAddress)
{
  float tempC = sensors.getTempC(deviceAddress);
  if (tempC == -127.00) {
    Serial.print("Error getting temperature");
     return 0;
  } else {
    return tempC;
  }
}


void loop(){

   delay(2000);
   feeding = printTemperature(feedLine);
   returning = printTemperature(returnLine);
    mySql;
    feeding = 0;
    returning = 0;
}

Both 'feeding' and 'returning' are declared as a float too.

1

2 Answers 2

2

printTemperature() is declared void (i.e., nothing returned) but it contains a return statement, and you've attempted to assign the (void) value. You probably meant to declare the function as 'float'.

1
  • Ah yeah. I hacked this sketch together and was meant to change it but forgot. Sometimes you spent ages looking for you error but just don't see it while it was staring you in the eyes... Tanks!
    – Arjan_IO
    Mar 2, 2017 at 12:37
1

First of all: In C/C++ world void means "empty" as in "no value".

As the method is declared void it is saying that it does not return a value. But the code sets the value and also assigns the return value to a variable. Logically that does not make sense.

In the old days a void method returned an int (I'm not sure whether that is still the case) but it has never been a good idea to make a value returning method a void returning method. So technically in the old days it made sense but it still was "not a good idea"

Nowadays compilers will return an error when you return a value in a void method. This can be turned off by using the -fpermissive compiler directive. This directive might be useful in some template cases. I advice not to use it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.