I know global variables are created when they are declared outside a function (says W3Schools).
If I create a global variable and edit it in a function, does it become local? Does the new value given by the function become the global value?
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.
In general, no, editing a global does not make it local:
However, if you pass the global variable as an argument, the argument is a local copy:
Note that objects are passed by reference, so editing the member variables of an argument variable changes the member variables of the original object passed in:
Finally, note that the local variable in
editLoc, above, is just a reference. If we try to overwrite the entire object (instead of a member variable), the function simply loses the reference to the original object: