void and int function

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: 1) 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) ? 2) 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 :)

1) 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) ?

Because the variable has global scope.

WHY does it work, if void does not return nothing to the Loop?

Because the variable has global scope.

Please get into the habit of posting code inside code tags

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.

edit... yay seems I was right, thanks Nick.

As has been mentioned, it's about 'scope'.

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.

Delta_G: Let it go... :)

econjack: Delta_G: Let it go... :)

What the hell is that supposed to mean?

You don't remember our go-around over define and declare? Probably a good thing.

econjack: You don't remember our go-around over define and declare? Probably a good thing.

I remember, but I don't see how trying to provoke another would be productive.

And I don't see anything in your previous post here on this thread that i would disagree with. If you had understood the earlier argument, you would know that. So I don't see the need to call me out by name here. Our earlier tiff wasn't about that difference, if you had been able to understand we were in total agreement about that difference. Our issue was about the difference between two logical assertions made in the English language.

Unless you're just trying to be a jerk. In which case, carry on.

Delta_G: Sorry...I should have known better.