Velocity Reviews - Computer Hardware Reviews

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

Reply
Thread Tools

Function and variable declarations

 
 
Christopher Benson-Manica
Guest
Posts: n/a
 
      11-27-2005
jacob navia <> wrote:

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


Indeed. We, until recently, had global variables argc and argv (you
read that right) visible to all portions of all modules. It was only
after a number of predictable problems had been caused that this
design faux pas was undone.

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
 
Reply With Quote
 
 
 
 
Joseph Wakeling
Guest
Posts: n/a
 
      11-27-2005
Thanks to all for helpful answers about this!


Flash Gordon wrote:
> Global (or at least file scope) variable named x and n? Yuk.


Normally I would agree. Originally these variable came as part of a
structure called "state", so the system was referring to state->x,
state->n and state->shuffle. Given the content of the program and how
it's going to be used (as an individually compiled module) it's not a
big deal.


> > return;

>
> Rather pointless having a return here IMHO.


That came with the GSL and I left it in. return; at the end of a void
function is a matter of preference anyway, right? It doesn't
functionally affect things but some people prefer it for readability.


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


OK. Does it make any difference to the speed at which the code runs?
Let's assume I'm going to be calling this function a LOT so small
differences add up.

Many thanks again for this & all other useful advice.

-- Joe

 
Reply With Quote
 
 
 
 
Joseph Wakeling
Guest
Posts: n/a
 
      11-27-2005
Christopher Benson-Manica wrote:
> We, until recently, had global variables argc and argv (you
> read that right) visible to all portions of all modules. It was only
> after a number of predictable problems had been caused that this
> design faux pas was undone.


How do you go about making variables global with respect to several
modules? I know about using the extern declaration for variables in a
module, but I thought that was generally pretty bad coding practice
because it limits the ability to make the modules stand effectively on
their own.

-- Joe

 
Reply With Quote
 
pemo
Guest
Posts: n/a
 
      11-27-2005

"Joseph Wakeling" <> wrote in message
news: ups.com...
> Christopher Benson-Manica wrote:
>> We, until recently, had global variables argc and argv (you
>> read that right) visible to all portions of all modules. It was only
>> after a number of predictable problems had been caused that this
>> design faux pas was undone.

>
> How do you go about making variables global with respect to several
> modules? I know about using the extern declaration for variables in a
> module, but I thought that was generally pretty bad coding practice
> because it limits the ability to make the modules stand effectively on
> their own.


There are basically only two ways, i.e., 1. have a defining declaration in
one .c module (i.e., a variable/const placed outside of any function body),
and then access that via extern in other modules - then the linker will
resolve the situation. 2. Use pointers, i.e., have a function in the same
module as the 'static' global that returns the address of variable/const to
a caller - although that rather defeats the purpose I think.



 
Reply With Quote
 
pemo
Guest
Posts: n/a
 
      11-27-2005

"Flash Gordon" <> wrote in message
news:...
> Malcolm wrote:
>> "Skarmander" <> 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.


I'm still a bit 'old school' I'm afraid, and typically declare my variables
at the top of a function - no matter where they're used in that function.

