Re: Paasing global variables to functions
- From: rouben@xxxxxxxxxxxxxxxxxx (Rouben Rostamian)
- Date: Thu, 28 Jul 2005 22:00:38 +0000 (UTC)
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?
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
.
- References:
- Paasing global variables to functions
- From: ankisharma
- Paasing global variables to functions
- Prev by Date: Re: Paasing global variables to functions
- Next by Date: Flexible size array
- Previous by thread: Re: Paasing global variables to functions
- Next by thread: Re: Paasing global variables to functions
- Index(es):
Relevant Pages
|