When a program requires frequent tests in the loop(), there seem to be two ways of doing so, both which work. Are the following equally efficient and acceptable? Or is one 'best practice'?
This:
void loop()
{
xyz();
// Other commands in loop()
}
void xyz()
{
// Frequently check for condition, e.g.
if (abc == def)
{
// function commands
}
}
Or this:
void loop()
{
// Frequently check for condition, e.g.
if (abc == def)
{
xyz();
}
// Other commands in loop()
}
void xyz()
{
// function commands
}
They are both acceptable and you can use either.
In the first case, your loop function will be cleaner and easier to understand, the second option is executed a little faster.
Then the test inside the function is preferable if the test may be extended later. Instead the test in loop() can better reflect global conditions, independent from the implementation in the function.
void loop() {
if (function_allowed) abc();
}
void abc() {
if (condition_met) ...
else if (other_condition) ...
}
In the first case, there will be a function call and an if on every single interation;
In the second case, there will only be the if in cases where abc != def
Highly unlikely to be significant.
And, as @GoForSmoke said, the call may well be inlined anyhow ...