Paasing global variables to functions

Discussion in 'C Programming' started by ankisharma@gmail.com, Jul 28, 2005.

  1. Guest

    Hi all

    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?
     
    , Jul 28, 2005
    #1
    1. Advertising

  2. wrote:

    > Hi all
    >
    > 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.


    Never underestimate the sheer overwhelming power of mindless stupidity.

    > need
    > some clues on this. somewhere i heard that this philosophy is from
    > object orieted world but is it applicable for c?


    Think global. Act local.

    --
    Richard Heathfield
    "Usenet is a strange place" - dmr 29/7/1999
    http://www.cpax.org.uk
    mail: rjh at above domain
     
    Richard Heathfield, Jul 28, 2005
    #2
    1. Advertising

  3. Alipha Guest

    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?


    for instance, the function is called multiple times with different
    arguments than that specific global variable. eg:

    f(myglobal);
    f(mylocal);
    f(myglobal2);
     
    Alipha, Jul 28, 2005
    #3
  4. wrote on 28/07/05 :
    > At many places I have seen that programmers pass global variables
    > to functions in c.


    Sounds weird. If a variable has a global scope, it's inconsistent to
    pass it to a function (via a parameter, I guess)...

    That said, better to avoid global scope variables at all...

    --
    Emmanuel
    The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
    The C-library: http://www.dinkumware.com/refxc.html

    "There are 10 types of people in the world today;
    those that understand binary, and those that dont."
     
    Emmanuel Delahaye, Jul 28, 2005
    #4
  5. Alipha wrote on 28/07/05 :
    > 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?

    >
    > for instance, the function is called multiple times with different
    > arguments than that specific global variable. eg:
    >
    > f(myglobal);
    > f(mylocal);
    > f(myglobal2);


    Huh! Yes!

    --
    Emmanuel
    The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
    The C-library: http://www.dinkumware.com/refxc.html

    "It's specified. But anyone who writes code like that should be
    transmogrified into earthworms and fed to ducks." -- Chris Dollin CLC
     
    Emmanuel Delahaye, Jul 28, 2005
    #5
  6. In article <>,
    "Emmanuel Delahaye" <> wrote:

    > wrote on 28/07/05 :
    > > At many places I have seen that programmers pass global variables
    > > to functions in c.

    >
    > Sounds weird. If a variable has a global scope, it's inconsistent to
    > pass it to a function (via a parameter, I guess)...


    So you would you print the contents of a global variable using printf
    (), for example?

    > That said, better to avoid global scope variables at all...
     
    Christian Bau, Jul 28, 2005
    #6
  7. In article <>,
    <> 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?


    I don't know about "many places", but a scenario like this is
    plausible:

    --- version 1 ---------------------------------
    int x; /* global variable */

    void foo(int n)
    {
    /* do something with n */
    }

    int main(void)
    {
    x = 4;
    foo(x);
    ...
    }

    --- version 2 ---------------------------------
    int x; /* global variable */

    void foo(void)
    {
    /* do something with x */
    }

    int main(void)
    {
    x = 4;
    foo();
    ...
    }

    If the function foo() does not change the value of the
    global variable, then the two versions are equivalent, but
    prefer version 1 because the function foo() is self-contained
    therefore easier to maintain. And if some day you redo your
    program so that the global variable goes away, the function
    foo() won't be affected.

    --
    Rouben Rostamian
     
    Rouben Rostamian, Jul 28, 2005
    #7
  8. In article <>,
    <> 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
    :eek:bject 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
     
    Walter Roberson, Jul 29, 2005
    #8
  9. Guest

    wrote:
    > Hi all
    >
    > 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?


    Perhaps they are doing so out of habit - but maybe it is in an
    unfinished project and they are planning not to have the variable be
    global at some point. Conversely the function may be ported elsewhere
    to a source in which the passed variable is not global. May as well
    get these things right first time though, wherever practicable
     
    , Jul 29, 2005
    #9
  10. Chris Dollin Guest

    wrote:

    > Hi all
    >
    > 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.


    To make the values of those variables available to those functions,
    which might be written without knowledge of the existence of those
    globals.

    Global variables introduce coupling; coupling makes systems harder
    to change (or, if you prefer, coupling is the name we give to one
    of the ways that systems are hard to change); so one may reduce
    one's use of globals to improve the design toward flexibility.

    [There Are No Universal Answers Except For "It Depends".]

    --
    Chris "electric hedgehog" Dollin
    predicting self-predictors' predictions is predictably unpredictable.
     
    Chris Dollin, Jul 29, 2005
    #10
  11. Emmanuel Delahaye wrote:
    > wrote on 28/07/05 :
    > > At many places I have seen that programmers pass global variables
    > > to functions in c.

    >
    > Sounds weird. If a variable has a global scope, it's inconsistent to
    > pass it to a function (via a parameter, I guess)...


    Only if you have control of the function to which you are passing
    the globals.

    If you have to use a function with a predefined interface
    (e.g. a function minimizer, or a library function like printf)
    the fact that the globals are in fact in scope for these functions
    does not help you.

    Suppose that you have a function funcmin(f) which takes a pointer
    to a function of a double returning a double. Assume you need to get
    the minumimum of g(x,y,z) ( x,y,z all doubles) with respect to x.

    set up globals g_y and g_z and define

    double f(double x)
    {
    return g(x,g_y,g_z);
    }

    Now you can get the needed minimum by setting g_y and g_z and calling
    funcmin(f). (You can make this safer by setting up static parameters
    in a module which can only be accessed through function calls,
    but the dependence of f on something other than the arguments to f
    is intrinsic to the problem).


    >
    > That said, better to avoid global scope variables at all...
    >


    Similar to the advice to avoid goto. Globals should be used
    infrequently, but they have their place. However, a blanket ban
    on globals does not cost you much and does of course prevent the
    misuse of globals.

    - William Hughes
     
    William Hughes, Jul 29, 2005
    #11
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Wayne
    Replies:
    2
    Views:
    493
    Wayne
    Nov 11, 2003
  2. Priya
    Replies:
    5
    Views:
    453
    Priya
    Feb 19, 2007
  3. jubelbrus
    Replies:
    5
    Views:
    634
    JohnQ
    Jul 20, 2007
  4. avinrosh
    Replies:
    0
    Views:
    408
    avinrosh
    Oct 10, 2007
  5. User1014
    Replies:
    3
    Views:
    202
    Richard Cornford
    Dec 1, 2006
Loading...

Share This Page