According to the most programming languages scope rules, I can access variables that are defined outside of functions inside them, but why doesn\'t this code work?
You can do one of the following:
Or
That being said, overuse of globals can lead to some poor code. It is usually better to pass in what you need. For example, instead of referencing a global database object you should pass in a handle to the database and act upon that. This is called dependency injection. It makes your life a lot easier when you implement automated testing (which you should).