The ESP32 IDE compiler is intolerant

When you declare a function returning a value, you must use a return statement to exit the function, otherwise the program crashes. The board is no longer visible on the COM port, and the colored LED flashes.
Example:
The following code runs perfectly on all my Arduino boards (Nano Every, Nano 33 BLE, nano 33 IoT and ESP 32):

bool light;

int test(void)
{
  digitalWrite(13, light);
  light=!light;
  return 10; // This statement is necessary for ESP32 if 
                    // the function returns a value
}

void setup()
{
  pinMode(13, OUTPUT);
}

void loop()
{
  test();
  delay(500);
}

But if I remove the return statement (which is not a good practice), the code continues to work on all my boards except on the ESP 32 where it crashes.

The result of running incorrect code is undefined.

Interesting. For me, a function with return type int, and no return statement:

  • Compiles with a warning when building for Arduino Uno R3
  • Fails to compile when building for ESP32 Dev board

I'm using Arduino IDE 1.8.13. I've not tried running the code on either board.

Can you print the return value?
I expect it will return 0 an 1 alternatingly.

Serial.println(test(), HEX);

You are right, there is a warning at compilation. I did not see it.

I can't print the return value since the code crashes.

So fix your code and move on. Or go down the rabbit hole of the ABI and figure out why it crashes. Probably has something to with loading something wrong into the return register or messing up the stack on the return.

Hi @frenchy22. The developers of the "esp32" boards platform recently adjusted the configuration of the platform so that the compilation will fail if there are missing return statements in non-void functions:

If you are using the "Arduino ESP32 Boards" platform of the Arduino Nano ESP32 board, that beneficial change should eventually be pulled in to the platform, since it is based on the "esp32" platform. However, the change is not yet present in Arduino ESP32 Boards because it is trailing behind the ongoing development work in the "esp32" platform.

Thank you Ptillisch, it's a good information.