Code before Void setup()

Hi All,

A newbie question:

How is code handled that is written before the void setup() instruction?

Would I be correct in thinking that it is scanned only on initialisation & that the code in void setup() section will not be processed until the code above it has been executed?

Thanks.

You can't put code outside a function.

Functions declared above setup() are declared and called just like any other function. The setup() and loop() functions are called by the Arduino core: setup() first and then loop() repeatedly.

mickjbriggs:
How is code handled that is written before the void setup() instruction?

Would I be correct in thinking that it is scanned only on initialisation & that the code in void setup() section will not be processed until the code above it has been executed?

It depends on what sort of 'code' you are referring to.

These sorts of things:

#define ARRAY_SIZE 42
int someValue= 1234;
char anArray[ARRAY_SIZE];

... are declarations of variables to be used in the program. They are not 'executed', but they are set up and initialised (the variable 'someValue' is given the value 1234) before setup();

Things like:

int myAddingFunction( int a, int b ){
 return a + b;
}

are declarations of functions which can be called in setup() or in loop(). They are not executed until they are called.

The other sort of thing you might have is like:

// include the library code:
#include <LiquidCrystal.h>

// initialize the library by associating any needed LCD interface pin
// with the arduino pin number it is connected to
const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);

...here we have an example of a C++ library, the 'initialisation' of an instance of that class (the last line which creates 'lcd', an instance of the LiquidCrystal class) is actually executed.

So, in answer to your question: Yes and No. :slight_smile:

You don't usually think of things written before setup() as being 'executed', but they are 'initialised' ready to be used.

The first thing to be executed in your program will be in setup().

Yours,
TonyWilk

1 Like

Would I be correct in thinking that it is scanned only on initialisation & that the code in void setup() section will not be processed until the code above it has been executed?

No

For amusement try this

void loop()
{
  Serial.println("in loop()");
  delay(1000);
}

void setup()
{
  Serial.begin(9600);
  Serial.println("in setup()");
}

TonyWilk:
It depends on what sort of 'code' you are referring to.

These sorts of things:

#define ARRAY_SIZE 42

int someValue= 1234;
char anArray[ARRAY_SIZE];



... are declarations of variables to be used in the program. They are not 'executed', but they are set up and initialised (the variable 'someValue' is given the value 1234) before setup();

Things like:


int myAddingFunction( int a, int b ){
return a + b;
}



are declarations of functions which can be called in setup() or in loop(). They are not executed until they are called.

The other sort of thing you might have is like:



// include the library code:
#include <LiquidCrystal.h>

// initialize the library by associating any needed LCD interface pin
// with the arduino pin number it is connected to
const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);



...here we have an example of a C++ library, the 'initialisation' of an instance of that class (the last line which creates 'lcd', an instance of the LiquidCrystal class) is actually executed.

So, in answer to your question: Yes and No. :)

You don't usually think of things written before setup() as being 'executed', but they are 'initialised' ready to be used.

The first thing to be executed *in your program* will be in setup().

Yours,
TonyWilk

Thank you.