global vs local variable/memory

Hi.
I want to know how these differents codes would affect memory:

code1:

int variable = 0;

void setup() {
  // put your setup code here, to run once:

}

void loop() {
  // put your main code here, to run repeatedly:
  variable++;
}

code2:

void setup() {
  // put your setup code here, to run once:

}

void loop() {
  // put your main code here, to run repeatedly:
  static int variable = 0;
  variable++;
}

code3:

void function1() {
  static int variable = 0;
  variable++;
}
void setup() {
  // put your setup code here, to run once:

}

void loop() {
  // put your main code here, to run repeatedly:
  function1();
}

code4:

int variable = 0;
void function1() {
  variable++;
}
void setup() {
  // put your setup code here, to run once:

}

void loop() {
  // put your main code here, to run repeatedly:
  function1();
}

code5:

int variable = 0;
int function1(int var) {
  var++;
  return var;
}
void setup() {
  // put your setup code here, to run once:

}

void loop() {
  // put your main code here, to run repeatedly:
  function1(variable);
}

code6:

int function1(int var) {
  var++;
  return var;
}
void setup() {
  // put your setup code here, to run once:

}

void loop() {
  // put your main code here, to run repeatedly:
  static int variable = 0;
  function1(variable);
}

and also is there a differene between code 4 and code 5 because i think the result is the same (a global variable that can be used by any other function)

Thanks.

At least the first three four don't compile, so the answer to the question is "not at all"

AWOL:
At least the first three four don’t compile, so the answer to the question is “not at all”

oops, sorry. It’s my keybord (sometimes does not detect ‘b’ pressed :D) so it’s just the “variable” written “variale”. I’ll correct it. thanks

What does "affect memory" mean to you? All the codes need to reserve space for the variable, for the life of the sketch. The only differences are the scope of the variable.

What is it that you are really trying to learn?

fjtheknight:
I want to know how these differents codes would affect memory:

Just from a cursory look I’d summarizes the differences as follows.

Both 1 and 2 produce the same effect. Both store the variable “statically”, as in it is only initialized once when the code runs and is persistent. The difference is that the scope in example 2 is only in loop(). That is the advantage of the static declaration. It allows you to have the persistence of a global variable while retaining the scope limitations of a local variable.

Essentially it’s the same situation for 3 and 4. The main difference is that in 3 the variable is only accessible in the function, but other than that it works in the same way as 4.

In examples 5 and 6, the var is not incremented in either case, as this “var” is passed by value to the function and therefore remains unaltered when the function returns.

fjtheknight:
and also is there a differene between code 4 and code 5 because i think the result is the same (a global variable that can be used by any other function)

No they're definitely not the same. In code 5, although "variable" is global, when it is used it is passed to "function1" by value. What this means is that function1 makes a local variable ("var") on the stack, and this is the thing that gets incremented, NOT "variable".

The incremented value of "var" would be returned if anything was assigned to the left hand side in the function call, but this doesn't happen. So the net effect is that neither code 5 or 6 increment "variable".

stuart0:
Just from a cursory look I'd summarizes the differences as follows.

Both 1 and 2 produce the same effect. Both store the variable "statically", as in it is only initialized once when the code runs and is persistent. The difference is that the scope in example 2 is only in loop(). That is the advantage of the static declaration. It allows you to have the persistence of a global variable while retaining the scope limitations of a local variable.

Essentially it's the same situation for 3 and 4. The main difference is that in 3 the variable is only accessible in the function, but other than that it works in the same way as 4.

In examples 5 and 6, the var is not incremented in either case, as this "var" is passed by value to the function and therefore remains unaltered when the function returns.

PaulS:
What does "affect memory" mean to you? All the codes need to reserve space for the variable, for the life of the sketch. The only differences are the scope of the variable.

What is it that you are really trying to learn?

Thanks!!!

Why not compile them and see for yourself?

Sketch uses 462 bytes (1%) of program storage space. Maximum is 30,720 bytes.
Global variables use 11 bytes (0%) of dynamic memory, leaving 2,037 bytes for local variables. Maximum is 2,048 bytes.