Fun programming with Processing, episode 50. Find out the difference between global and local variables. Global variables are declared outside any function, and they can be accessed (used) on any function in the program. Local variables are declared inside a function, and can be used only inside that function. It is possible to have local variables with the same name in different functions. Even the name is the same, they are not the same. It's like two people with the same name. Even the name is the same, the persons are not.
The scope of a variable refers to where is a variable visible or accesible. If a person asks what is the scope of a variable, she's asking whether it is local or global.
www.funprogramming.org
and how to make local variable to global ?
vitserp 3 months ago
@vitserp You can make a local variable global while writing the program, as far as I know you can't do that once the program has started. Making a local variable global is very easy, you just move the declaration of the variable from inside a function to the beginning of your program. myvar in this example is local: void setup() { int myvar = 3; } and here it's global: int myvar; void setup() { myvar = 3; }
hamoid 3 months ago
@vitserp Even you can't change the type of a variable once the program is running, you can copy the value of a local variable into a global variable and the other way around. For example: int myglobalvar = 3; void setup() { int mylocalvar = 77; myglobalvar = mylocalvar; } This way the variable myglobalvar will have a value of 77. You can access myglobalvar from inside any function in your program (setup(), draw(), mousePressed(), ...)
hamoid 3 months ago
congratulations for your 50th video, a great (and big) work!
gumli 5 months ago
@gumli Thank you! :)
hamoid 5 months ago