473,614 Members | 2,076 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1856
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.l earn.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.goo glegroups.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.goo glegroups.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

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

Similar topics

14
4450
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, an inlined function or an inlined static method. //1
11
2699
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
2378
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( size_t size ); void *operator new( size_t size, AllocationType allocType ); void *operator new( size_t size, MemoryHandler* memHandler ); void *operator new( size_t size, MemoryHandler* memHandler, AllocationType
2
1598
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 of any friend function defined in that template (unless we want to prevent more than one instantiation of a class template in a particular file, but this is rather unlikely). Let's apply this to a variation of our previous example:
6
1845
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 This would be use to do something like this: void main(int argc,char *argv) { FILE *p2file=fopen(DMP_FILE,"w"); }
3
3640
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' that takes arguments of type (void *) because the ADT must be able to deal with any type of data. In my actual code, I will code the function to take arguments of their real types, then when I pass this pointer through an interface function, I...
18
4341
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 can use dlopen/whatever to convert the function name into a pointer to that function, but actually calling it, with the right number of parameters, isn't easy. As far as I can see, there are only two solutions: 1) This one is portable. If...
4
1745
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 handle_command_2(*data), etc.) through my DispatchCommand(*data) function. For example, only implementing two of those functions is enough for an executable, but for another executable other functions are necessary. There can be many executables like...
0
8182
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8130
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8627
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8579
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
7093
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5540
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4127
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1747
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1425
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.