Does the & is mandatory in a bool function

Hello

I have a stupid question.

Here is my function which work fine...

txInterval(1544476926);
bool txInterval(int32_t &lastTx)
{ 
    int32_t nextTx = lastTx + (int32_t)TX_INTERVAL;
    int32_t unix_t;  
    DateTime t = rtc.now();

    //Serial.println(now.unixtime());
    unix_t = t.unixtime();
    Si.sprint(unix_t,2);
    Si.sprint(F(" - "),2);
    Si.sprintln(nextTx,2);
 
    if(unix_t > nextTx)
    {
      return true;
    }
    else
    {
      return false;
    }
}

Do I need to add the '&' here, as I am only passing a value but I use the get a result of a value?

bool txInterval(int32_t &lastTx)

I have asking this question because one I use a fncton similar to this one

int32_t ResistanceInput = 1223;
int16_t CTemperatureInput =23;
int16_t swp;
kPaCalc(ResistanceInput, CTemperatureInput, swp)
Serial.println(swp); 

void kPaCalc(int32_t ResistanceInput, int16_t CTemperatureInput, int16_t &swp2){
 
  swp2=0;
  float ResistanceCompensated =  ResistanceInput *(1 + 0.001*((CTemperatureInput * 1.8 + 32)-75));
  Si.sprint(F("WRM2: "),2); Si.sprint((int16_t)ResistanceCompensated,2); Si.sprint(F(" Ohm"),2);

  
    if (ResistanceCompensated <= SWPkPAarray[0]) {     // Minimum value
    //Serial.print(F("swp2a:")); Serial.println(swp2);
  }
  if (ResistanceCompensated >= SWPkPAarray[16]) {     // Maximum value
    swp2 = SWPkPAarray[16+1];
    //Serial.print(F("swp2b:")); Serial.println(swp2);
  }  
  //for (int i=0; i<SWPkPAarray.length-2; i=i+2) {
  for (int i=0; i<16; i=i+2) {
    if ((ResistanceCompensated >= SWPkPAarray[i]) && (ResistanceCompensated <= SWPkPAarray[i+2])) {
      //swp2 = SWPkPAarray[i+1] - ((SWPkPAarray[i+1]-SWPkPAarray[i+3]) * ((ResistanceInput-SWPkPAarray[i]) / (SWPkPAarray[i+2]-SWPkPAarray[i])));
      swp2 = SWPkPAarray[i+1] - ((SWPkPAarray[i+1]-SWPkPAarray[i+3]) * ((ResistanceCompensated -SWPkPAarray[i]) / (SWPkPAarray[i+2]-SWPkPAarray[i])));
      break;
    }
  } 
}

In the above code, if I missed the & of '&swp2', my print was uncorrect

Then, as I am only passing a value, I wonder if this is better

bool txInterval(int32_t lastTx)

than

bool txInterval(int32_t &lastTx)

or if it's the same

[...]

Thank

I am only passing a value

In order to pass a value (ie a copy of the value from the source variable) you should not use the &. The function will not be able to change the value of the source variable

If you use the & then you are passing by reference (ie giving access to the actual source variable) and the function will be able to change it directly

In either case you can return a variable if required

Hello
Thank for your reply.

int32_t lastSent = 1544476926; 
#define TX_INTERVAL 60

if(txInterval(lastSent)){
Serial.println("Next neasure can be sent");
}

bool txInterval(int32_t lastTx)
{
   int32_t nextTx = lastTx + (int32_t)TX_INTERVAL;
   int32_t unix_t; 

   DateTime t = rtc.now();
   //Serial.println(now.unixtime());
   unix_t = t.unixtime();
   Si.sprint(unix_t,2);
   Si.sprint(F(" - "),2);
   Si.sprintln(nextTx,2);

   if(unix_t > nextTx)
   {
     return true;
   }
   else
   {
     return false;
   }
}

Then as lastSent is a reference value of the unixtime of the last sent measure, it should not chnaged into the function.

Then I removed the &

Thanks for your clarification

I think the "Arguments passed by value and by reference" and "Efficiency considerations and const references" sections of this page do a good job of explaining the topic:
http://www.cplusplus.com/doc/tutorial/functions/

pierrot10:
Then I removed the &

An even better choice would be to add const...

bool txInterval(int32_t const lastTx)