Why we should never declare global variables | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Why we should never declare global variables

27th Jun 2017, 6:02 AM
ANAM NAUSHAD
ANAM NAUSHAD - avatar
3 Answers
+ 11
'Never' is an exaggeration, although global variables do have their cons. One of the most direct reasons is because a global variable is subjected to change everywhere - Functions, class methods, main(). This makes it hard to trace, and your program equally hard to debug when errors occur.
27th Jun 2017, 6:06 AM
Hatsy Rei
Hatsy Rei - avatar
+ 4
Depends on need. If you need a global variable, go for it. Usually, the story begins from main() and ends in main(), that's why people use local variable.
27th Jun 2017, 6:07 AM
Sachin Artani
Sachin Artani - avatar
+ 4
Global variables are typically used in game programming because they are much faster to read/modify.
27th Jun 2017, 8:44 AM
Karl T.
Karl T. - avatar