Requirements for using functions in conditions

Hi there,

My question is rather simple: Do I have to "execute" functions prior to using them/their value in an if-condition?

For example:

void loop() 
{
  sideA();
  sideB();
 
  if(sideA() == true || sideB() == true)
  {
    digitalWrite(relais, HIGH);
    delay(100);
    digitalWrite(relais, LOW);
    delay(1000);
}

Do I need sideA(); and sideB(); here or will they be executed when their value is checked in the if-function?
I've been trying to find out with the serial Monitor but it just confused me because I got seemingly random messages that sideA() or sideB() got executed.

Thank you in advance,

Smagel

Functions will be executed when called. Currently you code code calls both of them twice. You only need to call them in the if clause.

If your sideA() and sideB() functions return a boolean value then you could do this

if(sideA() || sideB())

Thanks alot for the quick answers!

Ok, that's what I hoped for.

Yes, they're booleans, thanks for the tip!

On the other hand ...

I prefer to save the function value to a variable and use the variable in the IF statement because, for debugging, it allows me to print the variable to check it before it is used in the IF statement

  sideAval = sideA();
  sideBval = sideB();

  // can put debugging Serial.print statements here
 
  if(sideAval== true || sideBval == true)

...R

There's nothing special about the if condition, its just an expression like any other - it the result
of the expression is non-zero, the then-clause is taken, otherwise the else-clause is taken.

Functions are objects in their own right, so you can pass them around by name, just like any value.
To call a function you must use (...) after the function name, that is a function call expression.

So to answer your question simply ask yourself "do I want the function called, or merely treated as
a function object"

For instance if a function is in a variable you might want to compare the variable to NULL to see
if its been set.

Typically though you aren't doing this, you are calling functions by name.

Take care when using functions in an if statement.

If all that a function does is to evaulate a condition and report true or false then all is okay.

However, if the functions do more than evaluate a condition you need to be aware that the second function my not always be called.

consider the following

bool SideA() {

  myGlobalVariable += 1;
  if myGlobalVariable >= 10
    myGlobalVariable = 0;

  return (myGlobalVariable >= 3);
}

bool SideB() {

  static bool toggle;

  
  digitalWrite(togglePin, toggle);
  toggle = !toggle; // toggle toggle for the next time we want to toggle the togglePin
  return (!toggle); // return what toggle was before we toggled it.
}

void setup() {
  ...
}

void loop {
  ...
  if (SideA() && SideB()) {
    doSomethingNow();
    ...
  }
  ...
}

Naff functions I know :slight_smile: but...

In the code above, if SideA() return false, SideB() never gets evaluated/called
If your code depends on both functions actually running because they assign values/perform operations you should call the functions outside of the if statement and assign the results to a couple of boolean variables and use those variables in the if statement.

There is a term, 'Short-circuit evaluation'. This is where "the second argument is executed or evaluated only if the first argument does not suffice to determine the value of the expression."
Both && and || are what is known as 'short circuit operators'.