Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.

  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint
Share this Page URL
Help

D. Variable Scope

Appendix D. Variable Scope

The rule of variable scope is defined simply: a variable created inside a block (code enclosed within braces: { and }) exists only inside that block. This means that a variable created inside setup() can be used only within the setup() block, and likewise, a variable declared inside draw() can be used only inside the draw() block. The exception to this rule is a variable declared outside of setup() and draw(). These variables can be used in both setup() and draw() (or inside any other function that you create). Think of the area outside of setup() and draw() as an implied code block. We call these variables global variables, because they can be used anywhere within the program. We call a variable that is used only within a single block a local variable. Following are a couple of code examples that further explain the concept. First:


  

You are currently reading a PREVIEW of this book.

                                                                                                                    

Get instant access to over $1 million worth of books and videos.

  

Start a Free 10-Day Trial


  
  • Safari Books Online
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint