Empty Function parameter lists 
Author Message
 Empty Function parameter lists

Hello all,

I'm not sure if this question should be in a C++ group or not.

Why is it considered bad programming style to set the argument list to void
if there are no
parameters for a function.?

For example, this is considered bad...

int MyFunction(void);

int MyFunction(void)
{
    ... do something
    return 1;

Quote:
}

.... while this is considered better...

int MyFunction();

int MyFunction()
{
    ... do something
    return 1;

Quote:
}

Thanks in advance,
Greg.

--



Mon, 05 Nov 2001 03:00:00 GMT  
 Empty Function parameter lists

Quote:

>Hello all,

>I'm not sure if this question should be in a C++ group or not.

>Why is it considered bad programming style to set the argument list to void
>if there are no
>parameters for a function.?

I'd rather say the other way round.

Quote:
>For example, this is considered bad...

>int MyFunction(void);
[...]
>.... while this is considered better...

>int MyFunction();

[...]

Well, in C () means an unknown number of arguments of unknown type
while in C++ it means no arguments. Now since this is a C group ...
void foo()
... is it okay to call foo(), or foo(1), or foo("I_am_a_foo") ?
Using foo(void) will tell you, and the compiler, "yep, no arguments".

Cheers,
Juergen

--
\ Real name     : Jrgen Heinzl                 \       no flames      /

--



Mon, 05 Nov 2001 03:00:00 GMT  
 Empty Function parameter lists

Quote:

> Hello all,

> I'm not sure if this question should be in a C++ group or not.

> Why is it considered bad programming style to set the argument list to void
> if there are no
> parameters for a function.?

In alt.comp.lang.learn.c-c++, a poster claimed it was "much better" in C++ to
use
   int foo();
rather than
   int foo(void);

Since these mean different things in C,
a) int foo(void);
   is obviously to be prefered in C, since it (and not the other form) mean that
the function takes no parameters.
b) int foo(void);
   should be prefered in C++ as well, since its meaning is invariant across
languages (which the other form is not) and it can never be "much better" to
leave implicit what can trivially be made explicit with no harm to readibility.

--


--



Fri, 09 Nov 2001 03:00:00 GMT  
 Empty Function parameter lists
This is a c++ question (as the standard stands at the moment, I think)

Once apon a time, a C function would be written:

int fred()
int i;  /* fred takes one int argument */
{ dostuff }


int fred( int i) /* fred takes one int argument */
{ dostuff }

but then the question arose - how do I describe a function that takes
no arguments?  Because if I write "fred()", is that an old-style
declaration or a new-style declaration that takes no arguments?
Remember that c++ and ansi c had to support all the old stuff as well
as the new stuff. So they came up with the 'void' argument:

int fred(void) /* fred takes no arguments */
{ dostuff  }

Of course, using 'fred()' would be nicer, but that would break a lot
of old code.

As of not so long ago, however, C++ decided that this has been going
on long enough for people to have converted their code to new style,
and have thrown out support for the old style declarations.  So
"fred()" just means that fred takes no arguments - and the first
example above is a syntax error.  If someone wants to compile old
code, they have to use a C compiler.

One day they plan to do away with the 'int fred(void)' syntax - so
that's why it's wise not to use it.

Didn't think it would take that long to explain...

Pete


Quote:
>Hello all,

>I'm not sure if this question should be in a C++ group or not.

>Why is it considered bad programming style to set the argument list to void
>if there are no parameters for a function.?

>For example, this is considered bad...

>int MyFunction(void);

>int MyFunction(void)
>{
>    ... do something
>    return 1;
>}

>.... while this is considered better...

>int MyFunction();

>int MyFunction()
>{
>    ... do something
>    return 1;
>}

>Thanks in advance,
>Greg.

--



Fri, 09 Nov 2001 03:00:00 GMT  
 Empty Function parameter lists


Quote:
>Hello all,

>I'm not sure if this question should be in a C++ group or not.

Depends.  Are you talking about C or C++?  Since this is a C group,
I'll assume the former.  There _are_ differences...

