choosing between making a variable global or passing as a parameter to a function...
Stacey Warren - Expert brainly.com
Hey! We 've verified this expert answer for you, click below to unlock the details :)
At vero eos et accusamus et iusto odio dignissimos ducimus qui blanditiis praesentium voluptatum deleniti atque corrupti quos dolores et quas molestias excepturi sint occaecati cupiditate non provident, similique sunt in culpa qui officia deserunt mollitia animi, id est laborum et dolorum fuga.
Et harum quidem rerum facilis est et expedita distinctio. Nam libero tempore, cum soluta nobis est eligendi optio cumque nihil impedit quo minus id quod maxime placeat facere possimus, omnis voluptas assumenda est, omnis dolor repellendus.
Itaque earum rerum hic tenetur a sapiente delectus, ut aut reiciendis voluptatibus maiores alias consequatur aut perferendis doloribus asperiores repellat.
I got my questions answered at brainly.com in under 10 minutes. Go to brainly.com now for free help!
cosider the case of calling the same function multiple times.... when the same param is passed again and again
Not the answer you are looking for? Search for more explanations.
which one is better....
ok i think passing as parameter is better
um u got a reason?
Generally, global variables should be avoided if possible because it can be inadvertently changed somewhere causing crashes or security issues. Some use is acceptable if they are needed in many places and should be named in a way that they do not get inadvertently used elsewhere. Passing a parameter is usually a much better idea.
I think it will depend on the language you are using ..
First off it depends on the language used.
Next I don't know why you wouldn't use global variables, because that's a bad idea @msmithhnova
Globals are used when you need to store data and be able to use it at some point int he future. If you pass the parameter in a method or function you only use it in that instance, whereas you might need it later on.,
you could, of course, create something like.
public void method()
i = 10;
r = new Rectangle(10,10,10,10);
so we have 2 cases.
1 you could have set i = 10 at the top, and used that 10 later on, or initialized rectangle at first.
Then we have what I posted above. using some method to define certain instances and such.
this will all be saved globally to be used in another method.
But if you did
public void method()
int i = 10;
Rectangle r = new Rectangle(10,10,10,10);
They would only be good in your method, and you would have to make it = something globally.
First, I didn't say never use global variables, but in most cases global variables can and should be avoided. They do have their uses and can be very useful, but their use should be limited and only when appropriate to do so. In your example above, if you set a global variable 'i' in a project that has hundreds of thousands of lines in it and dozens of people working on it and then someone somewhere uses it as a counter in some local code, good luck finding the bug. Most people consider them a good thing to avoid if reasonable to do so. And some links since there is no point in rehashing things that have been said many times in many places ...
Granted not everyone will agree but this is the general consensus and what I have heard from professors many times. Some people say globals are bad but that is not true, they are only bad if they are misused, overused etc.