Q : What is returned by "return;" 
Author Message
 Q : What is returned by "return;"

Quote:

> Hi,

> I have a simple question :

> What is returned by the following function when
> the argument it not 1 ?

> int foo(int a)
> {
>   if (a == 1)
>     return 1;
> }
> The compiler gives a warning, but I have no idea of
> the returned value if I call

Good for your compiler.
You need a second return statement, rather than just allowing the
function to fall through to the closing brace.

int foo( int a ) {
   if( a!=1 ) return 5;  /* whatever */
   return a;

Quote:
}

or
int foo( int a ) {
   if( a==1 ) return 1;
   return 0;   /* whatever */

Quote:
}
>   b = foo(0);

Using your definition of foo(), the return value is undefined whenever
(a!=1).  Basically, the program will 'return' whatever happens to be in
the memory area or register to be returned.  This could be anything.

Quote:
> Where is it taken ? On the stack ?

That is implementation specific, you would have to ask your compiler
publisher for tht information.

Quote:
> Thanks in advance.

You're welcome.

--
*********************************************************

* Supporter of the campaign against grumpiness on c.l.c *
*********************************************************
*      Children,                                        *
*      We spend two years teaching them to talk...      *
*      and 20 years trying to get them to shut up!      *
*********************************************************
The money spent on an {*filter*},
as any woman may find out,
can better be spent on other things,
like taking the kids to an amusment park, or on vacation.



Mon, 31 Jan 2000 03:00:00 GMT  
 Q : What is returned by "return;"

Hi,

I have a simple question :

What is returned by the following function when
the argument it not 1 ?

int foo(int a)
{
  if (a == 1)
    return 1;

Quote:
}

The compiler gives a warning, but I have no idea of
the returned value if I call

  b = foo(0);

Where is it taken ? On the stack ?

Thanks in advance.

Vincent

--------------------------------------------------
Vincent HUE              SDH/ATM Software Engineer
MET Commutation, 19 av Carnot 91348 MASSY / France
Tel.: +33-1 64 47 51 30  - Fax : +33-1 64 47 57 45



Mon, 31 Jan 2000 03:00:00 GMT  
 Q : What is returned by "return;"

Quote:

>Hi,

>I have a simple question :

>What is returned by the following function when
>the argument it not 1 ?

>int foo(int a)
>{
>  if (a == 1)
>    return 1;
>}

If a is not 1 then foo has an indeterminate return value. A valid program
cannot use that value, if it does it results in undefined behaviour which
means anything can happen such as the program crashing.

Quote:
>The compiler gives a warning, but I have no idea of
>the returned value if I call

There isn't a meaningful return value.

Quote:
>  b = foo(0);

>Where is it taken ? On the stack ?

The C language doesn't require the existence of a stack. How return values
are passed around is up to the specific compiler. Some might implement a
stack and pass it on that, others may use registers, and so on.

--
-----------------------------------------


-----------------------------------------



Mon, 31 Jan 2000 03:00:00 GMT  
 Q : What is returned by "return;"

[posted and mailed]

Quote:

> What is returned by the following function when
> the argument it not 1 ?
>    int foo(int a)
>    {
>      if (a == 1)
>        return 1;
>    }

I don't know.

Quote:
> The compiler gives a warning...

The compiler doesn't know either; that's why it gives you a
warning.

Quote:
> but I have no idea of the returned value if I call
>   b = foo(0);

In fact, *nobody* has an idea of the returned value!

Quote:
> Where is it taken ? On the stack ?

Some machines return values on the stack; others return them in
designated registers.  Wherever it is that the return value is
returned, your function foo() won't write any value to this
location if a is not equal to 1, so code that looks at the value
(e.g. the assignment b=foo(0)) will probably find whatever random
value happens to be there.

Since there's no defined behavior here, your compiler is
helpfully warning you, and if you'd like your program to have
well-defined behavior, you'll of course want to heed the warning,
and not write code like function foo(), which sometimes fails to
return a value although the caller expects it.

                                        Steve Summit



Mon, 31 Jan 2000 03:00:00 GMT  
 Q : What is returned by "return;"

Quote:

> I have a simple question :

> What is returned by the following function when
> the argument it not 1 ?

> int foo(int a)
> {
>   if (a == 1)
>     return 1;
> }
> The compiler gives a warning, but I have no idea of
> the returned value if I call

>   b = foo(0);

That's it exactly - you have no idea.  The value chosen is basically
unpredictable.  It might be on the stack, it might be the contents of
a given register, it might be the current temperature.  Calling the
function may even cause the program to crash horribly.  You can't rely
on *anything* about the result of calling the function.

--

            http://www.geocities.com/SiliconValley/Lakes/7537/



Tue, 01 Feb 2000 03:00:00 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. RegLoadKey returns "file not found".

2. Function without a "return type"?

3. Comparisons with value returned from "signal".

4. syslog() returns "bad file number"

5. a question on "return"

6. Q: Return-values of system("...") calls

7. When to "return"

8. CDaoRecordset::GetFieldValue returns {"?"}

9. lint warning "function actually returns double"

10. Q: "return" in a multistatement macro

11. Return "newed" variable

12. WinSock function "connect" returns unknown error

 

 
Powered by phpBB® Forum Software