Jump to content

Is 'Global' or 'Local' really required?


Recommended Posts

I am just beginning.

I read that variables are declared with either 'local' or 'global' scope.

Then I read that scope depends upon location when declared:  if declared in a function they are 'local' if they are declared outside all functions they are 'global'.

Does that mean we can simply declare them without any scope specified?

Link to comment
Share on other sites

58 minutes ago, abrogard said:

I am just beginning.

@Subz link is important. Why?. Well, structuring your code will save you trouble in the future when you get more creative as you know more and your code ends up too big for a single file. You will end up writing UDFs to simplify the maintenance of the code as you add more stuff to it. So, yes, do declare what you know will be global and what you know will be local. Do it for your mental health ;) 

Follow the link to my code contribution ( and other things too ).
FAQ - Please Read Before Posting.
autoit_scripter_blue_userbar.png

Link to comment
Share on other sites

As you probably understand about scopes at this point, you should also be aware now of how the scopes could be used to change how variables are accessible from other functions.  You can reassign/change the value a global (non-constant) variable within a function, not access other functions local scope variables, or define global variable within a function (though it is not best-practice).  

Variables declared outside of a function are inherently global, so I can understand why you might not care to distinguish which scope is defined. If you really don't want to declare variable scopes outside functions, that is up to you, but it's not a bad habit to get in to.  Having different rules for how you declare function variables versus variables outside of functions is unnecessary and may lead to confusion, IMHO.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...