Learn programming 50: What are global and local variables?

Loading...

Sign in or sign up now!
Alert icon
Upgrade to the latest Flash Player for improved playback performance. Upgrade now or more info.
242 views
Loading...
Alert icon
Sign in or sign up now!
Alert icon

Uploaded by on Sep 26, 2011

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

  • likes, 0 dislikes

Link to this comment:

Share to:

Uploader Comments (hamoid)

  • and how to make local variable to global ?

  • @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; }

  • @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(), ...)

  • congratulations for your 50th video, a great (and big) work!

  • @gumli Thank you! :)

see all

All Comments (6)

Sign In or Sign Up now to post a comment!
  • @hamoid thanks good explanation :)

Loading...

Alert icon
0 / 00Unsaved Playlist Return to active list
    1. Your queue is empty. Add videos to your queue using this button:
      or sign in to load a different list.
    Loading...Loading...Saving...
    • Clear all videos from this list
    • Learn more