-->
Page 1 of 1

Function not returning a value causes exception

PostPosted: Fri Sep 10, 2021 11:11 am
by cjdshaw
Very basic bit of code:
Code: Select allint initFunc() {
}

void setup() {
  Serial.begin(19200);
  initFunc();
}

void loop() {
}


So initFunc should return an int but doesn't, but setup ignores the return value. In C, this would generate a
warning: control reaches end of non-void function [-Wreturn-type]
but runs fine.
On Arduino/ESP8266 it compiles fine, but then triggers an exception, which seems extreme. Is there a logic to this that I'm missing, or is it a bug?
Using board 3.0.2 and Arduino 1.8.13

Re: Function not returning a value causes exception

PostPosted: Thu Sep 16, 2021 12:46 pm
by prgvitor
cjdshaw wrote:Very basic bit of code:
Code: Select allint initFunc() {
}

void setup() {
  Serial.begin(19200);
  initFunc();
}

void loop() {
}


So initFunc should return an int but doesn't, but setup ignores the return value. In C, this would generate a
warning: control reaches end of non-void function [-Wreturn-type]
but runs fine.
On Arduino/ESP8266 it compiles fine, but then triggers an exception, which seems extreme. Is there a logic to this that I'm missing, or is it a bug?
Using board 3.0.2 and Arduino 1.8.13



How must insert in the fuction "return", like that:

int function(void){


return 0; // Or other int value

}

If the function dont need returna a value, you must declare by "void", like that:

void function(void){


}