Need some code examples on how to affect a variable outside of a function without using globals. Also I want to know where should I use global and where not.
Thanks in advance.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
The only way to affect a variable in another scope without using globals is to use references. You can define a function that takes it’s arguments by reference by prefixing the argument with
&. For example:In answer to the question “when should I use globals” – never. They make the program flow confusing and invite errors and unexpected behaviour, and they also make code less portable, as it relies on variable names in another scope.