An Ethics Question (global variables)

Emuleomo emuleomo at surfers.rutgers.edu
Tue Apr 18 02:15:56 AEST 1989


>
> Global variables versus parameter passing
>
What I HATE about global variables is that the first time you look at
a function that uses it, the functions seems to work mysteriously.
Futhermore, you had better be damned sure that you dont 'tamper' with those
variables since they could also be used somewhere else (in another function).

The RULE OF THUMB is...
PASS PARAMETERS whenever you can. Believe me. It will save you countless
hours of debugging time.  Besides they make your functions transportable.
However, I must admit that if you find yourself passing say 10 parameters
to a function and maybe half of them are the addresses of variables 
into which you want to assign values, then you may consider using 
global variables.  Howvever, try to keep all the routines that need to 
communicate with each other via global variables in the same file and declare 
the variables as STATIC. 
Another helpful trick is make the Fisrt letter of your Global Variable
UPPERCASE and the rest lowercase!. This way, you can easily identify
which are the global variables in your code!

--Emuleomo O.O.
** The only global variable I use is a database! **
lowercase letters EXCEPT the f
you want their values
My first reaction in coding a function/subroutine is to pass parameters
to it and let it operate on these parameters.
If you are working on a large and complex so



More information about the Comp.lang.c mailing list