Quote:

>Why is it considered bad programming style to set the argument list to void
>if there are no
>parameters for a function.?

It's not.  Not in C, anyway.

Quote:

>For example, this is considered bad...

>int MyFunction(void);

>int MyFunction(void)
>{
>    ... do something
>    return 1;
>}

Looks OK to me...

Quote:

>.... while this is considered better...

>int MyFunction();

This doesn't mean the same thing as

   int MyFunction(void);

The former says MyFunction returns int, but doesn't say anything about
what (if any) parameters it requires.  The latter explicitly says it
takes no parameters.  

The former would not pass one of _my_ code reviews...

Quote:

>int MyFunction()
>{
>    ... do something
>    return 1;
>}

This is, however equivalent to the previous function.

Regards,

                          -=Dave
Just my (10-010) cents
I can barely speak for myself, so I certainly can't speak for B-Tree.
Change is inevitable.  Progress is not.
--



Fri, 09 Nov 2001 03:00:00 GMT  
 Empty Function parameter lists

Quote:
Peter Stephenson writes:
> Once apon a time, a C function would be written:

> int fred()
> int i;     /* fred takes one int argument */
> { dostuff }

Uh, no.
Once upon a time, a B function would be written:

  fred(i)
  { dostuff }

And this is still legal C today -- but it's highly discouraged,
and is to be prohibited under the new standard nicknamed C9X.

The above is equivalent to the following; note the syntactic
correction from Peter's version.

  int fred(i)  /* fred takes one argument ... */
  int i;       /* ... and its type is int */
  { dostuff }

Any other type than int has always had to be declared explicitly.

However, if were not defining this function, but merely declaring
it so you could make a call to it, *then* you wrote it without
parameters.  For example:

   extern int fred();

Quote:

> int fred( int i) /* fred takes one int argument */
> { dostuff }

> but then the question arose - how do I describe a function that takes
> no arguments?  Because if I write "fred()", is that an old-style
> declaration or a new-style declaration that takes no arguments?
> Remember that c++ and ansi c had to support all the old stuff as well
> as the new stuff. So they came up with the 'void' argument:

> int fred(void) /* fred takes no arguments */
> { dostuff  }

Right.
--
Mark Brader                     "C was developed for the programmer
Toronto                          (two of them, in fact)"

My text in this article is in the public domain.
--



Fri, 09 Nov 2001 03:00:00 GMT  
 Empty Function parameter lists

Quote:
> This is a c++ question (as the standard stands at the moment, I think)

> Once apon a time, a C function would be written:

> int fred()
> int i;     /* fred takes one int argument */
> { dostuff }

Nope -- that was:

int fred(I)
int I;
{ whatever }

However, the important point is that to declare the function, you
used:

int fred();

without any mention of the parameters at all.  Since basically all
previous C code used this style, when they first wrote a standard,
they felt obliged to allow code that used this style to continue being
legal.  Therefore, a function declaration that doesn't include any
parameter declarations, means the function is declared to take some
unknown set of parameters (including the possibility of taking no
parameters at all).

If you declare a function like this, the compiler can't check that the
calls you make are really correct.
--



Fri, 09 Nov 2001 03:00:00 GMT  
 Empty Function parameter lists

Quote:

> b) int foo(void);
>    should be prefered in C++ as well, since its meaning is invariant across
> languages (which the other form is not) and it can never be "much better" to
> leave implicit what can trivially be made explicit with no harm to readibility.

Interesting. Since variable names beginning with i, j, k, l, m, and n
are implicitly ints in fortran, should we all adopt that as a convention
in C and C++?

--
Pete Becker
Dinkumware, Ltd.
http://www.dinkumware.com
--



Sat, 10 Nov 2001 03:00:00 GMT  
 Empty Function parameter lists

Quote:


> > b) int foo(void);
> >    should be prefered in C++ as well, since its meaning is invariant across
> > languages (which the other form is not) and it can never be "much better" to
> > leave implicit what can trivially be made explicit with no harm to readibility.

