Why won't this compile?

enum hat {
   off, forward, back, left, right };
hat currHat = off;


void setup() {
  // put your setup code here, to run once:

}


hat something(void) {
   
   return back;
}

void loop() {
  // put your main code here, to run repeatedly: 
  
}

Comment out the something function and everything is fine. Has no issues with currHat. What am I missing?

Thanks!

-jim lee

Is this what you are trying to achieve?

The problem is that you didn't supply a prototype for something function. SO the IDE tried to help and put one in there for you. Unfortunately, it puts it in before the enum. I think you can write your own prototype and be OK. I know you can move the whole thing out into a header file and it will work fine.

This compiles:

enum hat {
   off, forward, back, left, right };
hat currHat = off;

hat something(void);    // I added this function prototype

void setup() {
  // put your setup code here, to run once:

}


hat something(void) {
   
   return back;
}

void loop() {
  // put your main code here, to run repeatedly: 
  
}

OH! Duh! Thought I was missing something like that!

That's the kind of thing I never would have considered.

Thanks!

-jim lee

You know, it would be neat to be able to edit compiler error messages. This would be a perfect example to put in the suggestion that “Maybe you forgot to add a function prototype here?” Because next time I run into this, I bet I’ll have forgotten why its being thrown.

-jim lee

You know, it would be neat to be able to edit compiler error messages. This would be a perfect example to put in the suggestion that "Maybe you forgot to add a function prototype here?" Because next time I run into this, I bet I'll have forgotten why its being thrown.

I can think of a lot of things that the compiler should suggest when one does something stupid syntactically incorrect.

However, the problem is not that there was not a function prototype. The problem was that the IDE "helpfully" created one for you IN THE WRONG PLACE.

There are two solutions. One is to not rely on the IDE creating function prototypes for you.

The other is for the Arduino team to put some effort (NOT lip service) into improving the IDE AND showing what it does to mangle your sketch. If you could see, without a bunch of useless effort, what mangling the IDE had done, life would be far easier.