View previous topic :: View next topic |
Author |
Message |
planet69
Joined: 25 Aug 2010 Posts: 8
|
local & global variables |
Posted: Mon Nov 08, 2010 9:27 pm |
|
|
Why do we need to have separate local & global variables.
I've completed several programs without ever declaring any local variables and everything as globals.
I avoid any 'conflict' that may come my way with globals.
What is the advantage of using local variables anyway other than that they may be re-used by the compiler to save limited RAM space.
Just a thought.
Thanks. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Mon Nov 08, 2010 10:00 pm |
|
|
For small programs it's not so bad. When you start to do projects that
contain many modules (files), there are too many variables to keep
track of mentally, and you will see the need for using mostly local
variables. See this page for more reasons:
http://c2.com/cgi/wiki?GlobalVariablesAreBad |
|
|
pmuldoon
Joined: 26 Sep 2003 Posts: 218 Location: Northern Indiana
|
|
Posted: Tue Nov 09, 2010 7:47 am |
|
|
In very small programs it doesn't matter much. But I prefer to use locals and static locals whenever possible because I know the scope of them (only used within that function) and they are easier to keep track of.
And they can also be given very specific and descriptive names.
Globals can create problems when you start to lose track of which functions are using them / changing them, and what their purpose is.
I would suggest being as descriptive as possible with your variable names, and limit the scope of them whenever possible. When you come back to your program 1 or 2 years from now to add a new feature, it will be that much easier to see that you're not creating some unintended side effect with the change.
Just my thoughts... |
|
|
Gabriel
Joined: 03 Aug 2009 Posts: 1067 Location: Panama
|
|
Posted: Tue Nov 09, 2010 8:01 am |
|
|
I write everything as small drivers, each driver file contains its own global variables. I keep track what they are for that way. If they are on the driver file, they only affect the driver functions....basically small groups of globals...works for me... _________________ CCS PCM 5.078 & CCS PCH 5.093 |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19504
|
|
Posted: Tue Nov 09, 2010 9:54 am |
|
|
The other 'key' thing is memory usage.
If you have a subroutine, that uses 20 bytes for a character array, and some other variables. If these are 'global', this memory is being used by the variables all the time. If you declare the variables as local, the same memory area, can be re-used by other routines.
Only using global, suggests 'small code'. With larger code, and more memory in use, the ability to re-use parts of the memory becomes essential....
Best Wsihes |
|
|
planet69
Joined: 25 Aug 2010 Posts: 8
|
|
Posted: Tue Nov 09, 2010 9:58 pm |
|
|
Appreciate the feedback. enlightening.
Anyway, I've done some pretty large programs but always with some RAM to spare.
I guess my 'large programs' are not that 'large' after all.
Thank you. |
|
|
pmuldoon
Joined: 26 Sep 2003 Posts: 218 Location: Northern Indiana
|
|
Posted: Wed Nov 10, 2010 6:10 am |
|
|
If it works for you, then it works. Chances are you are like me and nobody else will probably ever read your programs.
But keep in mind, as your skills grow, so will the complexity of your programs. |
|
|
|