Void not ignored as it ought to be? What?

Sometimes, when Arduino IDE wants You to fix some code, and You have no idea why it is complaining, maybe it’s IDE and not You? :)

See this error, at first glance – WTF?

void value not ignored as it ought to beThis is very specific case, and can make You scratch head for several minutes. But true is very simple.

First of all – there is a bug in code. The only problem is with IDE reporting it in wrong place. So… This relates to Arduino IDE 1.6.7 and void value not ignored as it ought to be error only. This error is reported, when You create a function which has type void, and later You try to use this function as it would be returning some value, for example – assign it to some variable.

The problem is when You have Your sketch in more than one tab, and in one tab You have defined void function and in other tab You use it as it would return some value. Then Arduino IDE throws error,  in correct line number, but in wrong tab. It points You to tab where You have defined function, not where You are using it as non-void function. If this line in other tab have some code, usually reason why error is thrown is completely not understandable :) Especially if that would be line with comment only ;)

In following sketch, the offending line is in first tab:

int val = add_two()

And as You can see, this is an error – we try to assign value returned by add_two() to int variable. Since add_two returns no value, compiler does not know what should be assigned as val.

You can test it by Yourself: example sketch