473,883 Members | 1,528 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

What use is void?

Hello, I was wondering, does it make any difference if you write

void foo(int x)
{
/* insert code here */
}

or

foo(int x)
{
/* insert code here */
}

What is the difference?? Both ways the function wont return anything,
right? What is the point in writing void? Is it merely a cosmetic feature?

--
Ian Tuomi
Jyväskylä, Finland

GCS d- s+: a--- C++>$ L+>+++$ E- W+ N+ !o>+ w---
!O- !M- t+ !5 !X R+ tv- b++ DI+ !D G e->+++ h!

Nov 13 '05
23 22212
In message <Pi************ *************** ********@unix50 .andrew.cmu.edu >
"Arthur J. O'Dwyer" <aj*@andrew.cmu .edu> wrote:
"Jarno A Wuolijoki" <jw******@cs.He lsinki.FI> wrote...
Someone correct me if I'm wrong, but IIRC it's OK to fall through
without returning as long as you don't check the return value.

Or not even fall through -- you can insert a blank

return;

in the function for the same effect (immediate return to caller,
undefined return value).


Not as of C99. Both of your examples are illegal in C99.
In such cases, the Prime Directive of Misplaced Efficiency Tuning
dictates that we use 'return;' in place of 'return 0;', to save
the single "load" instruction. :-)


Indeed. I don't think there was a Misplaced Efficiency Tuning representative
on the C99 committee, alas.

--
Kevin Bracey, Principal Software Engineer
Tematic Ltd Tel: +44 (0) 1223 503464
182-190 Newmarket Road Fax: +44 (0) 1223 503458
Cambridge, CB5 8HE, United Kingdom WWW: http://www.tematic.com/
Nov 13 '05 #21

On Mon, 22 Sep 2003, Kevin Bracey wrote:

"Arthur J. O'Dwyer" <aj*@andrew.cmu .edu> wrote:
"Jarno A Wuolijoki" <jw******@cs.He lsinki.FI> wrote...
> Someone correct me if I'm wrong, but IIRC it's OK to fall through
> without returning as long as you don't check the return value.


Or not even fall through -- you can insert a blank

return;

in the function for the same effect (immediate return to caller,
undefined return value).


Not as of C99. Both of your examples are illegal in C99.


Hmm... N869 says you're right.

6.8.6.4 The return statement

Constraints

