Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > if I define a function with no parameters, should i put void?

Reply
Thread Tools

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

 
 
TTroy
Guest
Posts: n/a
 
      02-17-2005
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

 
Reply With Quote
 
 
 
 
Andrey Tarasevich
Guest
Posts: n/a
 
      02-17-2005
TTroy wrote:
> 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".

--
Best regards,
Andrey Tarasevich
 
Reply With Quote
 
 
 
 
Thomas Matthews
Guest
Posts: n/a
 
      02-17-2005
Andrey Tarasevich wrote:
> TTroy wrote:
>
>>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".
>


<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

 
Reply With Quote
 
TTroy
Guest
Posts: n/a
 
      02-17-2005

Andrey Tarasevich wrote:
> TTroy wrote:
> > 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".
>


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?

 
Reply With Quote
 
Eric Sosman
Guest
Posts: n/a
 
      02-17-2005


TTroy wrote:
> Andrey Tarasevich wrote:
>
>>TTroy wrote:
>>
>>>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".
>
>
> 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.

--
http://www.velocityreviews.com/forums/(E-Mail Removed)

 
Reply With Quote
 
Luke Wu
Guest
Posts: n/a
 
      02-17-2005

TTroy wrote:
> Andrey Tarasevich wrote:
> > TTroy wrote:
> > > 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".
> >

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

 
Reply With Quote
 
Jens Marder
Guest
Posts: n/a
 
      02-17-2005
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.


"TTroy" <(E-Mail Removed)> schrieb im Newsbeitrag
news:(E-Mail Removed) oups.com...
> 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
>



 
Reply With Quote
 
Andrey Tarasevich
Guest
Posts: n/a
 
      02-17-2005
TTroy wrote:
>> ...
>> No. In a function _definition_ empty '()' immediately means "no

> parameters".
>>

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

--
Best regards,
Andrey Tarasevich
 
Reply With Quote
 
Andrey Tarasevich
Guest
Posts: n/a
 
      02-17-2005
Luke Wu wrote:
>> > ...
>> > No. In a function _definition_ empty '()' immediately means "no

>> parameters".
>> >

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


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

--
Best regards,
Andrey Tarasevich
 
Reply With Quote
 
Flash Gordon
Guest
Posts: n/a
 
      02-18-2005
Jens Marder wrote:

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

Top posting fixed.

> "TTroy" <(E-Mail Removed)> schrieb im Newsbeitrag
> news:(E-Mail Removed) oups.com...
>
>>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

>
> 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.
--
Flash Gordon
Living in interesting times.
Although my email address says spam, it is real and I read it.
 
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
About typedef -- define the function pointer or define function model? robin liu C Programming 3 04-21-2006 03:26 PM
#define _ and #define __ Brian Takita Ruby 0 01-23-2006 04:34 AM
write a function such that when ever i call this function in some other function .it should give me tha data type and value of calling function parameter komal C++ 6 01-25-2005 11:13 AM
How to define a define that defines some defines ? theotyflos C Programming 3 02-19-2004 05:07 PM
Is it possible to put the instance of ASP user define class into the session Galileo ASP General 0 10-21-2003 08:22 PM



Advertisments