Learning C++/IDE

Hi i'm the new guy :slight_smile:

I try to learn how to program my arduino from the learning section on this site, but i find it a bit confusing.

I'll give an example :

The example code for Setup. I expected to find all the code within setup() but some of the code is outside.
Why is that ?
because of this i still fail to understand the very basic structure of the IDE, can someone point me to a better place to learn arduino code ?

 int buttonPin = 3;

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

void loop()
{
  // ...
}

Hello and welcome,

buttonPin is a variable declared outside of the setup() function so that it can be used in the loop() function, or any other functions. It's called a global variable, because it is accessible from anywhere in the code.

If you declared it like so:

void setup()
{
  int buttonPin = 3;

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

Then it wouldn't be accessible outside setup(). It is then called a local variable, and its scope is limited to within setup()

More information here: Variable Scope in C++

I expected to find all the code within setup() but some of the code is outside.
Why is that ?

There is no executable code outside any function.

There are definitions of variables with one-time initialisers, but no "code".

AWOL:
There is no executable code outside any function.

There are definitions of variables with one-time initialisers, but no "code".

Ok so definitions can be anywhere in the sketch (Being local or global) but executable's can only be in the functions() ?

Ok so definitions can be anywhere in the sketch (Being local or global) but executable's can only be in the functions() ?

Yes.