Re: Paasing global variables to functions



In article <1122532047.716912.229400@xxxxxxxxxxxxxxxxxxxxxxxxxxxx>,
<ankisharma@xxxxxxxxx> wrote:
: At many places I have seen that programmers pass global variables
:to functions in c. I am not able to figure out why they do so. need
:some clues on this. somewhere i heard that this philosophy is from
:object orieted world but is it applicable for c?

Abstraction and Encapsulation.

Suppose I create a global variable in a routine and I use it
-as- a global variable in lower-level routines.

Suppose now that I change my mind about the implementation detail of
whether it should be a global variable or a file-scoped variable or
some malloc()'d storage or an automatic variable. Suddenly I need
to hunt down and change -all- the global references to
the variable.

If, on the other hand, I create a global variable, and as far
as is practical, I pass it explicitly to routines, then when I change
my mind about the storage paradigm, I do not have to change those
lower-level routines.

Even in cases where it is not really practical to pass the variable
through many levels, it is not uncommon to write accessor functions
that get or set the value of the variable, rather than just going
ahead and reading or writing directly to the global.

Creating accessor functions tends to divorce the implementation details
from semantic details. The accessor functions might make sanity
checks, or might hide details such as whether a value is stored
as an inddpendant variable or as a bitfield. An accessor function
might invoke associated behaviour (e.g., flushing a buffer), or
might synthesize the variable by way of others. If you imagine a
data structure representing triangles, then an accessor function
that allowed setting the coordinates of one of the vertices might
take on the ancillary job of recalculating angles -- or in an accessor
function for a data structure representing a square, setting the
coordinates of one of the vertices might result in the recalculation
of the location of two of the other vertices.

One -could-, in each of the above cases, have directly bashed the
appropriate global variables (and then called any necessary
follow-on routines), but if you think about the situation, you will
realize that just because a variable -happens- to be implemented as
a global doesn't mean that the variable is functionally independant
of all other variables. Sbstracting away the representation issues
can sometimes result in much better code that is more maintainable
and more extensible.
--
"Never install telephone wiring during a lightning storm." -- Linksys
.



Relevant Pages

  • Re: Developing/compiling software
    ... But when you have a clearly defined data item that can be safely read or written, then wrapping the data in simple accessor functions is a waste of source code, a waste of object code, and a waste of runtime, and gives exactly zero benefits for modularisation, encapsulation, structure, clarity, code maintenance, or any other buzz words you like. ... potential for name conflicts than "SetUartTimeout" and "GetUartTimeout". ... The code is small enough that is not a big problem to keep track of modules and their interfaces, and that includes global variables. ... the waste of runtime and code space caused by unnecessary accessor functions /can/ be significant. ...
    (comp.arch.embedded)
  • Re: global variables coding preference
    ... >> static variables and accessor functions. ... The simplest approach is ... > program wouldn't even need to know about the struct. ... assertion "you have to use global variables in some programs" was ...
    (comp.lang.c)
  • Re: Does anyone in Delphi land NOT use OOP
    ... It is not global routines that I don't like, it is global variables, ... When the routine needs to know something other than the ...
    (borland.public.delphi.non-technical)
  • Re: Calculator
    ... > Which, if true,would be an implementation detail of libc that ... The interface is all in terms of FILE ... > The basic advice "Normally you should not have global variables" is ...
    (comp.lang.c)
  • Re: Ive Had Enough
    ... Meaning that one can create global variables and routines (procedures or ... void Foo() // global, ... > think that there is a perfect .NET language out there. ...
    (microsoft.public.dotnet.languages.csharp)