Basic Coding question on variables

Hi,
After doing some sketches it occurs to me I don't know if variables should be initialized within setup() or outside. For example, which is the preferred initialization spot, here:

int led;

// the setup routine runs once when you press reset:
void setup() {                
  led = 13;
  // initialize the digital pin as an output.
  pinMode(led, OUTPUT);     
}

// the loop routine runs over and over again forever:
void loop() {
  digitalWrite(led, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(1000);               // wait for a second
  digitalWrite(led, LOW);    // turn the LED off by making the voltage LOW
  delay(1000);               // wait for a second
}

or here:

int led=13;

// the setup routine runs once when you press reset:
void setup() {                
  
  // initialize the digital pin as an output.
  pinMode(led, OUTPUT);     
}

// the loop routine runs over and over again forever:
void loop() {
  digitalWrite(led, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(1000);               // wait for a second
  digitalWrite(led, LOW);    // turn the LED off by making the voltage LOW
  delay(1000);               // wait for a second
}

They seem to behave the same in the sketch but am wondering if (for example) I start calling other functions if it could have an effect.

Thanks

If you initialise a variable within a function, including setup(), then it will only be valid within that function, whereas if it is initialised outside of a function it is globally available. NOTE - a variable can be initialised without being given a value.

In the case of your examples led is initialised as a global variable in both but in one case is given a value at the same time and in the other it is given a value within setup()

For readability the second is better as you can group all these constants together at the top of the sketch. Even better is to call them const if they will never change.

Because the led variable has a global scope it will persist and you can change it from anywhere. However, in your first case it does not have a value until setup is called. In the second case the compiler initialises the memory location so it has that value when the program starts running. In the arduino this makes no difference because setup is called first. On other systems it might.

Global variables get initialized to 0 when they are declared without a value. Local variables don't come with that guarantee and should always be initialized when they are created otherwise they may contain some junk value.

UKHeliBob:
If you initialise a variable within a function, including setup(), then it will only be valid within that function

That's misleading - you're confusing definition with initialisation.

The scope of the variable is determined by where it is defined. It can be initialised at the same time, or by code that runs subsequently. The code can be anywhere that has visibility of the variable.

In the specific example given the variable actually represents a constant and should be defined like this:

const int led = 13;

(It would be better if a more meaningful the name was used.)

In the more general case where you are defining a global variable you can reasonably initialise it at the point of defining it, or separately in code. My preference is to do it within the declaration since that makes the code easier to maintain, and also establishes a good habit for the initialisation of automatic (local) variables.

I take your point about definition/initialisation. Sloppy wording on my part.

http://www.cplusplus.com/doc/tutorial/variables/

Read through that. Pay attention to part about scope. Very useful information.