unsigned int functions in tabs -> "not declared"


I've put some functions of type unsigned int in a tab, but I get:

In function 'void loop()': error: '[functionname]' was not declared in this scope

when I try to compile.

However, if I change the function in the tab from:

unsigned int functionname() { ... }


int functionname() { ... }

or if I move the unsigned function to the main tab, everything works.

Why so annoying?


try declaring your function in your “main” tab

unsigned int function();

then in your other tab, define the function as usual:

unsigned int function()
  // function body goes here

kg4wsv has the right suggestion.

What's happening is that normally in C/C++, functions must be declared (with a single line giving their name, return type and arguments as in kg4wsv's post) before they're used. The actual definition of the function (i.e. its code) can then come later, but the compiler (which turns your sketch into a binary file for the microcontroller on the Arduino board) needs to know the name and type of function before you call it.

The Arduino environment attempts to automatically generate those function declarations for you by looking for the functions you define in your sketch and silently inserting the declarations before passing the code to the compiler. Unfortunately, this detection of your functions doesn't work perfectly, and seems to miss functions whose return type has multiple words (like "unsigned int"). I've added this to the bug list: https://developer.berlios.de/bugs/?func=detailbug&bug_id=11447&group_id=3590, but in the meantime, you can follow kg4wsv's suggestion and creating the function declarations yourself, you can put the the function above the places where you call it, or you can change it to return an int.

OK, thanks!

I thought the environment automatically inserted function prototypes, which was why one could use tabs, but so there's a bug. Is there something I can do (change some config files or whatnot) to over come this problem? This makes tabs kind of useless for my program, since I've got a bunch of unsigned int one-liner functions, I might as well just copy the whole function over.


You could try creating a typedef for unsigned int and then using it instead. I.e. at the top of the tab, put:

typedef unsigned int unsignedint;

And then use "unsignedint" instead of "unsigned int".

That's a great idea, but it doesn't seem to work? I get:

error: 'unsignedint' does not name a type In function 'void loop()':

If I remove all references to the functions in the tab from void loop(), I get:

error: 'unsignedint' does not name a type

In fact, I get the same error even if the typedef and the functions are in the main tab. Do typedef's even work for unsigned int?

This is getting weird. It says:

error: 'unsignedint' does not name a type

even if I comment out everything in the tab, with nothing in the main tab. I had to completely delete all the code from the tab to get it to compile!

I think I'll avoid tabs for a while...


Ah, crap. Yea, the automatically generated prototypes get inserted at the top of your code, above the typedef. You could try something like uint16_t which might already be defined in a header file that gets included above the function prototypes (I think).

Oh MAN! I pressed "save" after I deleted all my SPI functions that took me hours to write last night! >:( I've got most of them rewritten now, it didn't take long since last night I was suck on some small error for a while.

Anyway, good news is that uint16_t works perfectly!

Thanks a lot!