Pages: [1]   Go Down
Author Topic: Basic Coding question on variables  (Read 524 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Full Member
***
Karma: 0
Posts: 158
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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:
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
Logged

East Anglia (UK)
Offline Offline
Faraday Member
**
Karma: 116
Posts: 4317
May all of your blinks be without delay()
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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()
Logged

Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

Sydney, Australia
Offline Offline
Edison Member
*
Karma: 33
Posts: 1283
Big things come in large packages
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
« Last Edit: February 08, 2013, 03:45:30 pm by marco_c » Logged

Arduino libraries http://arduinocode.codeplex.com
Parola hardware & library http://parola.codeplex.com

Offline Offline
God Member
*****
Karma: 19
Posts: 785
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

UK
Offline Offline
Shannon Member
****
Karma: 223
Posts: 12630
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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.
Logged

I only provide help via the forum - please do not contact me for private consultancy.

East Anglia (UK)
Offline Offline
Faraday Member
**
Karma: 116
Posts: 4317
May all of your blinks be without delay()
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

Saskatchewan
Offline Offline
Sr. Member
****
Karma: 19
Posts: 364
When the going gets weird, the weird turn pro. - Hunter S. Thompson
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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

Pages: [1]   Go Up
Jump to: