Setup() vs writing above in the header

Hi, new here.

It's been a while since I have programmed and I'm teaching my young kids with the Arduino.

Anyway..

What's the difference between setting up your variables, pin modes or libraries with "void setup()" verses putting it above it in the header?

It seems like either works, right? I think I see examples with both. They each only run once? does it behave to same after a reset?

Thanks!

Everything in setup() is local to setup. Whereas everything above setup is global. So if you were to define a variable in setup, the rest of your program would not see it.

When you declare variables outside a function they are global and can be accessed throughout the program. But when you declare variables within a function they are only visible within the function and are normally discarded when the function ends. If you want local variables to persist you can prefix them with "static".

pinMode() is different because it is a call to a function and that should go within setup().

Note that the function's name is "setup" and the word "void" is there to tell the compiler that the function does not return any value when it completes.

...R

You might also look at post #6 in:

http://forum.arduino.cc/index.php?topic=228630.5

Your questions are leading into a discussion of a programming concept called scope.

pinMode can be used within loop() also, if you wanted to change how a pin was used.
An example could be a "soft-I2C" with 1 master and several slaves. The master, the 328P in this case, could set SCL as an output, but SDA would need to toggle between output to send slave addresses and data, and then input to receive results back from slave.