if I define a function with no parameters, should i put void?

T

TTroy

For function definitions (not declarations/prototypes), is it necessary
to put void in the emptry braces if the function is to receive no
parameters? Does this turn any error checking off or cause any other
effects?
-thanks
 
A

Andrey Tarasevich

TTroy said:
For function definitions (not declarations/prototypes), is it necessary
to put void in the emptry braces if the function is to receive no
parameters? Does this turn any error checking off or cause any other
effects?

No. In a function _definition_ empty '()' immediately means "no parameters".
 
T

Thomas Matthews

Andrey said:
No. In a function _definition_ empty '()' immediately means "no parameters".

<Style Opinion>
I personally believe that placing "void" inside the parens
makes the code more readable. But that is my opinion.
</Style Opinion>

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.comeaucomputing.com/learn/faq/
Other sites:
http://www.josuttis.com -- C++ STL Library book
http://www.sgi.com/tech/stl -- Standard Template Library
 
T

TTroy

Andrey said:
No. In a function _definition_ empty '()' immediately means "no parameters".

Even if I use the empty () definition of a function as the only
declaration?

I also have another question. If I was calling a function and also
casting values of arguments, does this cast come BEFORE or AFTER the
automatic conversion caused by the prototype? I guess the question
applies when there is no prototype - does the case happen before or
after the promotions?
 
E

Eric Sosman

TTroy said:
parameters".


Even if I use the empty () definition of a function as the only
declaration?

Let's get specific: If you write

void f(void) { ... }
void g(void) { f(42); }

.... the compiler is required to issue a diagnostic for the
mismatch between the number of arguments in the call and the
number of parameters in the prototype. But if you write

void f() { ... }
void g(void) { f(42); }

.... the compiler is not required to catch the mistake.
I also have another question. If I was calling a function and also
casting values of arguments, does this cast come BEFORE or AFTER the
automatic conversion caused by the prototype? I guess the question
applies when there is no prototype - does the case happen before or
after the promotions?

Before. The cast operator is part of the expression
that calculates the value you provide as an argument, just
like other operators (+, -, ...) that might appear in the
expression. The expression yields a value of some type,
and conversions or promotions occur depending on what that
type is and on what the compiler knows about the function.
The cast has already been applied before this happens.
 
L

Luke Wu

TTroy said:
Even if I use the empty () definition of a function as the only
declaration?

empty () in declarations tell the compiler to assume nothing about
parameters (turns parameter checking off)
I also have another question. If I was calling a function and also
casting values of arguments, does this cast come BEFORE or AFTER the
automatic conversion caused by the prototype? I guess the question
applies when there is no prototype - does the case happen before or
after the promotions?

Before.

For example, back in the days when there were no prototypes, and people
wanted to send intergers to functions that accepted only floats, they
would do this:

int i = 7;
func((float)i);

These days casting is never required because prototypes do the same
thing, but casting is still used when "weird" conversions might cause
the compiler to complain.
 
J

Jens Marder

In ANSI-C that's fine (no void needed)

But in old Standard-C (K&R) code func() ment, that
0..many parameters are possible (NOT checked).

So if you don't come across OLD K&R-style C-code, you
NEEDN'T use func(void), but i guess it doesn't hurt, either.
 
A

Andrey Tarasevich

TTroy said:
Even if I use the empty () definition of a function as the only
declaration?
Yes.

I also have another question. If I was calling a function and also
casting values of arguments, does this cast come BEFORE or AFTER the
automatic conversion caused by the prototype? I guess the question
applies when there is no prototype - does the case happen before or
after the promotions?

Explicitly specified cast precedes any implicit conversions.
 
A

Andrey Tarasevich

Luke said:
empty () in declarations tell the compiler to assume nothing about
parameters (turns parameter checking off)
...

Once again, unless this function declaration also happens to be a
definition. In that case '()' explicitly means "no parameters".
 
F

Flash Gordon

Jens Marder wrote:

Please don't top post. You reply belongs after or intermixed with the
text you are replying to.

Top posting fixed.
In ANSI-C that's fine (no void needed)

You obviously don't know pre-ANSI C, since it was ANSI C that
*introduced* void to the language.
But in old Standard-C (K&R) code func() ment, that
0..many parameters are possible (NOT checked).

It means the same in *all* versions of C.
So if you don't come across OLD K&R-style C-code, you
NEEDN'T use func(void), but i guess it doesn't hurt, either.

If you use old K&R style C then you *can't* use void because it did not
exist then.

With ANSI/ISO C (as others have stated) you need to specify void if you
want the compiler to be required to diagnose parameters being passed to
a function that does not accept parameters.
 
J

Jack Klein

empty () in declarations tell the compiler to assume nothing about
parameters (turns parameter checking off)


Before.

For example, back in the days when there were no prototypes, and people
wanted to send intergers to functions that accepted only floats, they
would do this:

int i = 7;
func((float)i);

These days casting is never required because prototypes do the same
thing, but casting is still used when "weird" conversions might cause
the compiler to complain.

No, prototypes don't always do the same thing:

#include <stdio.h>

int main(void)
{
printf("%d", 1);
return 0;
}
 
P

Peter Nilsson

Andrey said:
Once again, unless this function declaration also happens to be a
definition. In that case '()' explicitly means "no parameters".

An empty declaration, even if part of a function definition, is
still not a prototype...

void foo()
{
foo(42); /* UB, but no diagnostic is required */
}

void baa(void)
{
baa(42); /* constraint violation */
}
 
L

Lawrence Kirby

Even if I use the empty () definition of a function as the only
declaration?

If you define a function with no paramaters then it has no parameters, how
else could it be? However if you don't specify the parameter list as void
(i.e. make it a prototype) you reduce the amount of error checking that
the compiler is required to do. So specifying void is a good thing.
I also have another question. If I was calling a function and also
casting values of arguments, does this cast come BEFORE or AFTER the
automatic conversion caused by the prototype? I guess the question
applies when there is no prototype - does the case happen before or
after the promotions?

The promotions happen after the argument expression has been evaluated
and any casts are part of the argument expression.

Lawrence
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top