473,836 Members | 1,560 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 22202

"Jarno A Wuolijoki" <jw******@cs.He lsinki.FI> wrote in message
news:Pi******** *************** *************** @melkinpaasi.cs .Helsinki.FI...
On Fri, 19 Sep 2003, Mike Wahler wrote:
foo(int x)
{
/* insert code here */


/* insert required return statement here */
}


In C99, this is invalid. A function *must* declare
a return type. In C89, the omission of a return
type from a function declaration causes the return
type to default to 'int'. So in C89 this is also
invalid, since the function is declared to return
type 'int' but does not return a value.


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.


But what would be the point? Either you are returning a value your not
checking, or one that has a random--thus useless--value.
Nov 13 '05 #11

On Fri, 19 Sep 2003, Xenos wrote:

"Jarno A Wuolijoki" <jw******@cs.He lsinki.FI> wrote...
On Fri, 19 Sep 2003, Mike Wahler wrote:

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

In C99, this is invalid. A function *must* declare
a return type. In C89, the omission of a return
type from a function declaration causes the return
type to default to 'int'. So in C89 this is also
invalid, since the function is declared to return
type 'int' but does not return a value.
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).
But what would be the point? Either you are returning a value your not
checking, or one that has a random--thus useless--value.

int silly_sprintf(c har *buffer, const char *fmt, ...)
{
if (buffer == NULL) {
how_many_charac ters = some_function_o f_(input);
return how_many_charac ters;
}
else {
strcpy(buffer, some_other_func tion_of_(input) ;
return;
}
}
The idea being that in *some* cases, maybe you want to return
useful information, and in *some* cases, there's no useful
information to return. Obviously, this is a bad example;
a call to silly_sprintf(n on_null, "foo") could return an
integer too. But I'm sure such cases could exist.

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. :-)
More practically, in modern C you can even have a function
that returns a structure directly:

struct foo return_a_foo(in t should_i)
{
if (should_i)
return (struct foo){0, 1, should_i, "and so on", 42, "infinity"} ;
else
return;
}

The time saved in not constructing the return value might
really add up, in this case!

HTH,
-Arthur
Nov 13 '05 #12
In <Pi************ *************** ***********@mel kinpaasi.cs.Hel sinki.FI> Jarno A Wuolijoki <jw******@cs.He lsinki.FI> writes:
On Fri, 19 Sep 2003, Mike Wahler wrote:
> foo(int x)
> {
> /* insert code here */


/* insert required return statement here */
> }


In C99, this is invalid. A function *must* declare
a return type. In C89, the omission of a return
type from a function declaration causes the return
type to default to 'int'. So in C89 this is also
invalid, since the function is declared to return
type 'int' but does not return a value.


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.


Right. Back in the days of K&R C, when there were no void functions,
some people used the following convention:

int foo() { ... return something; }

for functions that were supposed to return something and

bar() { ... }

for functions that were not supposed to return anything (the equivalent
of void functions in standard C).

But from the language point of view, there is no difference between the
two function definitions: both are defined as taking no parameters and
returning int. To avoid breaking such code, C89 allowed non-void
functions that don't return anything, as long as the caller is not
attempting to use the return value.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 13 '05 #13

"Jarno A Wuolijoki" <jw******@cs.He lsinki.FI> wrote in message
news:Pi******** *************** *************** @melkinpaasi.cs .Helsinki.FI...
On Fri, 19 Sep 2003, Mike Wahler wrote:
foo(int x)
{
/* insert code here */
/* insert required return statement here */
}


In C99, this is invalid. A function *must* declare
a return type. In C89, the omission of a return
type from a function declaration causes the return
type to default to 'int'. So in C89 this is also
invalid, since the function is declared to return
type 'int' but does not return a value.


Someone correct me if I'm wrong,


You're wrong.
but IIRC
Recall from where?
it's OK to fall through
without returning as long as you don't check the return value.


Not true. Where did you hear that? A function *always*
returns at the closing brace ( '}' ), regardless of whether
or not it returns a value. This is a fundamental property
of functions. There's no such thing as 'fall through'.
Whether or not the caller inspects or stores the return value
is irrelevant.

If you heard what you're telling us from a book, burn it.
If from an individual, shoot him. :-)

-Mike
Nov 13 '05 #14

"Thomas Stegen" <ts*****@cis.st rath.ac.uk> wrote in message
news:3f******@n ntphost.cis.str ath.ac.uk...

"Ian Tuomi" <ia*@co.jyu.f i> wrote in message
news:bk******** **@phys-news1.kolumbus. fi...

Well, now you know the language rules after reading
all those posts :)

Anyways...

Just one more "reason". Even if it wasn't so that functions
did default to int it is nice to have void for symmetry and it
makes it easier to write simple parsing tools so they can use
simpler heuristics.

IMO it was a mistake to make functions default to int,
albeit a small one. More people seem to agree as it is now
a deprecated feature.


