Toggle Functions vs Seperate On and Off Functions

Is it better to define one single function that checks a boolean and switches states based on that then switches it back. Or to define one function to turn the state on and one to turn it off.

The example I have is a set of LEDs. I can define one function to toggle the array on if they are off and vice versa OR two separate functions one for turning it on and one for turning it off.

Programmatically is there any reason to do one over the other? Program Size? Compile time? Memory Management?

Or is it just a style thing? If so is there a style guide or standard that explains why?

The example I have is a set of LEDs. I can define one function to toggle the array on if they are off and vice versa OR two separate functions one for turning it on and one for turning it off.

I would think that it makes very little difference but whatever way you do it make sure that the function(s) have meaningful names.

Personally I would go for the option to change the state of the LEDs rather than separate functions. Did you know that you can flip the state of a pin without explicitly knowing its current state ? One way of doing it is

digitalWrite(ledPin, !digitalRead(ledPin));

Sometimes it depends on the feedback available. The 'power' button on a TV usually has a pilot light to indicate the current state. That is fine when there is a human in the loop that can see the pilot light. However when a home automation system wants to turn on a TV, the system has to blindly send commands and hope the power is being turned ON instead of OFF. That is a case where it would have been much better to provide separate controls for ON and OFF.

If you are saving money by using one momentary button instead of a toggle switch or two momentary buttons then you have to be sure that the current state is clear enough to whoever is pushing the button.

Thank you very much for your answers that is very helpful and I did find in the Arduino library style guide it says "Try to avoid boolean arguments".