Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > function not returning a value

Reply
Thread Tools

function not returning a value

 
 
happy
Guest
Posts: n/a
 
      01-12-2010
K&R 2nd edition, page no. 26, says that a function need not return a
value; a return statement with no
expression causes control, but no useful value, to be returned to the
caller......

So according to above statement, is the following code correct?

#include<stdio.h>
int fun()
{
return;
}
int main()
{
printf("%d\n",fun());
return 0;
}

Here, although no useful value is returned by fun(), but can main use
that value in printf()? Will it be UB?
 
Reply With Quote
 
 
 
 
Lew Pitcher
Guest
Posts: n/a
 
      01-12-2010
On January 12, 2010 14:12, in comp.lang.c, http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:

> K&R 2nd edition, page no. 26, says that a function need not return a
> value; a return statement with no
> expression causes control, but no useful value, to be returned to the
> caller......
>
> So according to above statement, is the following code correct?


No. You misunderstand the statement in K&R

A function need not return a value. Those functions that do not return a
value must be declared or defined as returning void. Functions that return
void cannot return a value using the return statement.

Functions that return a value must be declared or defined as returning a
value of a specific type. Functions that return a value of a specific type
must (pedants? am I correct here, or is there an implicit return value?)
return the value using the return statement.

>
> #include<stdio.h>
> int fun()
> {
> return;
> }
> int main()
> {
> printf("%d\n",fun());
> return 0;
> }
>
> Here, although no useful value is returned by fun(), but can main use
> that value in printf()? Will it be UB?


IFAIK, yes.

--
Lew Pitcher
Master Codewright & JOAT-in-training | Registered Linux User #112576
Me: http://pitcher.digitalfreehold.ca/ | Just Linux: http://justlinux.ca/
---------- Slackware - Because I know what I'm doing. ------


 
Reply With Quote
 
 
 
 
Ersek, Laszlo
Guest
Posts: n/a
 
      01-12-2010
In article <(E-Mail Removed)>, happy <(E-Mail Removed)> writes:

> Here, although no useful value is returned by fun(), but can main use
> that value in printf()? Will it be UB?


See C89 6.6.6.4 "The return statement":

"A return statement with an expression shall not appear in a function
whose return type is void. [...] A function may have any number of
return statements, with and without expressions. [...] If a return
statement without an expression is executed, and the value of the
function call is used by the caller, the behavior is undefined."

lacos
 
Reply With Quote
 
Antoninus Twink
Guest
Posts: n/a
 
      01-12-2010
On 12 Jan 2010 at 19:39, Lew Pitcher wrote:
> On January 12, 2010 14:12, in comp.lang.c, (E-Mail Removed) wrote:
>> K&R 2nd edition, page no. 26, says that a function need not return a
>> value; a return statement with no expression causes control, but no
>> useful value, to be returned to the caller......

>
> No. You misunderstand the statement in K&R


He understood it perfectly well.

> A function need not return a value.


True. But K&R specifically say: useful value. They are pragmatists,
unlike the fanatics in this group.

> Those functions that do not return a value must be declared or defined
> as returning void. Functions that return void cannot return a value
> using the return statement.


Read on: the next paragraph says "Since main is a function like any
other, it MAY return a value to its caller" (my emphasis).

It is obvious that what they are saying is that failing to return a
value from a non-void function (or falling off the end of the function)
returns garbage to the caller - whatever happens to be in a certain CPU
register, for example. That is the practical answer.

Or there is the "Heathfield answer": undefined behavior, it probably
reformats your hard disk. A useless, bullshit answer.

One of the reasons K&R are widely regarded as brilliant pedagogues is
precisely because they apply common sense and don't bombard newbies with
a hail of standardese.

Listen to them speak: "Automatic variables for which there is no
explicit initializer have undefined (i.e. garbage) values". How many of
the clc "regulars" would tolerate that inexact but highly informative
parenthesis in their quest for literal exactness?

 
Reply With Quote
 
Walter Banks
Guest
Posts: n/a
 
      01-12-2010


"Ersek, Laszlo" wrote:

