condition-function order

Hi.

Is there any difference between

void thingstodo(){
if (condition) {
do something
}
}

void loop(){
thingstodo();
}

and

void thingstodo(){
do something
}

void loop(){
if (condition) {
thingstodo();
}
}

No, if condition is something global. Aka, the scope of both is different.

And both are indented terrible ;)

septillion: No, if condition is something global. Aka, the scope of both is different.

And both are indented terrible ;)

thanks :) //have been waiting for 5 minutes to be able toreply :D

Actually, there are differences. In the first case, the function is called, even if there is nothing for it to do. In the second, it is not.

So, the function in the first case should be called doThingsIfNecessary(), since it may not always do things, while the function in the second case can be call doThings(), since it will always do things.

In most cases, the overhead of a function call will be insignificant. But, not always.

PaulS: Actually, there are differences. In the first case, the function is called, even if there is nothing for it to do. In the second, it is not.

So, the function in the first case should be called doThingsIfNecessary(), since it may not always do things, while the function in the second case can be call doThings(), since it will always do things.

In most cases, the overhead of a function call will be insignificant. But, not always.

Hi PaulS. Thanks again.

I wanted to know if there is an impact on the arduino ressources (i don't know , but let's say available RAM? SRAM? latency?).

In other words, does calling a function then check the condition to do something make the arduino "more" tired then cheking a condition before calling the function to do something?

I think the answer is yes but only by a very very very tiny amount that in almost all cases isn't worth thinking about. It is usually much more advantageous to try to contain the scope of condition than to save one or two clock cycles.

Delta_G: I think the answer is yes but only by a very very very tiny amount that in almost all cases isn't worth thinking about. It is usually much more advantageous to try to contain the scope of condition than to save one or two clock cycles.

Thanks ^^

A good rule of thumb, don't try to outsmart the compiler ;) If you really want to do that, write assembly :)