Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Function and variable declarations

Reply
Thread Tools

Function and variable declarations

 
 
Joseph Wakeling
Guest
Posts: n/a
 
      11-26-2005
Hello all,

Here's a brief function from the aforementioned (and controversial) GNU
Scientific Library (GSL), or rather, a slightly rewritten version of it
that I've made. The function takes an integer and sets up certain
necessary values for a random number generator. x, n and shuffle in
this function are global variables.

/************************************************** ***********/
static void
ran1_set (unsigned long int s) {
int i;

if(s==0)
s = 1;

for(i=0;i<8;++i) {
long int h = s / q;
long int t = a * (s - h * q) - h * r;
if(t < 0)
t += m;
s = t;
}

for(i=N_SHUFFLE-1;i>=0;--i) {
long int h = s/q;
long int t = a * (s - h * q) - h * r;
if(t < 0)
t += m;
s = t;
shuffle[i] = s;
}

x = s;
n = s;

return;
}
/************************************************** ***********/

Anyway, two questions:

(1) why declare the function a "static void" instead of just void? (My
personal use of "static" is just to preserve values between different
calls to modules or functions, but I know its use can be more complex
than this.)

(2) Any particular reason why the long int's h and t are declared twice
inside different loops, instead of just being declared at the beginning
of the function?

Many thanks,

-- Joe

 
Reply With Quote
 
 
 
 
rayw
Guest
Posts: n/a
 
      11-26-2005

"Joseph Wakeling" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) ups.com...
> Hello all,
>
> Here's a brief function from the aforementioned (and controversial) GNU
> Scientific Library (GSL), or rather, a slightly rewritten version of it
> that I've made. The function takes an integer and sets up certain
> necessary values for a random number generator. x, n and shuffle in
> this function are global variables.
>
> /************************************************** ***********/
> static void
> ran1_set (unsigned long int s) {
> int i;
>
> if(s==0)
> s = 1;
>
> for(i=0;i<8;++i) {
> long int h = s / q;
> long int t = a * (s - h * q) - h * r;
> if(t < 0)
> t += m;
> s = t;
> }
>
> for(i=N_SHUFFLE-1;i>=0;--i) {
> long int h = s/q;
> long int t = a * (s - h * q) - h * r;
> if(t < 0)
> t += m;
> s = t;
> shuffle[i] = s;
> }
>
> x = s;
> n = s;
>
> return;
> }
> /************************************************** ***********/
>
> Anyway, two questions:
>
> (1) why declare the function a "static void" instead of just void? (My
> personal use of "static" is just to preserve values between different
> calls to modules or functions, but I know its use can be more complex
> than this.)
>
> (2) Any particular reason why the long int's h and t are declared twice
> inside different loops, instead of just being declared at the beginning
> of the function?


1. The use of static for a function restricts the function's visibility - to
that of the translation unit (file) it's defined in.

2. As far as I know - Nope.


 
Reply With Quote
 
 
 
 
Flash Gordon
Guest
Posts: n/a
 
      11-26-2005
Joseph Wakeling wrote:
> Hello all,
>
> Here's a brief function from the aforementioned (and controversial) GNU
> Scientific Library (GSL), or rather, a slightly rewritten version of it
> that I've made. The function takes an integer and sets up certain
> necessary values for a random number generator. x, n and shuffle in
> this function are global variables.
>
> /************************************************** ***********/
> static void
> ran1_set (unsigned long int s) {
> int i;
>
> if(s==0)
> s = 1;
>
> for(i=0;i<8;++i) {
> long int h = s / q;
> long int t = a * (s - h * q) - h * r;
> if(t < 0)
> t += m;
> s = t;
> }
>
> for(i=N_SHUFFLE-1;i>=0;--i) {
> long int h = s/q;
> long int t = a * (s - h * q) - h * r;
> if(t < 0)
> t += m;
> s = t;
> shuffle[i] = s;
> }
>
> x = s;
> n = s;


Global (or at least file scope) variable named x and n? Yuk.

> return;


Rather pointless having a return here IMHO.

> }
> /************************************************** ***********/
>
> Anyway, two questions:
>
> (1) why declare the function a "static void" instead of just void? (My
> personal use of "static" is just to preserve values between different
> calls to modules or functions, but I know its use can be more complex
> than this.)


