Quantcast

Got Homework?

Connect with other students for help. It's a free community.

  • across
    MIT Grad Student
    Online now
  • laura*
    Helped 1,000 students
    Online now
  • Hero
    College Math Guru
    Online now

Here's the question you clicked on:

55 members online
  • 0 replying
  • 0 viewing

A.Avinash_Goutham

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

  • one year ago
  • one year ago

  • This Question is Closed
  1. A.Avinash_Goutham
    Best Response
    You've already chosen the best response.
    Medals 0

    @slotema @ajprincess

    • one year ago
  2. A.Avinash_Goutham
    Best Response
    You've already chosen the best response.
    Medals 0

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

    • one year ago
  3. moha_10
    Best Response
    You've already chosen the best response.
    Medals 0

    i don't get u

    • one year ago
  4. A.Avinash_Goutham
    Best Response
    You've already chosen the best response.
    Medals 0

    which one is better....

    • one year ago
  5. moha_10
    Best Response
    You've already chosen the best response.
    Medals 0

    ok i think passing as parameter is better

    • one year ago
  6. A.Avinash_Goutham
    Best Response
    You've already chosen the best response.
    Medals 0

    um u got a reason?

    • one year ago
  7. msmithhnova
    Best Response
    You've already chosen the best response.
    Medals 1

    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.

    • one year ago
  8. manishdablu
    Best Response
    You've already chosen the best response.
    Medals 0

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

    • one year ago
  9. KonradZuse
    Best Response
    You've already chosen the best response.
    Medals 0

    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.

    • one year ago
  10. msmithhnova
    Best Response
    You've already chosen the best response.
    Medals 1

    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.

    • one year ago
    • Attachments:

See more questions >>>

Your question is ready. Sign up for free to start getting answers.

spraguer (Moderator)
5 → View Detailed Profile

is replying to Can someone tell me what button the professor is hitting...

23

  • Teamwork 19 Teammate
  • Problem Solving 19 Hero
  • You have blocked this person.
  • ✔ You're a fan Checking fan status...

Thanks for being so helpful in mathematics. If you are getting quality help, make sure you spread the word about OpenStudy.

This is the testimonial you wrote.
You haven't written a testimonial for Owlfred.