> In article <(E-Mail Removed)>, happy <(E-Mail Removed)> writes:
>
> > Here, although no useful value is returned by fun(), but can main use
> > that value in printf()? Will it be UB?

>
> See C89 6.6.6.4 "The return statement":
>
> "A return statement with an expression shall not appear in a function
> whose return type is void. [...] A function may have any number of
> return statements, with and without expressions. [...] If a return
> statement without an expression is executed, and the value of the
> function call is used by the caller, the behavior is undefined."


In ISO/IEC 9899:201x the only return without an expression is
when the function type is a void.


6.8.6.4 The return statement

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.

Regards,

--
Walter Banks
Byte Craft Limited
http://www.bytecraft.com



 
Reply With Quote
 
Nick
Guest
Posts: n/a
 
      01-12-2010
Lew Pitcher <(E-Mail Removed)> writes:

> On January 12, 2010 14:12, in comp.lang.c, (E-Mail Removed) wrote:
>
>> K&R 2nd edition, page no. 26, says that a function need not return a
>> value; a return statement with no
>> expression causes control, but no useful value, to be returned to the
>> caller......
>>
>> So according to above statement, is the following code correct?

>
> No. You misunderstand the statement in K&R
>
> A function need not return a value. Those functions that do not return a
> value must be declared or defined as returning void. Functions that return
> void cannot return a value using the return statement.
>
> Functions that return a value must be declared or defined as returning a
> value of a specific type. Functions that return a value of a specific type
> must (pedants? am I correct here, or is there an implicit return value?)
> return the value using the return statement.


A while ago I was thinking about this, and idly wondered about this.
Pure language pedantry: anyone who writes something like this ought to
be shot.

#include <stdio.h>
#include <stdlib.h>

int f(int x) {
if(x)
return 5*x;
printf("Hello\n");
return;
}

int main(void) {
printf("Here is something %d\n",f(10));
f(0);
return EXIT_SUCCESS;
}
--
Online waterways route planner | http://canalplan.eu
Plan trips, see photos, check facilities | http://canalplan.org.uk
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      01-12-2010
Lew Pitcher <(E-Mail Removed)> writes:
> On January 12, 2010 14:12, in comp.lang.c, (E-Mail Removed) wrote:
>
>> K&R 2nd edition, page no. 26, says that a function need not return a
>> value; a return statement with no
>> expression causes control, but no useful value, to be returned to the
>> caller......
>>
>> So according to above statement, is the following code correct?

>
> No. You misunderstand the statement in K&R


I don't think he did. At that point, K&R hadn't introduced void
functions.

In fact, it's legal for a non-void function to fall off the end -- but
if the caller attempts to use the result, the behavior is undefined
(C99 6.9.1p12).

In C99, a return statement with an expression may only appear in a
non-void function, and a return statement without an expression may
only appear in a void function (C99 6.8.6.4p1). In C90, which is what
K&R2 describes, a return statement without an expression ("return;")
may appear legally in a non-void function (C90 6.6.6.4).

> A function need not return a value. Those functions that do not return a
> value must be declared or defined as returning void. Functions that return
> void cannot return a value using the return statement.
>
> Functions that return a value must be declared or defined as returning a
> value of a specific type. Functions that return a value of a specific type
> must (pedants? am I correct here, or is there an implicit return value?)
> return the value using the return statement.


There's only an implicit return value in the special case of main, and
that's only in C99.

>>
>> #include<stdio.h>
>> int fun()
>> {
>> return;
>> }
>> int main()
>> {
>> printf("%d\n",fun());
>> return 0;
>> }
>>
>> Here, although no useful value is returned by fun(), but can main use
>> that value in printf()? Will it be UB?

>
> IFAIK, yes.


Yes, that's undefined behavior in C99 (I'm not sure about C90, but
it's certainly a bad idea).

Remember that pre-ANSI C didn't have void, so the usual way to define
a function that doesn't return a useful value was to leave off the
return type, letting it default to int:

do_something() /* implicitly returns int */
{
/* ... */
}

...

do_something(); /* function returns garbage int value,
which is ignored */