[#1] A return statement with an expression shall not appear
in a function whose return type is void. A return statement
without an expression shall only appear in a function whose
return type is void.
I wonder where I became so sure of what I posted earlier... I
distinctly remember seeing the opinion I posted, somewhere else
recently. I thought it was somewhere in N869, but apparently
not.

Thanks for the correction.

-Arthur
Nov 13 '05 #22
In message <Pi************ *************** *******@unix48. andrew.cmu.edu>
"Arthur J. O'Dwyer" <aj*@nospam.and rew.cmu.edu> wrote:
On Mon, 22 Sep 2003, Kevin Bracey wrote:

"Arthur J. O'Dwyer" <aj*@andrew.cmu .edu> wrote:
Or not even fall through -- you can insert a blank

return;

in the function for the same effect (immediate return to caller,
undefined return value).


Not as of C99. Both of your examples are illegal in C99.


I wonder where I became so sure of what I posted earlier... I
distinctly remember seeing the opinion I posted, somewhere else
recently. I thought it was somewhere in N869, but apparently
not.


Well, it was perfectly legal to "return;" in a non-void function before C99;
maybe you saw it in a C90 context.

--
Kevin Bracey, Principal Software Engineer
Tematic Ltd Tel: +44 (0) 1223 503464
182-190 Newmarket Road Fax: +44 (0) 1223 503458
Cambridge, CB5 8HE, United Kingdom WWW: http://www.tematic.com/
Nov 13 '05 #23
On Fri, 19 Sep 2003, Ian Tuomi wrote:
Hello, I was wondering, does it make any difference if you write

void foo(int x)
{
/* insert code here */
}

or

foo(int x)
{
/* insert code here */
}

What is the difference?? Both ways the function wont return anything,
right? What is the point in writing void? Is it merely a cosmetic feature?
Incorrect. The second version will return an int. All too often new
programmers assume that having no return value indicates no return value
is necessary. The truth is that the second version returns an int. These
are the same:

int foo(int x)
{
/* insert code here */
/* make sure you return an int */
}

foo(int x)
{
/* insert code here */
/* make sure you return an int */
}

The purpose of void is to tell the compiler the function does not default
to returning an int.
--
Ian Tuomi
Jyväskylä, Finland

GCS d- s+: a--- C++>$ L+>+++$ E- W+ N+ !o>+ w---
!O- !M- t+ !5 !X R+ tv- b++ DI+ !D G e->+++ h!


--
darrell at cs dot toronto dot edu
or
main(){int j=1234;char t[]=":@abcdefghijk lmnopqrstuvwxyz .\n",*i=
"iqgbgxmdbjlgdv .lksrqek.n";cha r *strchr(const char *,int);while(
*i){j+=strchr(t ,*i++)-t;j%=sizeof t-1;putchar(t[j]);} return 0;}
Nov 13 '05 #24

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

Similar topics

21
2258
by: tyler_durden | last post by:
hi there peeps... like I say in the topic, I need to do an e-mail program in C language, and has to be made until the 3th of january..the problem is I'm having some problems with it. I was wondering if someone had a program like this..it has to send e-mails, read e-mails from a .txt file and show them in the screen with the subject, date, etc... can someone please help me out? thanks
13
2200
by: ranjeet.gupta | last post by:
Dear All What does exactly below code means struct File { void* data; }; typedef struct File File; typedef File* fl;
22
28992
by: nick | last post by:
i do not know what is the use of (e.g. void *pt), when will use it. thanks!
16
10416
by: Abhishek | last post by:
why do I see that in most C programs, pointers in functions are accepted as: int func(int i,(void *)p) where p is a pointer or an address which is passed from the place where it is called. what do you mean by pointing to a void and why is it done? Aso, what happens when we typecast a pointer to another type. say for example int *i=(char *)p; under different situations? I am kind of confused..can anybody clear this confusion by clearly...
21
6550
by: Niu Xiao | last post by:
I see a lot of use in function declarations, such as size_t fread(void* restrict ptr, size_t size, size_t nobj, FILE* restrict fp); but what does the keyword 'restrict' mean? there is no definition found in K&R 2nd.
5
4424
by: Niu Xiao | last post by:
I saw a lot of codes like: void foo(void* arg) void bar(void** arg) f((void*)p) but what does void pointer mean in c? I just know it stands for generic pointer. thanks.
2
2762
by: dasilva109 | last post by:
Hi guys I am new to C++ and need urgent help with this part of my code for a uni coursework I have to submit by Thursday //ClientData.h #ifndef CLIENTDATA_H #define CLIENTDATA_H #include <string>
3
2018
by: smorrey | last post by:
Howdy folks, I recently purchased a book on C++ MUD creation and it features alot of nifty tidbits. The book is MUD GAME PROGRAMMING by Ron Penton Publisher: Premier Press Anyways of particular interest and what drew me to the book was the crossplatform threading library. But the problem is it appears to be broken at a single line of code,
6
1714
by: Shraddha | last post by:
What is this exactly... int(*(*ptr (int))(void) First I thought that this is the pointer to function...But I recognize that the syntax iss quite different... If we say that the function is taking void parameters i.e. no parameters then what "(int i)" is doing there...
9
1943
by: xiao | last post by:
It always dumped when I tried to run it... But it compiles OK. What I want to do is to do a test: Read information from a .dat file and then write it to another file. The original DAT file is like this : (very simple..........) 010001010110001101010101010101010101010101 #include<stdio.h>
0
9932
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
9777
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,...
1
10833
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10405
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9558
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...
1
7959
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5980
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4602
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4200
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.