> Interesting. Since variable names beginning with i, j, k, l, m, and n
> are implicitly ints in FORTRAN, should we all adopt that as a convention
> in C and C++?

I suppose that Mr. Becker is not aware that the preferred usage in Fortran is
that programs use the statement
      implicit none
Since he thinks that the language is called FORTRAN, it appears that he is still
living in the 60s.

Remember that it is the C++-cultists who {*filter*} us with the "C is a subset of
C++" crap.  Yet here we see an appeal to use constructs with different semantics
when the same semantics could be easily preserved.  

--


--



Sat, 10 Nov 2001 03:00:00 GMT  
 Empty Function parameter lists

Quote:



> > > b) int foo(void);
> > >    should be prefered in C++ as well, since its meaning is invariant across
> > > languages (which the other form is not) and it can never be "much better" to
> > > leave implicit what can trivially be made explicit with no harm to readibility.

> > Interesting. Since variable names beginning with i, j, k, l, m, and n
> > are implicitly ints in FORTRAN, should we all adopt that as a convention
> > in C and C++?

> I suppose that Mr. Becker is not aware that the preferred usage in Fortran is
> that programs use the statement
>       implicit none

Apparently when Mr. Ambuhl said that a construct "should be
prefered[sic] [if it's] meaning is invariant across languages", what he
meant is that it should be preferred unless he thinks there is a
"preferred usage" that overrides his rule. And since he does not accept
that not using void is a "preferred usage" in C++, he need not override
his rule. So the only question left to be resolved is what his criteria
are for recognizing "preferred usage," which are, so far, unstated.

--
Pete Becker
Dinkumware, Ltd.
http://www.dinkumware.com
--



Sun, 11 Nov 2001 03:00:00 GMT  
 Empty Function parameter lists
On Tue, 25 May 1999 17:27:58 GMT, Pete Becker

Quote:


>> b) int foo(void);
>>    should be prefered in C++ as well, since its meaning is invariant across
>> languages (which the other form is not) and it can never be "much better" to
>> leave implicit what can trivially be made explicit with no harm to readibility.

>Interesting. Since variable names beginning with i, j, k, l, m, and n
>are implicitly ints in FORTRAN, should we all adopt that as a convention
>in C and C++?

And don't forget to fully parenthesize expressions like

        (a * b) + c

so they will have the same meaning in C++ as in APL. :-)

--
Michael M Rubenstein
--



Sun, 11 Nov 2001 03:00:00 GMT  
 Empty Function parameter lists

Quote:

>On Tue, 25 May 1999 17:27:58 GMT, Pete Becker


>>> b) int foo(void);
>>>    should be prefered in C++ as well, since its meaning is invariant across
>>> languages (which the other form is not) and it can never be "much better" to
>>> leave implicit what can trivially be made explicit with no harm to readibility.

>>Interesting. Since variable names beginning with i, j, k, l, m, and n
>>are implicitly ints in FORTRAN, should we all adopt that as a convention
>>in C and C++?

>And don't forget to fully parenthesize expressions like

>    (a * b) + c

>so they will have the same meaning in C++ as in APL. :-)

     And don't ever return any values with a return statement.  Some
BASICs just can't cope.  Speaking of BASIC, dangling semicolons at the
end of print statements could get rather sticky.

     We can eliminate gotos once and for all.  Since in xBASE, goto is
for moving the record pointer of a table (not transfer of control), it
would seem no cross-compatible use of the statement could be made and
thus it couldn't be used at all.  Well, considering how dense the last
sentence was, maybe we can use goto for its prestructured (whatever
that is) programming use of causing confusion.

     IIRC, "==" is used in DEC BASIC-Plus for an inexact comparison
(a==b if they print the same).  We could wipe out all of the confusion
in C of
          if (a=b)
and
          if (a==b)

     I am sure that there are plenty of other cases where certain
inappropriate situations could be dealt with once and for all.

Sincerely,

Gene Wirchenko

Computerese Irregular Verb Conjugation:
     I have preferences.
     You have biases.
     He/She has prejudices.
--



