-2

When would you use a global variable instead of an argument, vis versa. Also, why would you do this?

99.9% of the time, I use global variables then return a product of those variables. I rarely use Arguments in my functions, but I want to know when the right time to use either one of these is. Am I doing this wrong, or does it really matter?

Piguinrulist
  • 95
  • 1
  • 7
  • 1
    99.99999% of the time you want to avoid global variables, code written like that is the hardest to debug, and maintain. Function parameters exist for a good reason, use them! – Óscar López Apr 02 '16 at 18:45
  • 1
    99.9% of people are going to tell you you're in the wrong. If you can get by programming the way you are, then you're likely on a very small scale. As soon as you start creating more complex programs you'll notice how awful your practice is. Regardless, this question is primarily opinion based, and not suitable for SO. – Oka Apr 02 '16 at 18:48
  • Possible duplicate of [Why are global variables evil?](http://stackoverflow.com/questions/19158339/why-are-global-variables-evil) – skrrgwasme Apr 02 '16 at 18:53
  • 1
    When would you use a global variable? Never. When would you use an argument? Always. – zondo Apr 02 '16 at 18:58

1 Answers1

0

99.9999% of the programmers use arguments, and avoid at all costs global variables.

In my opinion that is because functions arguments define very well what input does the function require, and returning values defines what the function's result is. Using global variables, it is very difficult to know at a glimpse what data the function uses and what data it returns.

Amit Gold
  • 727
  • 7
  • 22