Hello. There is a code for ESP8266 in which I control the LED via wi-fi. I would like to know how to start the led_blink function at /On and how to exit it at /Off. Since I did, it doesn't work. The LED just turns on when?On and off on /Off. How do I do this?
This will not work, because your 'timing' variable is a local dynamic variable, that will be newly created and set to 0 with every call to led_blink. You must define this as a static variable.
Wouldn't it be better to omit the while at all and change it to a simple if?
Also the return; isn't really a good idea. loop() should loop - from start to end .
I agree with @MicroBahner. Using return as quick 'n dirty escape from the rest of loop() is not good code structure. Not as bad as using goto, but not good.
Instead, use if/else to skip the remaining code in loop().
I try to use return only in non-void functions, and even then, have only one return statement where the result is returned to the caller.
Because if you add some extra code just before the end of loop(), as @arduino556665 did, it won't be executed because of the return. But if you structure your if/else correctly, you would skip only the parts of the code you intended to skip (in this case when no client is connected) but still execute other code later in loop() that was intended to execute.
This isn't a law, just an opinion! I probably got my opinion from my uni lecturer in structured programming ~40 years ago.
I agree that it a valid reason not to use return in the way that I phrased my question
Perhaps I should have asked, why not use return when all of the required code in loop() has been executed ?
As you say, it is a matter of opinion, but if the logic is followed to its conclusion then the code should never return early from a function even when a value is being returned, but it is common to do so
That's exactly the trap. And I've seen it snap shut so many times in the many decades I deal with programming.
So my experience is not to do so. A function should be left with the last closing brace. And a return that's returns a value should be immediately before that brace. It can save you a lot of troubleshooting.
The only circumstances where I see it valuable to have a return at the beginning of a function, if an error condition is detected that makes it useless to execute the function at all.