Quantcast

A community for students. Sign up today!

Here's the question you clicked on:

55 members online
  • 0 replying
  • 0 viewing

A.Avinash_Goutham

  • one year ago

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

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

    @slotema @ajprincess

  2. A.Avinash_Goutham
    • one year ago
    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

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

    i don't get u

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

    which one is better....

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

    ok i think passing as parameter is better

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

    um u got a reason?

  7. msmithhnova
    • one year ago
    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.

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

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

  9. KonradZuse
    • one year ago
    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.

  10. msmithhnova
    • one year ago
    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.

  11. Not the answer you are looking for?
    Search for more explanations.

    Search OpenStudy
    • Attachments:

Ask your own question

Ask a Question
Find more explanations on OpenStudy

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.