In effect, there's an implicit contract between the caller and the
function: the function doesn't return a result, and the caller doesn't
attempt to use it. In pre-ANSI C, there was no way to express or
enforce this contract. ANSI added "void" for this purpose. C90 and
C99 continue to permit falling off the end of a non-void function to
avoid breaking old code like this.

--
Keith Thompson (The_Other_Keith) (E-Mail Removed) <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
happy
Guest
Posts: n/a
 
      01-12-2010
On Jan 13, 12:45*am, (E-Mail Removed) (Ersek, Laszlo) wrote:
> In article <(E-Mail Removed)..com>, happy <(E-Mail Removed)> writes:
>
> > Here, although no useful value is returned by fun(), but can main use
> > that value in printf()? Will it be UB?

>
> See C89 6.6.6.4 "The return statement":
>
> "A return statement with an expression shall not appear in a function
> whose return type is void. [...] A function may have any number of
> return statements, with and without expressions. [...] If a return
> statement without an expression is executed, and the value of the
> function call is used by the caller, the behavior is undefined."
>
> lacos


Thank you Ersek and Lew.
Ersek, just one more doubt. C89 says as above "If a return
statement without an expression is executed, and the value of the
function call is used by the caller, the behavior is undefined."
How can the caller use the value of function call which has a return
statement without an expression?
I mean if I try as in my first program, it shows compile time error.

If I write as

void fun()
{
return ;
}

int main()
{
printf("%d\n",fun());
return 0;
}

Then also it shows error. So how value of fun() can be used when it
has no expression in return statement (although it is UB)?
 
Reply With Quote
 
Stefan Ram
Guest
Posts: n/a
 
      01-12-2010
Nick <(E-Mail Removed)> writes:
>int f(int x) {
> if(x)
> return 5*x;
> printf("Hello\n");
> return;
>}


int f( int const x ){ return x ? 5 * x : printf( "Hello\n" ); }

 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      01-12-2010
happy <(E-Mail Removed)> writes:
> On Jan 13, 12:45*am, (E-Mail Removed) (Ersek, Laszlo) wrote:
>> In article
>> <(E-Mail Removed)>,
>> happy <(E-Mail Removed)> writes:
>>
>> > Here, although no useful value is returned by fun(), but can main use
>> > that value in printf()? Will it be UB?

>>
>> See C89 6.6.6.4 "The return statement":
>>
>> "A return statement with an expression shall not appear in a function
>> whose return type is void. [...] A function may have any number of
>> return statements, with and without expressions. [...] If a return
>> statement without an expression is executed, and the value of the
>> function call is used by the caller, the behavior is undefined."

>
> Thank you Ersek and Lew.
> Ersek, just one more doubt. C89 says as above "If a return
> statement without an expression is executed, and the value of the
> function call is used by the caller, the behavior is undefined."


It does? I know C99 says that, but I couldn't find the corresponding
text in C90. Can you cite the section number?

> How can the caller use the value of function call which has a return
> statement without an expression?
> I mean if I try as in my first program, it shows compile time error.
>
> If I write as
>
> void fun()
> {
> return ;
> }
>
> int main()
> {
> printf("%d\n",fun());
> return 0;
> }
>
> Then also it shows error. So how value of fun() can be used when it
> has no expression in return statement (although it is UB)?


For example:

#include <stdio.h>

int fun(void)
{
/* no return statement */
}

int main(void)
{
printf("%d\n", fun());
return 0;
}

--
Keith Thompson (The_Other_Keith) (E-Mail Removed) <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
write a function such that when ever i call this function in some other function .it should give me tha data type and value of calling function parameter komal C++ 6 01-25-2005 11:13 AM
Recursive function not returning value Derek Rhodes Python 4 09-17-2004 07:47 AM
'exit function'...always use it when returning a value? darrel ASP .Net 3 08-06-2004 07:02 PM
NaN and function not returning value. J Lake Javascript 5 05-04-2004 06:25 PM
void function returning value? Jochen Zeischka C++ 3 02-25-2004 03:30 PM



Advertisments