Code before setup() in examples - what is this???

New to Arduino, and not particularly skilled in C or C++, I admit (or am guilty of being)!

But in several of the examples that come with the hardware I have obtained, there is code entered in the sketch before setup(). How does this fit in the grand scheme?

When is this code executed? Once before setup? Between executions of loop()? I'm bewildered!

The code in question is supposedly a routine to listen to the serial port, and depending on what is submitted to the Arduino, the characters are used to determine the path through a case statement.

Any help to bring my understanding up is much appreciated!

And many thanks to the designer of this forum! It is one of the best looking ones that I have seen in a long time (even though the functionality is what we are accustomed to). Eye-candy has its merits!

What you are talking about is a function declaration, that defines a function name, what arguments it excepts and what value it returns if any, and it's code statements. It's executed later whenever the function name is used (called)inside another function like setup or loop or even inside another declared function. This function declaration could be placed at the end of the sketch after the loop function, which is my preference.

Lefty

If you post the code (using code tags), you can get a more detailed explanation of what is going on.

retrolefty: What you are talking about is a function declaration, that defines a function name, what arguments it excepts and what value it returns if any, and it's code statements. It's executed later whenever the function name is used (called)inside another function like setup or loop or even inside another declared function. This function declaration could be placed at the end of the sketch after the loop function, which is my preference.

Lefty

Many thanks, makes perfect sense. And I agree with placing it at the bottom, and using descriptive function names!

Thanks!!