Bool return is strange.. what am I missing?

This is a hardware dependant program, so I'll post parts of the code:

bool SolidFR(uint32_t color_front, uint32_t color_rear) {
  bool color_did_change = false;
  if (some == conditiont) {
    color_did_change = true;
  }
    //print debug messages
    _DEBUGSerial.print("CHANGED?  : ");   
    _DEBUGSerial.println (color_did_change);
  return color_did_change;

so far, so good, the debug print (DPL) always show a 0 or 1 for the bool.
here is the calling code:

      bool changed = false
      changed = changed or SolidFR(color_front_curr, color_rear_curr);
      _DEBUGSerial.print ("and the return is :");
      _DEBUGSerial.println (changed);

This "changed" is always turns out false, the "changed" is not "true" even is that is what SolidFR returns..

I get debug displaying...
CHANGED? : 1
and the return is :0
how is that possible ?

Does that return a true?

yes, only in the first depug print, it does.. the problem is that the calling function's variable changed is never set at true/1

You are stating that the return value is true for only one iteration then it changes to false?

You are saying the function that calls the function is not properly doing its thing but you are trying to figure out why the function that is called is is not properly working. Easy, garbage in garbage out.

...that won't compile. How about showing your actual code, instead of paraphrasing it?

I guess if you create a small compiling example that tries to show the error,
you see what is going on.

1 Like

the issue is in the code we don't see...

I added enough code to make a compiling sketch and it works properly for me:

bool SolidFR(uint32_t color_front, uint32_t color_rear)
{
  uint32_t some = color_front;
  uint32_t conditiont = color_rear;

  bool color_did_change = false;
  if (some == conditiont)
  {
    color_did_change = true;
  }

  //print debug messages
  Serial.print("CHANGED?  : ");
  Serial.println(color_did_change);

  return color_did_change;
}

void setup()
{
  uint32_t color_front_curr = 0, color_rear_curr = 0;
  Serial.begin(115200);
  delay(200);
  
  bool changed = false;
  changed = changed or SolidFR(color_front_curr, color_rear_curr);
  
  Serial.print ("and the return is :");
  Serial.println (changed);

  changed = false;
  changed = changed or SolidFR(color_front_curr, color_rear_curr + 1);
  
  Serial.print ("and the return is :");
  Serial.println (changed);
}

void loop() {}

The output I get is:

CHANGED?  : 1
and the return is :1
CHANGED?  : 0
and the return is :0
1 Like

this must be some odd compiler bug, I figured out that this redundant test for color_did_change makes it work:

bool test() {
....
  if (color_did_change) {
    return color_did_change;
  }
}

if remove the if.. just return as the last instruction... it won't work (return is always 0)

bool test() {
....
return color_did_change;
}

odd... - this is on STM32 core all else works fine, and it is a rather huge programs with many libraries.

That statement is 99.99999% false.

are you sure you did not create a local scoped color_did_change variable that you set to true and then loose that scope for what you return?

Highly unlikely. In my 35+ years of software development I have only uncovered 3 actual compiler bugs.