Go Down

Topic: How to send and REFERENCE value and DEFAULT value to function (Read 235 times) previous topic - next topic

alexblade

Code: [Select]
void soft_api(int &port = 1, int &pin = 1){}

I get error cannot convert int to int&

gfvalvo

The parameters will need to be 'const'.

And, supply your own prototype:

Code: [Select]
void soft_api(const int &port = 1, const int &pin = 1);

void setup() {
  soft_api();
}

void loop() {
}

void soft_api(const int &port, const int &pin) {
}
No technical questions via PM. They will be ignored. Post your questions in the forum so that all may learn.

Montmorency

A non-const lvalue reference cannot be bound to a temporary value in C++. Which is why your code does not compile.

One way to literally supply "default values" to your references is to declare global/static variables for that purpose

Code: [Select]
int default_port = 1;
int default_pin = 1;

void soft_api(int &port = default_port, int &pin = default_pin){}


Keep in mind though that if you modify the referenced value inside this function the modification will affect the global variable, i.e. it will "stick".

---

An alternative approach would be to use function overloading instead of default arguments

Code: [Select]

void soft_api(int &port, int &pin){}

void soft_api(int &port)
{
  int pin = 1;
  soft_api(port, pin);
}

void soft_api()
{
  int port = 1;
  soft_api(port);
}


---

Of course, the real question here is why your references are not to `const`. Are you intending to modify the referenced values inside the function? If not, then you can simply use references to `const`

Code: [Select]

void soft_api(const int &port = 1, const int &pin = 1){}


(but in this case the question would be why you are even using references instead of plain values.)

alexblade

thank you guys!

did it as const, willsee what happen :)

gfvalvo

(but in this case the question would be why you are even using references instead of plain values.)
Since OP is passing a const, using a reference is only advantageous if the parameter being passed is a large object (I'd probably use a pointer). In that case, pass-by-value wastes space putting the value on the stack and needs to invoke the class copy constructor. For passing a couple ints (which should probably be uint8_t anyway), using a reference is kind of silly.
No technical questions via PM. They will be ignored. Post your questions in the forum so that all may learn.

alexblade

because reference and pointer ONLY could check realtime data in my case port register. pass - by - value will send me to func out dated value of port register :)
sure if I correct understand manuals and logic, but I correct understand it ;)

gfvalvo, buy the way, make sense to make all my passing as const ?
since I many time pass the same value like
func1(const &port)
func2(const &port) {func1(port)}
func3(const &port) {func2(port)}
func4(const &port) {func3(port)}

alexblade

ok now other error
if put const I get error
Code: [Select]
binding 'volatile uint8_t {aka volatile unsigned char}' to reference of type 'const uint8_t& {aka const unsigned char&}' discards qualifiers

if I remove const I get error
Code: [Select]
invalid initialization of non-const reference of type 'uint8_t& {aka unsigned char&}' from an rvalue of type 'uint8_t {aka unsigned char}'


my code
Code: [Select]

#include <avr/wdt.h>

volatile uint8_t &portd = PIND; //interrupts port
const uint8_t pinint0 = 1 << INT0; //pin INT0

void soft_reset(const uint8_t wdt_prescale, const uint8_t &port = 1, const uint8_t &pin = 1){
wdt_enable(wdt_prescale);
while(port & pin);
wdt_reset();
wdt_disable();
}

void isPIRHigh(const uint8_t &port, const uint8_t &pin){
soft_reset(WDTO_8S, ~port, pin); //finish when PD0 high
}

void isPIRLow(const uint8_t &port, const uint8_t &pin){
soft_reset(WDTO_8S, port, pin); //finish when PD0 low
}

void isPIRAlive(){
isPIRHigh(portd, pinint0);
isPIRLow(portd, pinint0);
}

void setup(){isPIRAlive();}

RayLivingston

What on earth is the point of passing by reference, for a simple data type like int, if the value is never going to be changed?  It is pointless, and unnecessary.

Regards,
Ray L.

alexblade

even if I remove reference for pin error the same. I cannot remove reference from portd



Go Up