Boolean function return

I have a function that I want to know if a value is saved correctly so I declared it as boolean and after write the value and read that is correct, I set a return = true for this function:

boolean LinWriteRead (byte CompresorID, byte IDSpeedSettingHb, byte IDSpeedSettingLb, byte ValueH, byte ValueL  ){
     contar2 ++;      
      if (contar2 == 1) LINwriteEnable = true; 
      else if (contar2 == 2) LINwriteEnable = false;
      else if (contar2 > 2) contar2 = 0;
            
      if (LINwriteEnable)LINwriteRegister(0x01,IDSpeedSettingHb,IDSpeedSettingLb,ValueH,ValueL);
      else if (contar2 ==2)LINreadRegister(0x01,IDSpeedSettingHb,IDSpeedSettingLb);
      if ( contar2 ==2 && valorH == ValueH && valorL == ValueL) {
        mySerial.println ("VALUE SAVED");
        valorH = 0x00; 
        valorL = 0x00;
       return = true; 
      }
      else if (contar2 ==2) {
      error ++;
      }
      else if (error ==5) {
        mySerial.println ("ERROR LIN WIRE NO CONECTED");  
        error=0;
       return = false;
      }
     // delay (100);
  }

Now, how can I call the function and after run, take the return value? And how can I try again to write the value, if the function return false?

I tryed something like :

if (!LinWriteRead (CompresorID, IDSpeedSettingHb, IDSpeedSettingLb, ValueH, ValueL  )) LinWriteRead (CompresorID, IDSpeedSettingHb, IDSpeedSettingLb, ValueH, ValueL;

and

if (!LinWriteRead (CompresorID, IDSpeedSettingHb, IDSpeedSettingLb, ValueH, ValueL  )) serial.print ("false");

But no luck. What I am doing wrong? What is the right way to do this?

Thanks in advance!!

you need to return that value (true or false). return is a keyword in the language

bool myTest(int a, int b)  {
  if (condition) {
    doSomething();
    return true;  // success
  } 
 return false; // fail
}

of if the logic is more complicated, declare a local variable that you set to false for example, modify it as you go and return it

bool myTest(int a, int b) {
  bool success = false;
  if (condition) {
    if (someOtherStuff()) {
      ...
      success = true;
    } 
    ...
  } 
  return success;
}

then you can call if (myTest(10,20)) {....}

Didn't you get compile errors?!?

sketch_oct19a:15:12: error: expected primary-expression before '=' token
     return = true;
            ^

sketch_oct19a:25:12: error: expected primary-expression before '=' token
     return = false;
            ^

@johnwasser, yes sorry, I typed wrong after many changes in the Code. Its without equal simbol. The problem is that I don't know how to call the function and get the return, I tried similar code recommended by @J-M-L

if (!LinWriteRead (CompresorID, IDSpeedSettingHb, IDSpeedSettingLb, ValueH, ValueL  )) LinWriteRead (CompresorID, IDSpeedSettingHb, IDSpeedSettingLb, ValueH, ValueL);

if (!LinWriteRead (CompresorID, IDSpeedSettingHb, IDSpeedSettingLb, ValueH, ValueL  )) serial.print ("false");

With your example line, the function is called and its return value is used as the condition of the if statement

If your function returned true, then the code block of this if statement will not be executed, because the result of the condition is false ( ! mean NOT so if (!true) is the same as if (false) )

Here is a small example to help you understand : https://ideone.com/YWkILG

@guix , I tryed this, if function if false, print false:

if (!LinWriteRead (CompresorID, IDSpeedSettingHb, IDSpeedSettingLb, ValueH, ValueL  )) serial.print ("false");

And Also tryed this, if function is false, call again the function until get true:

if (!LinWriteRead (CompresorID, IDSpeedSettingHb, IDSpeedSettingLb, ValueH, ValueL  )) LinWriteRead (CompresorID, IDSpeedSettingHb, IDSpeedSettingLb, ValueH, ValueL);

I Will try again but to the moment I can't get the result with the if statement in the way I am trying to do.

You realise that the exclamation mark (bang) '!' before the function call means 'not', right ?

so it's something like this

if (functionCall is not true) 
  Serial.print("function call was false, so this gets printed");

if function is false, call again the function until get true

if you want to do this and have an active wait, you need to use a loop statement of some sort. for example

// Blocking active wait
while (! functionCall(x,y,z)) { // as long as functionCall returns false
  yield(); // do nothing
}

yield() might be needed on some Arduino (like ESP) where the watchdog could bite you if you wait too long in that loop and do not give some processor time to some other stuff. using yield(); on AVR won't hurt, it will just do nothing.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.