Program structure and variable scope

I've been using an Arduino for some time but have only needed small programs. So I didn't care too much about the optimum structure of the program. Now I would like to better understand how the Arduino code works. and how to "properly" write it.

I am not an accomplished C programmed but I learned enough to write reasonable code. I was taught global variables were only to be used when absolutely necessary (like in an interrupt).

  1. My question is: In the Arduino structure below I would like to know the best place to declare local variables for the main loop?

In C I would define them in the void main() routine before the while(1) statement (or some such inner loop)

  1. I downloaded a program that had void setup() after void loop () and other functions seemingly before and after the setup/loop code. Not considering good programming practice, does the compiler care where things are in the code?

compiler directives
#define
global varible declaration etc

void setup ()

voil loop ()

The compiler does care where things are.
You can for example put decorations after loop() to see what happens.

In small programs like on an Arduino, the global variables are bad concept kind of falls apart. You probably aren't going to write a code big enough for it to really matter and still have it fit on an Arduino.

But to properly answer your question, if you want persistent variables in the loop function, then define them with the static keyword so they will hold their value between runs of loop.

void setup(void){
   Serial.begin(9600);
}

void loop(void){
   int a = 0;
   static int b = 0;

   Serial.print("a = ");
   Serial.println(a);
   Serial.print("b = ");
   Serial.println(b);
   a = a + 1;
   b = b + 1;
}

If you run that, it will keep printing 0 for a, but b will count up and up.

I downloaded a program that had void setup() after void loop () and other functions seemingly before and after the setup/loop code. Not considering good programming practice, does the compiler care where things are in the code?

The Arduino IDE does several "helpful" things for you. One of those things is to write function prototypes for you and put them at the top of your code. This means that you can call functions whenever regardless of where you defined them because they are being declared, unbeknownst to you, at the top of the program.

Try that with a variable and it won't work.

Also note that this doesn't always work. For example, if you declare an enum type and then try to define a function that returns that type or takes it as a parameter then the compiler will always complain that your enum type doesn't name a type for the function definition even though it clearly does. What is happening there is that the IDE is "helpfully" writing your prototype for you and placing the prototype before the enum definition. It's a really confusing error because it is erroring out on a line that you didn't write and you didn't put in the code.

When you start getting into heady C++ stuff and complicated programs, you eventually end up abandoning the Arduino IDE over these sorts of issues and use something different to write and compile code against the Arduino core.

larryd:
The compiler does care where things are.
You can for example put decorations after loop() to see what happens.

I tried to put some decorations. I put stickers all over the screen. I added a bit of tinsel. Some glitter glue. The compiler didn't seem to care. :slight_smile:

EDIT: Tried again with emojis as decoration after loop. That got some weird errors.

@Delta_G

I knew that emoji code would give me problems :slight_smile:

Seriously thank for your help, it gives me a better understanding of the Arduino IDE/compiler and how to structure my code.

John

Delta_G:
I tried to put some decorations. I put stickers all over the screen. I added a bit of tinsel. Some glitter glue. The compiler didn't seem to care. :slight_smile:

EDIT: Tried again with emojis as decoration after loop. That got some weird errors.

That's odd, when I cover my screen with butchers paper I don't see any output from the compiler :confused:

Delta_G:
I tried to put some decorations. I put stickers all over the screen. I added a bit of tinsel. Some glitter glue. The compiler didn't seem to care. :slight_smile:

EDIT: Tried again with emojis as decoration after loop. That got some weird errors.

Did you try some candy?

Would that be eye candy?