Not deprecated, but disallowed (by 9899:1999)
9899:1989 is no longer the C standard.

-Mike

Nov 13 '05 #15

"Ian Pilcher" <i.*******@comc ast.net> wrote in message
news:_XHab.5243 04$uu5.87448@sc crnsc04...
Ian Tuomi wrote:

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

I'm not entirely sure that this is even a valid function declaration.


It's valid for C89, but not C99.
If it is, the compiler will assume that foo returns an int.


A C89 compiler, yes. A C99 compiler that doesn't diagnose
it is broken.

-Mike
Nov 13 '05 #16
On Fri, 19 Sep 2003 20:50:55 +0300
Ian Tuomi <ia*@co.jyu.f i> wrote:
Hello, I was wondering, does it make any difference if you write

void foo(int x)
{
/* insert code here */
}
Legal. Returning from this function will choke your compiler.
foo(int x)
{
/* insert code here */
}
Illegal in C99, defaults to int in C89. Not returning here may cause undefined
behaviour.
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?


Blah Blah Blah, it's been said in this thread more than once.

Another important example of a use for void lies in prototypes:

int blog ();
Is a prototype of a function returning an int, with unspecified function
arguments.

int glob (void);
A prototype for a function returning an int, with no function arguments.

--
char*x(c,k,s)ch ar*k,*s;{if(!k) return*s-36?x(0,0,s+1):s ;if(s)if(*s)c=1 0+(c?(x(
c,k,0),x(c,k+=* s-c,s+1),*k):(x(* s,k,s+1),0));el se c=10;printf(&x( ~0,0,k)[c-~-
c+"1"[~c<-c]],c);}main(){x(0 ,"^[kXc6]dn_eaoh$%c","-34*1'.+(,03#;+, )/'///*");}
Nov 13 '05 #17
Pieter Droogendijk <gi*@binky.home unix.org> wrote:
On Fri, 19 Sep 2003 20:50:55 +0300
Ian Tuomi <ia*@co.jyu.f i> wrote:
Hello, I was wondering, does it make any difference if you write

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


Legal. Returning from this function will choke your compiler.


Huh, returning from a void function does harm to your compiler???
Well, (in C99) a return statement /with an expression/ shall not
appear in a function whose return type is void, but a return statement
without an expression shall only appear in a function whose return type
is void.

This implies that

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

is fine.

<SNIP>

Regards

Irrwahn
--
The generation of random numbers is too important to be left to chance.
Nov 13 '05 #18
On Fri, 19 Sep 2003 20:16:53 GMT, "Mike Wahler"
<mk******@mkwah ler.net> wrote in comp.lang.c:

"Thomas Stegen" <ts*****@cis.st rath.ac.uk> wrote in message
news:3f******@n ntphost.cis.str ath.ac.uk...

"Ian Tuomi" <ia*@co.jyu.f i> wrote in message
news:bk******** **@phys-news1.kolumbus. fi...

Well, now you know the language rules after reading
all those posts :)

Anyways...

Just one more "reason". Even if it wasn't so that functions
did default to int it is nice to have void for symmetry and it
makes it easier to write simple parsing tools so they can use
simpler heuristics.

IMO it was a mistake to make functions default to int,
albeit a small one. More people seem to agree as it is now
a deprecated feature.


Not deprecated, but disallowed (by 9899:1999)
9899:1989 is no longer the C standard.


....and never was, but 9899:1990 was.

--
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.l earn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq
Nov 13 '05 #19
On Fri, 19 Sep 2003, Mike Wahler wrote:
Someone correct me if I'm wrong, You're wrong.


Be more specific.

but IIRC

Recall from where?


Probably this very group or comp.std.c couple of months ago.

it's OK to fall through
without returning as long as you don't check the return value.


Oh, sorry. Translation:

...it's OK to return implicitly by letting the execution reach the
last closing brace of the function rather than with explicit return
statement with a proper value even for non-void functions as long
as you don't check the return value.

Not true. Where did you hear that? A function *always*
returns at the closing brace ( '}' ), regardless of whether
or not it returns a value. This is a fundamental property
of functions. There's no such thing as 'fall through'.
Duh. I was talking about /return/ing rather than returning ;)

Whether or not the caller inspects or stores the return value
is irrelevant.
"If the } that terminates a function is reached, and the value of the
function call is used by the caller, the behavior is undefined."

If you heard what you're telling us from a book, burn it.
Luckily I have only a draft. Otherwise this would get on my wallet.

If from an individual, shoot him. :-)


Can I quote that in court?

Nov 13 '05 #20

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

Similar topics

21
2256
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
2198
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
28987
by: nick | last post by:
i do not know what is the use of (e.g. void *pt), when will use it. thanks!
16
10411
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
6540
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
4422
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
2760
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
2015
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
1938
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
9816
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
9668
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
10840
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
10546
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
9371
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
7790
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
5823
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4448
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
3
3112
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.