Good evening,
i'm a beginner in Arduino and i'm trying for the first time to understand funcions. There are some things that i don't understand. here's a sketch:
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
}
int somma,valore1,valore2,valore3;
void loop() {
// put your main code here, to run repeatedly:
valore1=10;
valore2=3;
addizione (valore1); //passo alla funz addizione i numeri contenuti in valore1 e valore2
Serial.println(somma);
delay(300);
}
int addizione (int valore1) //prende i valori come interi
{
somma=valore1+valore2;
return somma;
}
QUESTIONS:
if i give,i pass, valore1 to func. addizione, why it can read valore2 inside of it? valore2 is not passed, how can it read his value (that is 3) ?
if i modify my function addizione to:
void addizione (int valore1) //prende i valori come interi
{
somma=valore1+valore2;
//return somma;
}
it shall not return anything, but it still print me a serial.println(somma) correct, giving me 13 as result. WHY does it work, if void does not return nothing to the Loop?
Thanks
Answer to both questions is, as far as I know (I'm no expert) that those variables are declared outside of any functions, and therefore have global (ie program-wide) visibility.
You'd be wise to learn up on it, it's one of those things that if you don't know it sooner or later you won't be able to progress and write more efficient and 'safe' code (that complex programs can't live without). It's hammered in in first year courses at uni for instance.
Google 'variable scope' (in any popular non-obscure language it'll still make sense).
...those variables are declared defined outside of any functions, and therefore have global (ie program-wide) visibility.
This is quite true in the Arduino IDE, but it is not true for most C compilers. If you define a variable outside a function, as in:
int x;
void setup()
{
// the usual stuff
and open a second source code tab in the IDE and write:
void MyFunction()
{
x = 10;
}
the code compiles without error because the Arduino IDE treats the source code in the additional tabs as "appends" to the *.ino file. The variable x is defined with global scope. That is, the additional "tab" source code files really aren't viewed as separate compile units. However, the same code on other compilers will throw an "undefined variable" error. For most other compilers, global scope pertains to the source code file in which the variable is defined. In the source code files, other that where the data definition is made, you need to add the statement:
extern int x;
This statement says: "I know x is defined in another source file, but let me declare it in this source file so I can use it in this file as an int." The use of the extern keyword tells the compiler that this is a data declaration. The statement without the extern keyword is the data definition of x. Most programmers treat define and declare to mean the same thing: They don't. If they were truly the same thing, Dennis wouldn't have added the keyword extern to the language.