Better coding practising for comparators and if statements?

Say I have the following functions,

int val1,val2,val3,val4;
void test_1()
{
  if(val_1 < val_2)
  {
    //...
  }
  if(val2 > val3)
  {
  
  }
}

void test_2()
{
  if(val1 > val2)
  {
    //...
  }
  if(val2 < val3)
  {
  
  }
}

As you can see the comparator operators are switched in test_2.
I d like to be able to simplify this as one function. So because both functions are almost the same it would make more sense to have one general function. Obviously you cant pass comparator operations as function arguments so I was wondering whats the best way to do this. One suggestion that i v proposed is the following,

int val1,val2,val3,val4
void test_1(int val1, int val2)
{
  if(val_1 < val_2)
  {
    //...
  }
  if(val2 > val3)
  {
  
  }
}

//...

test_1(val1, val2);

//...

test_1(val2, val1);

So I could just switch the arguments that are passed but are there better ways?

depends on the application, but generally you've hit it on the head, have a generic function, and switch the arguments when calling it.

Or you could try standard math.h isgreater macro , assuming it works in GCC.

/* isgreater example */


#include <stdio.h>      /* printf */
#include <math.h>       /* isgreater, log */

int main ()
{
  double result;
  result = log (10.0);

  if (isgreater(result,0.0))
    printf ("log(10.0) is positive");
  else
    printf ("log(10.0) is not positive");

  return 0;
}

I have an idea, Vaclav. Rather than assuming, how about testing it yourself?

#include <math.h>       /* isgreater, log */
void setup ()
  {
  Serial.begin (115200);
  Serial.println ();
  }  // end of setup
void loop ()
  {
  double result;
  result = log (10.0);
  if (isgreater(result,0.0))
    Serial.println ("log(10.0) is positive");
  else
    Serial.println ("log(10.0) is not positive");
  }  // end of loop

Nope, doesn’t work:

sketch_jul11b.ino: In function 'void loop()':
sketch_jul11b:15: error: 'isgreater' was not declared in this scope
'isgreater' was not declared in this scope
int val1,val2,val3,val4
void test_1(int val1, int val2)
{
  if(val_1 < val_2)
  {
    //...
  }
  if(val2 > val3)
  {
 
  }
}

Why pass down two things (val1 and val2) but also test global variables val2 and val3? It looks like a bit of a mess like that.