473,782 Members | 2,494 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Paasing global variables to functions

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?

Nov 15 '05 #1
10 1972
an********@gmai l.com 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
Nov 15 '05 #2
an********@gmai l.com 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);

Nov 15 '05 #3
an********@gmai l.com 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."
Nov 15 '05 #4
Alipha wrote on 28/07/05 :
an********@gmai l.com 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
Nov 15 '05 #5
In article <mn************ ***********@YOU RBRAnoos.fr>,
"Emmanuel Delahaye" <em***@YOURBRAn oos.fr> wrote:
an********@gmai l.com 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...

Nov 15 '05 #6
In article <11************ **********@o13g 2000cwo.googleg roups.com>,
<an********@gma il.com> 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
Nov 15 '05 #7
In article <11************ **********@o13g 2000cwo.googleg roups.com>,
<an********@gma il.com> 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
Nov 15 '05 #8

an********@gmai l.com 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

Nov 15 '05 #9
an********@gmai l.com 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.
Nov 15 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
17882
by: Matt | last post by:
Greetings, What are people's thoughts on global variables in C++? Why are we taught not to use them in programming? Is it true that if you are running two copies of the C program one copy can overwrite another copies global variable? I know that you could overwrite a value in a global variable from a function, but you could also do that if you pass the variable in and then out again... so how is that any different?
2
9170
by: Patient Guy | last post by:
I have a library of functions representing a filesystem interface (essentially a file selection interface, to be used in opening/reading/writing/closing files). Heavily scripted HTML document #1, very application-like, must include the JS file for this library of functions. One reason is that it must call a function to name a "callback" function, and within its own script block, must define the callback function. The callback handles...
33
3051
by: MLH | last post by:
I've read some posts indicating that having tons of GV's in an Access app is a bad idea. Personally, I love GVs and I use them (possibly abuse them) all the time for everything imaginable - have been for years. If the machine has memory to spare and windows can use it - I'm thinking "Why not?" I was wondering what some of you have to say about that, particularly any severe "gotchas" you've had the unfortunate experience to contend with.
11
2567
by: Capstar | last post by:
Hi, I am working on an application, which will run embedded without an OS. The app is build up out of a couple of well defined parts. At first I wanted to keep those parts seperated and use opaque data types to transfer information in between them. At some stage I was stuck and needed to make a variable global, and I also needed to make the struct declaration public to some other parts. Looking through the code I found out that lots...
7
3145
by: Michael | last post by:
Hi newsgroup, as the subject indicates I am looking for an advice using global variables. I am not if this problem is more about style then C. If its wrong in thi group, sorry. So I have a couple of function that all need the same information (all located in the same file). By now it looks like /* file beginns */
10
2662
by: Charles O'Flynn | last post by:
As a complete newcomer (2-3 days) to PHP, although not to programming in general, I have 'dived in' to start a small project to read and parse an XML data stream. I have already worked out most of the more specialist aspects of the job but am now completely stuck on something I would have thought were simplicity itself... I need to have a large number of global variables visible inside functions - it's not possible to pass them into the...
9
8659
by: CDMAPoster | last post by:
About a year ago there was a thread about the use of global variables in A97: http://groups.google.com/group/comp.databases.ms-access/browse_frm/thread/fedc837a5aeb6157 Best Practices by Kang Su Gatlin, casual mention was made about using static variables as an alternative to using global variables. This caused me to think of the following: '-----Begin module code
5
20395
by: Priya | last post by:
Hey all, I have some values in the database which has to retrieved and then passed on to some functions in the javascript. The javascript file is a separate file. I have included it in the aspx page using the src option. How do I pass the values that I get from the database to javascript ? Any help would be appreciated.
1
29382
weaknessforcats
by: weaknessforcats | last post by:
C++: The Case Against Global Variables Summary This article explores the negative ramifications of using global variables. The use of global variables is such a problem that C++ architects have called it polluting the global namespace. This article explores what happens when the global namespace becomes polluted and how to avoid this condition. The opinions expressed in this article are those of the author alone although many have...
0
9639
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10311
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10146
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9942
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8967
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7492
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5509
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4043
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3639
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.