WAY OFF TOPIC, but I would like some help getting my head around this.

Re declaration and definition, I think you'll have to hit the books but I found this on the web.

In The Definition of a variable space is reserved and some initial value is given to it, whereas a declaration only identifies the type of the variable for the function. Thus definition is the place where the variable is created or assigned storage whereas declaration refers to places where the nature of the variable is stated but no storage is allocated.

I admit I get them confused as well but the following is right I think

void myFunc(void);    // declaration of myFunc() so the compiler knows what to expect if the function is referenced before it is defined
//
// lots of other code
//
void myFunc(void) {   // definition if myFunc, this is the bit that actually has the code
   // some code
}

You need the declaration if you have a forward reference to a function for example

loop () {
   myFunc();
}

void myFunc(void) {   
   // some code
}

Will cause an error because the compiler sees the reference to myFunc() before it knows what the heck it is. One way to fix this is to declare the function first.

void myFunc(void);   // this is also called a "prototype"

loop () {
   myFunc();
}

void myFunc(void) {   
   // some code
}

The declaration (AKA prototype) tells the compiler what myFunc() is so when it sees the reference in the loop() function it's happy in the knowledge that myFunc() exists, the call is correct WRT parameters and return type, and that at some point myFunc() will be defined.

You can also fix this by simply placing myFunc() before any code that references it but often this is not practical.

The Arduino IDE pulls a trick to help beginners with this. It scans the program for forward references and (I think) inserts declarations for you.


Rob