Many years ago, I would have loved having some mechanism that allowed me to
declare them 'nearer' to where they were used - but, then, C wouldn't allow
that. Nowadays, [being an 'old dog' that can't learn 'new tricks'] I stick
with my old habit. However, one thing has improved so much that it's made
things easier, namely, decent IDEs. Years ago, when one only had dumb
editors, it was sometimes really hard to find where things had been
declared/defined. but now, well, I just ask the IDE, and it shows me where a
'thing' is. That's allowed me to learn one new trick however - I no longer
find that hungarian notation is terribly useful.


 
Reply With Quote
 
Flash Gordon
Guest
Posts: n/a
 
      11-27-2005
pemo wrote:

<snip>

> I'm still a bit 'old school' I'm afraid, and typically declare my variables
> at the top of a function - no matter where they're used in that function.
>
> Many years ago, I would have loved having some mechanism that allowed me to
> declare them 'nearer' to where they were used - but, then, C wouldn't allow
> that. Nowadays, [being an 'old dog' that can't learn 'new tricks'] I stick


Must be very old school, since it is allowed by C89.

> with my old habit. However, one thing has improved so much that it's made
> things easier, namely, decent IDEs. Years ago, when one only had dumb
> editors, it was sometimes really hard to find where things had been
> declared/defined. but now, well, I just ask the IDE, and it shows me where a
> 'thing' is. That's allowed me to learn one new trick however - I no longer
> find that hungarian notation is terribly useful.


I have never found hungarian notation useful.
--
Flash Gordon
Living in interesting times.
Although my email address says spam, it is real and I read it.
 
Reply With Quote
 
Flash Gordon
Guest
Posts: n/a
 
      11-27-2005
Joseph Wakeling wrote:
> Thanks to all for helpful answers about this!
>
> Flash Gordon wrote:
>> Global (or at least file scope) variable named x and n? Yuk.

>
> Normally I would agree. Originally these variable came as part of a
> structure called "state", so the system was referring to state->x,
> state->n and state->shuffle. Given the content of the program and how
> it's going to be used (as an individually compiled module) it's not a
> big deal.


I stand by my comment. If I had to review that code I would tell you to
go and fix it. After all, some other poor chap may have to maintain that
code.

>>> return;

>> Rather pointless having a return here IMHO.

>
> That came with the GSL and I left it in. return; at the end of a void
> function is a matter of preference anyway, right? It doesn't
> functionally affect things but some people prefer it for readability.


Agreed.

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

>
> OK. Does it make any difference to the speed at which the code runs?
> Let's assume I'm going to be calling this function a LOT so small
> differences add up.


It depends. The only way to find out it to measure on your specific
system. However, my opinion would be that with modern compilers it will
generally make absolutely no difference one way or the other.

If you are concerned with speed the first thing to do is forget it
unless you have a very good reason to think it won't be fast enough.

If you actually do find yourself with a real performance issue, don't
start by looking at the code, start by looking at the algorithm to see
if there is a more efficient algorithm.

My experience is that trying to optimise one function only helps if that
function is very badly written, the compiler is very bad, or you are
doing embedded work and there is a time constraint on that specific
function (e.g. it has to complete during the video blanking period).
Other than those rare cases there is more to be had by improving the
algorithm.
--
Flash Gordon
Living in interesting times.
Although my email address says spam, it is real and I read it.
 
Reply With Quote
 
Joseph Wakeling
Guest
Posts: n/a
 
      11-27-2005

Flash Gordon wrote:
> I stand by my comment. If I had to review that code I would tell you to
> go and fix it. After all, some other poor chap may have to maintain that
> code.


I'm being lazy right now because the chance of anyone else even *using*
that code, let alone maintaining it, are about zero. But you're right.



> My experience is that trying to optimise one function only helps if that
> function is very badly written, the compiler is very bad, or you are
> doing embedded work and there is a time constraint on that specific
> function (e.g. it has to complete during the video blanking period).
> Other than those rare cases there is more to be had by improving the
> algorithm.


My experience too. But if one is running the same function millions of
times, every little helps...

 
Reply With Quote
 
pemo
Guest
Posts: n/a
 
      11-27-2005

"Flash Gordon" <> wrote in message
news...
> pemo wrote:
>
> <snip>
>
>> I'm still a bit 'old school' I'm afraid, and typically declare my
>> variables at the top of a function - no matter where they're used in that
>> function.
>>
>> Many years ago, I would have loved having some mechanism that allowed me
>> to declare them 'nearer' to where they were used - but, then, C wouldn't
>> allow that. Nowadays, [being an 'old dog' that can't learn 'new tricks']
>> I stick

>
> Must be very old school, since it is allowed by C89.


Um, let's see - can't remember exactly when I started, but it was around
1980 [and I used to be quite good at it then I believe]

>> with my old habit. However, one thing has improved so much that it's
>> made things easier, namely, decent IDEs. Years ago, when one only had
>> dumb editors, it was sometimes really hard to find where things had been
>> declared/defined. but now, well, I just ask the IDE, and it shows me
>> where a 'thing' is. That's allowed me to learn one new trick however - I
>> no longer find that hungarian notation is terribly useful.

>
> I have never found hungarian notation useful.


I had to use it - guess where I used to work!



 
Reply With Quote
 
Christopher Benson-Manica
Guest
Posts: n/a
 
      11-28-2005
Joseph Wakeling <> wrote:

> How do you go about making variables global with respect to several
> modules?


I spoke poorly there. What I should have said is that our "main"
header file that everything includes used to define these variables as
global. It made for more than its share of headaches.

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
 
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