Sun, 11 Nov 2001 03:00:00 GMT  
 Empty Function parameter lists

Quote:




>> > > b) int foo(void);
>> > >    should be prefered in C++ as well, since its meaning is invariant across
>> > > languages (which the other form is not) and it can never be "much better" to
>> > > leave implicit what can trivially be made explicit with no harm to readibility.

     2+2=4.

     Well, that was certainly trivial to make explicit.  I don't think
it hurt the readability either.

     "But, Doctor, I still have this nagging feeling that I'm being
silly!"

Quote:
>> > Interesting. Since variable names beginning with i, j, k, l, m, and n
>> > are implicitly ints in FORTRAN, should we all adopt that as a convention
>> > in C and C++?

     It isn't part of the language.  It is in PL/I though.

Quote:
>> I suppose that Mr. Becker is not aware that the preferred usage in Fortran is
>> that programs use the statement
>>       implicit none

     It can't be all that preferred if it isn't the default!

Quote:
>Apparently when Mr. Ambuhl said that a construct "should be
>prefered[sic] [if it's] meaning is invariant across languages", what he

                       ^[sic]  (Try "its".)
      Look, folks!  Almost of a sighting of the rare nested "sic"s!

Quote:
>meant is that it should be preferred unless he thinks there is a
>"preferred usage" that overrides his rule. And since he does not accept
>that not using void is a "preferred usage" in C++, he need not override
>his rule. So the only question left to be resolved is what his criteria
>are for recognizing "preferred usage," which are, so far, unstated.

     See my sig for a clue about *that*.

Sincerely,

Gene Wirchenko

Computerese Irregular Verb Conjugation:
     I have preferences.
     You have biases.
     He/She has prejudices.
--



Sun, 11 Nov 2001 03:00:00 GMT  
 Empty Function parameter lists

Quote:


> > I'm not sure if this question should be in a C++ group or not.

> > Why is it considered bad programming style to set the argument list to void
> > if there are no parameters for a function.?

> In alt.comp.lang.learn.c-c++, a poster claimed it was "much better" in C++ to
> use
>    int foo();
> rather than
>    int foo(void);

Quoting from my copy of the C++ FAQs[1] p27:

"Note that C programmers use (void) to declare a function that takes no
parameters, but this is not necessary in C++.  Be warned that some C++
developers consider the (void) syntax in C++ code to be an indicator
that the author of the code is still a warmed over C programmer - that
the author hasn't yet made the paradigm shift.  This is an unfair
judgement, but it might be wise to use () rather than (void) in C++
code."

[1] _C++ FAQs, Second Edition_,
        Marshall Cline et al, Addison Wesley,
        ISBN 0-201-30983-1
--



Mon, 12 Nov 2001 03:00:00 GMT  
 Empty Function parameter lists

Quote:

>> b) int foo(void);
>>    should be prefered in C++ as well, since its meaning is invariant across
>> languages (which the other form is not) and it can never be "much better"
>to
>> leave implicit what can trivially be made explicit with no harm to
>readibility.

>Interesting. Since variable names beginning with i, j, k, l, m, and n
>are implicitly ints in FORTRAN, should we all adopt that as a convention
>in C and C++?

It already is.  If you declare a variable without specifying a type, it is an
int. For example,

   static i;

is the same as

   static int i;
--



Tue, 27 Nov 2001 03:00:00 GMT  
 
 [ 16 post ]  Go to page: [1] [2]

 Relevant Pages 

1. Preferred empty parameter list function declaration.

2. Calling DLL functions without known function name, parameter-list until run-time

3. dedicated (002b) free list non-empty but marked as empty

4. Correct rules for function macro parameter lists

5. Function parameter lists

6. dynamic parameter list for external function

7. ADODB.Recordset::Execute with empty parameters.

8. Empty output parameters when executing stored procedure through ODBC

9. Empty strings as parameter for P/Invoke calls

10. Call a function in a function with name of a function as parameter

11. function parameters: default value, optional parameter

12. Initializer list can not be empty

 

 
Powered by phpBB® Forum Software