Documentation descrepancy.

I am confused about the Documentation regarding the setup(). Specifically why is "int buttonPin = 3;" above the Setup() and not within the setup function??

the text below is right off the setup() reference page.

--------------------------------------Reference page------------------------------------------- setup()

The setup() function is called when a sketch starts. Use it to initialize variables, pin modes, start using libraries, etc. The setup function will only run once, after each powerup or reset of the Arduino board.

Example

int buttonPin = 3;

void setup() { Serial.begin(9600); pinMode(buttonPin, INPUT); }

void loop() { // ... }

If it's within the setup() function, it is local to there. It if is prior to setup(), it is global and all functions can access it.

Because the declaration of buttonPin would not work if it where in setup(). Look up "scope"

Mark

holmes4: Because the declaration of buttonPin would not work if it where in setup(). Look up "scope"

Mark

It would work perfectly well, within "setup()"

The documentation you quoted says you can "initialize" variables in setup. It doesn't say you can declare them there. Thus you can say,

int buttonPin;

void setup()
{
  buttonPin = 3;
  Serial.begin(9600);
  pinMode(buttonPin, INPUT);
}

and buttonPin remains visible in loop().