473,408 Members | 1,749 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

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

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

Nov 14 '05 #1
12 1834
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
Nov 14 '05 #2
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

Nov 14 '05 #3

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?

Nov 14 '05 #4


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.

--
Er*********@sun.com

Nov 14 '05 #5

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.

Nov 14 '05 #6
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" <ti*****@gmail.com> schrieb im Newsbeitrag
news:11**********************@g14g2000cwa.googlegr 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

Nov 14 '05 #7
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
Nov 14 '05 #8
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
Nov 14 '05 #9
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" <ti*****@gmail.com> schrieb im Newsbeitrag
news:11**********************@g14g2000cwa.googlegr 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.
Nov 14 '05 #10
On 17 Feb 2005 12:34:48 -0800, "Luke Wu" <Lo***********@gmail.com>
wrote in comp.lang.c:

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.


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

#include <stdio.h>

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

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Nov 14 '05 #11
Andrey Tarasevich wrote:
Luke Wu wrote:
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".


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 */
}

--
Peter

Nov 14 '05 #12
On Thu, 17 Feb 2005 12:01:42 -0800, 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?


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
Nov 14 '05 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: Carl Ribbegaardh | last post by:
What other c++ constructs can I use instead of #define for executing a couple of functions? Example: #define DO_STUFF doThis(); doThat(); I'd guess that I can either use a template function,...
11
by: JKop | last post by:
Take the following simple function: unsigned long Plus5Percent(unsigned long input) { return ( input + input / 20 ); } Do yous ever consider the possibly more efficent:
5
by: kUfa.scoopex | last post by:
Hi there! I have a small problem, and i really dont see any convenient way to fix it. Basically, i'd like to overload global new operators, into something like this: void *operator new(...
2
by: baumann | last post by:
hi all, i am reading the C++ templates complete guide, i have question on the following statement. We must therefore make sure the template parameters of the class template appear in the type...
6
by: msigwald | last post by:
The following line of code works, however, since my professor is a real purist of c, I would like to know if this code is valid (is it good code or a piece of crap?): #define DMP_FILE argv ...
3
by: Beta What | last post by:
Hello, I have a question about casting a function pointer. Say I want to make a generic module (say some ADT implementation) that requires a function pointer from the 'actual/other modules'...
18
by: John Friedland | last post by:
My problem: I need to call (from C code) an arbitrary C library function, but I don't know until runtime what the function name is, how many parameters are required, and what the parameters are. I...
4
by: adnan.yalin | last post by:
Could you suggest solutions to a problem currently I have? Problem: Let's say I have many different commands and I call a different function for each (void handle_command_1(*data), void...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.