Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Paasing global variables to functions

Reply
Thread Tools

Paasing global variables to functions

 
 
ankisharma@gmail.com
Guest
Posts: n/a
 
      07-28-2005
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?

 
Reply With Quote
 
 
 
 
Richard Heathfield
Guest
Posts: n/a
 
      07-28-2005
http://www.velocityreviews.com/forums/(E-Mail Removed) 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
 
Reply With Quote
 
 
 
 
Alipha
Guest
Posts: n/a
 
      07-28-2005
(E-Mail Removed) 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);

 
Reply With Quote
 
Emmanuel Delahaye
Guest
Posts: n/a
 
      07-28-2005
(E-Mail Removed) 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."


 
Reply With Quote
 
Emmanuel Delahaye
Guest
Posts: n/a
 
      07-28-2005
Alipha wrote on 28/07/05 :
> (E-Mail Removed) 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


 
Reply With Quote
 
Christian Bau
Guest
Posts: n/a
 
      07-28-2005
In article <(E-Mail Removed)>,
"Emmanuel Delahaye" <(E-Mail Removed)> wrote:

> (E-Mail Removed) 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...

 
Reply With Quote
 
Rouben Rostamian
Guest
Posts: n/a
 
      07-28-2005
In article <(E-Mail Removed) .com>,
<(E-Mail Removed)> 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
 
Reply With Quote
 
Walter Roberson
Guest
Posts: n/a
 
      07-28-2005
In article <(E-Mail Removed) .com>,
<(E-Mail Removed)> 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
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
 
Reply With Quote
 
gswork@mailcity.com
Guest
Posts: n/a
 
      07-29-2005

(E-Mail Removed) 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

 
Reply With Quote
 
Chris Dollin
Guest
Posts: n/a
 
      07-29-2005
(E-Mail Removed) 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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Paasing 70-562 before 70-536 antar MCTS 1 12-15-2009 04:41 PM
Paasing file name from Python to Fortran DLL avinrosh Python 0 10-10-2007 11:05 AM
Paasing values to javascript from asp.net Priya ASP .Net 5 02-19-2007 03:52 AM
FWSM/PIX and Dynamic PAT using global IP range vs. global interface vs. global IP Hoffa Cisco 1 10-25-2006 06:50 PM
FWSM/PIX and Dynamic PAT using global IP range vs. global interface vs. global IP Hoffa Cisco 0 10-25-2006 01:04 PM



Advertisments