Ask your own question, for FREE!
Computer Science 14 Online
OpenStudy (anonymous):

choosing between making a variable global or passing as a parameter to a function...

OpenStudy (anonymous):

@slotema @ajprincess

OpenStudy (anonymous):

cosider the case of calling the same function multiple times.... when the same param is passed again and again

OpenStudy (anonymous):

i don't get u

OpenStudy (anonymous):

which one is better....

OpenStudy (anonymous):

ok i think passing as parameter is better

OpenStudy (anonymous):

um u got a reason?

OpenStudy (anonymous):

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.

OpenStudy (anonymous):

I think it will depend on the language you are using ..

OpenStudy (konradzuse):

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. int i; Rectangle r; 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.

OpenStudy (anonymous):

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 ... http://c2.com/cgi/wiki?GlobalVariablesAreBad http://stackoverflow.com/questions/484635/are-global-variables-bad 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.

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!