Why volatile cannot be pass-by-reference

Montmorency:
What is it doing here and why? Show us an example of how you expect to use it.

Montmorency , I just tried to implement logic from other languages.
where I can make like this

func(arg = 1, var = 10){
print arg,var
}

func() // print 1, 10
func(5, 5) //print 5,5

or I can make this

func(arg, var){
arg = (arg==NULL) ? 1 : arg;
var = (var==NULL) ? 10 : var;
print arg,var
}

func() // print 1, 10
func(5, 5) //print 5,5

first method, due to your explain, I cannot use in C++ when I send temporary value (default value) to volatile reference... so now I just want understand can I make in C++ like 2nd method for my volatile reference

Use a pointer instead of a reference:

void aFunction(const volatile uint8_t *ptr = nullptr);

void setup() {
  aFunction();        // call function with default of nullptr
  aFunction(&PINB);   // call function with address of PINB register
}

void loop() {
}

void aFunction(const volatile uint8_t *ptr){
  if (ptr) {
    // Do stuff with pointer to register like:
    uint8_t x = *ptr;
  } else {
    // Do default stuff if no pointer supplied
  }
}

EDIT:no this is not correct code

gentlemen, the following code is passing by compiler but could you please check if it valid as logical, if it make sense, if it applicable to use with real data/real scenario

#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 volatile uint8_t *port, const uint8_t pin){
	wdt_enable(wdt_prescale);
	while(*port & pin);        //is it correct expression? 
	wdt_disable();
}

void soft_reset(){
	uint8_t port = 1;
	uint8_t pin = 1;
	soft_reset(WDTO_15MS, port, pin);				//is this correct call?
}

void isPIRHigh(){
	soft_reset(WDTO_8S, ~*portd, pinint0);			//is ~*portd will work as expected ? will be compared inverse value in while?
}

void setup(){
isPIRHigh();										//no issue here?
soft_reset();										//no issue here?
soft_reset(WDTO_15MS, PIND, 1);						//no issue here?
}

Gentlemen, is this logic correct now ? especially this line soft_reset(WDTO_15MS, port, pin, level);

void soft_reset(const uint8_t wdt_prescale, const volatile uint8_t *port, const uint8_t pin, const uint8_t level){
	wdt_enable(wdt_prescale);
	while(level ^ (*port & pin));
	wdt_disable();
}

void soft_reset(){
	const uint8_t port = 1;
	const uint8_t pin = 1;
	const uint8_t level = 0;
	soft_reset(WDTO_15MS, port, pin, level);  //"port" is this correct call ?
}

void isPIRHigh(){
	soft_reset(WDTO_8S, &PIND, PIN_INT0, PIN_INT0);					//expect PD0 high
}

alexblade:
Gentlemen, is this logic correct now ?

What did your compiler tell you?

Should be

	soft_reset(WDTO_15MS, &port, pin, level);

yes , exactly ! :slight_smile: I did it the same myself :slight_smile:
thank you Montmorency!

so now looks like code is correct ? and always in WHILE it will read realtime(volatile) state of port ?

void soft_reset(const uint8_t wdt_prescale, const volatile uint8_t *port, const uint8_t pin, const uint8_t level){
	wdt_enable(wdt_prescale);
	while(level ^ (*port & pin));
	wdt_disable();
}

void soft_reset(){
	const uint8_t port = 1;
	const uint8_t pin = 1;
	const uint8_t level = 0;
	soft_reset(WDTO_15MS, &port, pin, level);
}

void isPIRHigh(){
	soft_reset(WDTO_8S, &PIND, PIN_INT0, PIN_INT0);					//break when PD0 high
}

void isPIRLow(){
	soft_reset(WDTO_8S, &PIND, PIN_INT0, !PIN_INT0);				//break when PD0 low
}

void isPIRAlive(){
	isPIRHigh();
	isPIRLow();
}