When used on a function or a variable at file scope it means
approximately "don't make the name available to other modules." Or, to
use standard terminology, it only has internal linkage.

> (2) Any particular reason why the long int's h and t are declared twice
> inside different loops, instead of just being declared at the beginning
> of the function?


It's called minimising scope. Because they are declared in the loop you
know, without having to check, that they are not used outside.
Otherwise, you would have to read beyond the end of the loop to see if
the last value gets used outside the loop.
--
Flash Gordon
Living in interesting times.
Although my email address says spam, it is real and I read it.
 
Reply With Quote
 
Skarmander
Guest
Posts: n/a
 
      11-26-2005
Joseph Wakeling wrote:
> static void
> ran1_set (unsigned long int s) {

<snip>
> for(i=0;i<8;++i) {
> long int h = s / q;
> long int t = a * (s - h * q) - h * r;
> if(t < 0)
> t += m;
> s = t;
> }
>
> for(i=N_SHUFFLE-1;i>=0;--i) {
> long int h = s/q;
> long int t = a * (s - h * q) - h * r;
> if(t < 0)
> t += m;
> s = t;
> shuffle[i] = s;
> }

<snip>
> (1) why declare the function a "static void" instead of just void? (My
> personal use of "static" is just to preserve values between different
> calls to modules or functions, but I know its use can be more complex
> than this.)
>

"static" in declarations has two different meanings; one is to give an
identifier static storage (the use you describe), the other is to give
it internal linkage (this use). Here "static" just means "not extern",
that is, the function symbol is internal to the unit and inaccessible
from the outside. This improves modularity.

There's actually a third meaning in C99, used in passing array
parameters, but if you're interested in this you can look it up. You're
not likely to encounter it in the wild yet. And we'll ignore C++, which
assigns yet another meaning to it.

> (2) Any particular reason why the long int's h and t are declared twice
> inside different loops, instead of just being declared at the beginning
> of the function?
>

Declaring variables in the innermost block they are used in is good for
readability and may have positive effects on register usage too. In this
case it doesn't buy you much, but it's not hurting anyone either.

S.
 
Reply With Quote
 
rayw
Guest
Posts: n/a
 
      11-26-2005

"Skarmander" <(E-Mail Removed)> wrote in message
news:43886e33$0$24373$(E-Mail Removed)4all.nl...
<snip>

>
> There's actually a third meaning in C99, used in passing array parameters,
> but if you're interested in this you can look it up. You're not likely to
> encounter it in the wild yet. And we'll ignore C++, which assigns yet
> another meaning to it.
>


void f(double a[static 3]);

Specifies that the argument corresponding to 'a' in any call to f must be a
non-null pointer to the first element????


 
Reply With Quote
 
Michael Mair
Guest
Posts: n/a
 
      11-26-2005
rayw wrote:
> "Skarmander" <(E-Mail Removed)> wrote in message
> news:43886e33$0$24373$(E-Mail Removed)4all.nl...
> <snip>
>
>>There's actually a third meaning in C99, used in passing array parameters,
>>but if you're interested in this you can look it up. You're not likely to
>>encounter it in the wild yet. And we'll ignore C++, which assigns yet
>>another meaning to it.

>
> void f(double a[static 3]);
>
> Specifies that the argument corresponding to 'a' in any call to f must be a
> non-null pointer to the first element????


Among other things also this.
The above tells you that the array a points to has at least
three elements, i.e. that you can safely access a[0], a[1], a[2].
This can give rise to optimisations and (conversely) the
compiler may give you a diagnostic when you pass a pointer
to an array which is either NULL or does not point to an
array with sufficiently many elements.


Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
 
Reply With Quote
 
jacob navia
Guest
Posts: n/a
 
      11-26-2005
Flash Gordon a écrit :
> Global (or at least file scope) variable named x and n? Yuk.
>


I would like to emphasize that.

It is ALWAYS an error to name a GLOBAL variable (i.e. at file or even
process scope) with a name like "n" or "x".

This is bound to provoke name clashes. Who hasn't a local variable
named "x" in a math package somewhere???

Now suppose you mistype the declaration of your x, and istead of writing
int x;
you type
int xc;

The compiler SILENTLY will use the global variable x, that you will
destroy!!!

Today, names can be quite long in C, some compilers accept 256 or even
more characters in a name.

There is a measure in everything, and extremely long names could
be a bore to type, but a global variable should always have a
name that conveys something about its usage, and avoids name clashes.

This is even more necessary in the case of a library like in this example.

If you are writing a LIBRARY, you should do your best to avoid
polluting your user's name space, prefixing all your globals
(if you need globals) with the prefix of your library. Note that
the gsl library always uses gsl_something for its names. I would
ve very surprised if they would leave a variable like 'x'
floatng around... Nobody could use such a library.

jacob
 
Reply With Quote
 
Malcolm
Guest
Posts: n/a
 
      11-26-2005

"Skarmander" <(E-Mail Removed)> wrote
> Declaring variables in the innermost block they are used in is good for
> readability and may have positive effects on register usage too. In this
> case it doesn't buy you much, but it's not hurting anyone either.
>

I believe in the rule of three.

A human being can cope with three layers of nested parentheses, three levels
of indirection, three dimensions, and three levels of scope.
In C these are global, file scope, and function scope. By adding more levels
you render your program non-human understandable.


 
Reply With Quote
 
Flash Gordon
Guest
Posts: n/a
 
      11-26-2005
Malcolm wrote:
> "Skarmander" <(E-Mail Removed)> wrote
>> Declaring variables in the innermost block they are used in is good for
>> readability and may have positive effects on register usage too. In this
>> case it doesn't buy you much, but it's not hurting anyone either.
>>

> I believe in the rule of three.
>
> A human being can cope with three layers of nested parentheses, three levels
> of indirection, three dimensions, and three levels of scope.
> In C these are global, file scope, and function scope. By adding more levels
> you render your program non-human understandable.


I don't have a problem reading other peoples code that uses block scope
variables, but perhaps I am non-human. Sometimes they make it easier to
understand, because you don't have to worry about whether they are used
outside that scope. Having said that, I won't have multiple blocks using
the same name for separate variables, I would prefer either distinct
names or to define than at function scope.
--
Flash Gordon
Living in interesting times.
Although my email address says spam, it is real and I read it.
 
Reply With Quote
 
Skarmander
Guest
Posts: n/a
 
      11-27-2005
Malcolm wrote:
> "Skarmander" <(E-Mail Removed)> wrote
>
>>Declaring variables in the innermost block they are used in is good for
>>readability and may have positive effects on register usage too. In this
>>case it doesn't buy you much, but it's not hurting anyone either.
>>

>
> I believe in the rule of three.
>
> A human being can cope with three layers of nested parentheses, three levels
> of indirection, three dimensions, and three levels of scope.
> In C these are global, file scope, and function scope. By adding more levels
> you render your program non-human understandable.
>
>

I believe in the rule of seven.

A human being can cope with at most seven items held in active memory
before you have to start referring back to refresh your memory. If, by
putting your variables in subblocks, you can reduce the number of
outstanding variables that have to be kept in active memory at any given
point, you've achieved something.

That said, I've found that very often when people start declaring
variables in subblocks (especially when it's more than one variable)
it's usually time to split the block off to a new function, rather than
trying to keep it all squeezed into one.

S.
 
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
Variable argument function as a parameter of a variable argument function AikidoGuy C Programming 11 11-21-2011 10:43 PM
Variable declarations TCB ASP .Net 3 03-21-2006 07:18 PM
member variable declaration and forward declarations John Ratliff C++ 2 08-26-2005 07:50 PM
Visual Studio forgets about variable declarations Nick Stansbury ASP .Net 2 04-11-2005 10:21 AM
function declarations, global/within a function? Douglas C Programming 2 07-05-2004 08:54 PM



Advertisments