What use is void?

K

Kevin Bracey

In message <[email protected]>
"Arthur J. O'Dwyer said:
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.
 
A

Arthur J. O'Dwyer

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
 
K

Kevin Bracey

In message <[email protected]>
"Arthur J. O'Dwyer said:
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.
 
D

Darrell Grainger

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[]=":mad:abcdefghijklmnopqrstuvwxyz.\n",*i=
"iqgbgxmdbjlgdv.lksrqek.n";char *strchr(const char *,int);while(
*i){j+=strchr(t,*i++)-t;j%=sizeof t-1;putchar(t[j]);} return 0;}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
474,079
Messages
2,570,575
Members
47,207
Latest member
HelenaCani

Latest Threads

Top