Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > getting return value from function without return statement.

Reply
Thread Tools

getting return value from function without return statement.

 
 
Seong-Kook Shin
Guest
Posts: n/a
 
      06-18-2004
Hi.

Just curiocity,

Because of pre-ANSI C, it is possible to have a function without specifying
return type of a function (which makes the return type 'int', though) and
give no 'return' statement. For example:

foo(int a, int b)
{
int sum;
sum = a + b;
}

void
bar(void)
{
int i = foo(1, 2);
...
}

In above code, the 'i' variable in bar() will have no useful value.
But what does the standard says? I overlooked the ISO C standard, but
found no mention about this problem.

Is this a kind of 'undefined bebavior'? Or 'unspecified behavior'?

If there's wrong point on my explanation, please enlighten me.
Or, could you give me the section number of ISO C document where it deals
with such case?
 
Reply With Quote
 
 
 
 
Richard Bos
Guest
Posts: n/a
 
      06-18-2004
(Seong-Kook Shin) wrote:

> foo(int a, int b)
> {
> int sum;
> sum = a + b;
> }
>
> void
> bar(void)
> {
> int i = foo(1, 2);
> ...
> }
>
> In above code, the 'i' variable in bar() will have no useful value.
> But what does the standard says?


That it invokes undefined behaviour (which is worse than having no
useful value; it could crash, for example):

# 12 If the } that terminates a function is reached, and the value of
# the function call is used by the caller, the behavior is undefined.

That's from 6.9.1.

Richard
 
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
Getting ID, calling url, search for value, return value Tim Fröglich ASP .Net Web Services 1 01-10-2006 09:18 PM
A value returned to a function without a `return' statement, why? lovecreatesbeauty C Programming 8 03-15-2005 08:00 PM
A value returned to a function without a `return' statement? lovecreatesbeauty C Programming 0 03-14-2005 05:38 AM
what value does lack of return or empty "return;" return Greenhorn C Programming 15 03-06-2005 08:19 PM
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



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57