Paasing global variables to functions

A

ankisharma

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?
 
R

Richard Heathfield

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.
 
A

Alipha

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);
 
E

Emmanuel Delahaye

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."
 
E

Emmanuel Delahaye

C

Christian Bau

"Emmanuel Delahaye said:
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?
 
R

Rouben Rostamian

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.
 
W

Walter Roberson

: 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.
 
G

gswork

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
 
C

Chris Dollin

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".]
 
W

William Hughes

Emmanuel said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top