Go Down

Topic: Basic Coding question on variables (Read 585 times) previous topic - next topic

memotick

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:
Code: [Select]

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:
Code: [Select]
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

UKHeliBob

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()
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

marco_c

#2
Feb 08, 2013, 09:16 pm Last Edit: Feb 08, 2013, 09:45 pm by marco_c Reason: 1
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.
Arduino libraries http://arduinocode.codeplex.com
Parola for Arduino http://parola.codeplex.com

Delta_G

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.

PeterH


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:

Code: [Select]
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 only provide help via the forum - please do not contact me for private consultancy.

UKHeliBob

I take your point about definition/initialisation.  Sloppy wording on my part.
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

Jimmy60

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

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

Go Up