What use is void?

I

Ian Tuomi

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!
 
G

Guest

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?

Absolutely not. If you omit a return type from a function declaration, the
return type defaults to `int', so

foo(int x)

is actually

int foo(int x)

The void type is used as the type returned by functions that generate no
value.
 
J

Jens.Toerring

Ian Tuomi said:
Hello, I was wondering, does it make any difference if you write
void foo(int x)
{
/* insert code here */
}

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?

No, at least with the C89 standard a function with no specified
return value (like your second example) is supposed to return an
integer. You need void to explicitely tell the compiler that the
function isn't returning a value. The new (C99) standard changed
this, but not in the sense that a function without a specified
return value returns nothing (aka void), but making it a syntax
error, so you still need void.
Regards, Jens
--
_ _____ _____
| ||_ _||_ _| (e-mail address removed)-berlin.de
_ | | | | | |
| |_| | | | | | http://www.physik.fu-berlin.de/~toerring
\___/ens|_|homs|_|oerring
 
I

Ian Tuomi

Ian said:
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

"Very funny scotty, now beam down my clothes."

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

Ed Morton

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??

The first one returns nothing while the second returns an int.

Both ways the function wont return anything,

No. If you put a "return 1;" inside both functions, the first one won't compile
whereas the second will compile with just a warning about the return type
defaulting to int.

What is the point in writing void? Is it merely a cosmetic feature?
No. It tells the compiler (and future developers!) that this function must not
return anything, so if someone wrongly adds a return(value) statement or wrongly
assgines a void functions return to a variable, they get a compilation error or
warning.

Ed.
 
I

Ian Pilcher

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

I'm not entirely sure that this is even a valid function declaration.
If it is, the compiler will assume that foo returns an int.
 
I

Irrwahn Grausewitz

Ian Tuomi said:
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?

The first example declares a function returning nothing at all, whereas
in the second example the function defaults to return int (at least in
K&R-C and C89). Your compiler will most probably issue a diagnostic if
you fail to supply a return statement in the second example.

Furthermore you need void when you want to declare a generic pointer,
IOW a pointer of which is not known what type of object it points to.
E.g. the malloc function returns a void*.

Regards

Irrwahn
 
M

Mike Wahler

Ian Tuomi said:
Hello, I was wondering, does it make any difference if you write

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

or

/* in C99, insert required return type here */
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.
What is the difference??

See above.
Both ways the function wont return anything,

Wrong. What C book(s) are you reading?
What is the point in writing void?

It specifies that a function does not return a value.
This is the *only* way to specify no return value.
Is it merely a cosmetic feature?

Absolutely not.

-Mike
 
J

Jarno A Wuolijoki

/* 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.
 
X

Xenos

Jarno A Wuolijoki said:
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.
 
T

Thomas Stegen

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.

As mentioned elsethread it is also used to declare generic
pointers. This is perhaps strange (I think so at least). Consider
that a pointer to int can point to objects of type int. You can
in fact have objects of type int so that is ok and intuitive.
When you have a pointer to void you have pointer than can
point to objects of type void. But you cannot have objects
of type void! This is not a big problem though and I find it
an adequate solution over having a separate keyword or
type for this, like gptr_t or something like 'object *p;'.
 
D

Dan Pop

In said:
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
 
P

Pieter Droogendijk

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.
 
A

Arthur J. O'Dwyer

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(char *buffer, const char *fmt, ...)
{
if (buffer == NULL) {
how_many_characters = some_function_of_(input);
return how_many_characters;
}
else {
strcpy(buffer, some_other_function_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(non_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(int 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
 
M

Mike Wahler

Jarno A Wuolijoki said:
Someone correct me if I'm wrong,

You're wrong.

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
 
M

Mike Wahler

Thomas Stegen said:
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
 
M

Mike Wahler

Ian Pilcher said:
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
 
I

Irrwahn Grausewitz

Pieter Droogendijk said:
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
 
J

Jarno A Wuolijoki

You're wrong.

Be more specific.

Recall from where?

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


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?
 

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
473,995
Messages
2,570,233
Members
46,820
Latest member
GilbertoA5

Latest Threads

Top