C and C++ Functions Without a Return Statement

Function returns value without return statement

For x86 at least, the return value of this function should be in eax register. Anything that was there will be considered to be the return value by the caller.

Because eax is used as return register, it is often used as "scratch" register by callee, because it does not need to be preserved. This means that it's very possible that it will be used as any of local variables. Because both of them are equal at the end, it's more probable that the correct value will be left in eax.

C and C++ functions without a return statement

"Why is this still allowed?" It is not, but in general, the compiler cannot prove you are doing it. Consider this (of course extremely simplified) example:

// Input is always true because logic reason
int fun (bool b) {
if (b) {
return 7;
}
}

Or even this one:

int fun (bool b) {
if (b) {
return 7;
}
// Defined in a different translation unit, will always call exit()
foo();
// Now we can never get here, but the compiler cannot know
}

Now the first example could flow off the end, but it never will as long as the function is used "properly"; and the second one could not, but the compiler cannot know this. So the compiler would break "working" and legal, although probably stupid, code by making this an error.

Now the example you posted is a little different: Here, all paths flow off the end, so the compiler could reject or just ignore this function. It would however break real world code that relies on the compiler specific behavior as in your production code, and people do not like that, even if they are wrong.

But in the end, flowing off the end of a non-void function still is undefined behavior. It might work on certain compilers, but it is not and never was guaranteed to.

What does a non void C function without return statement actually return?

  1. what does a non void function without any return statement actually return ? Could it be the last allocated variable on the stack ?

The C standard does not define the behavior if a function does not execute return with a value and the return value of the function is used.

(There is some evidence that, in some situations, GCC deliberately returns the value of the last full expression evaluated in the function, but that was discussed in comments some time ago, and I do not have a reference.)

GCC raises a warning for that , but not an error. Wouldn't a compiler error be actually more pertinent for such a case instead of a compiler warning ?

The C standard does not prohibit a function with a non-void return type from returning without returning a value (by allowing program control to flow to the terminating } of the function definition). So a conforming compiler should allow this in the function definition, meaning it can warn but should not produce an error. It is allowed to call such a function and not use its return value; that has behavior defined by the C standard.

If the compiler can see that a function call uses the return value of a function that does not return a value, the compiler could produce an error for that.

It is occasionally useful for a function with a non-void return type to return a value in some situations and not in others, such as a function that accepts a command to perform and returns a value when a “Get value of setting” command is performed and does not return a value when a “Set value of setting” command is performed.

Why does the C++ function without a return statement return a value?

Not returning from a non-void function is undefined behavior. This means anything can happen. One of those things is actually returning a value.

However, this is not behavior you can rely upon. Crashing the program, printing garbage, etc, are all valid behaviors. Turn on compiler warnings, they will tell you that you are making a mistake.

Why does a main function without a return statement return value 12?

As swegi says, it's undefined behavior. As Steve Jessop et al say, it's an unspecified value until C89, and specified in C99 (the observed behavior is non-conformant to C99)

What actually happens in most environments is that the return value from the last printf is left in the register used for return values.

So it'll be 11 for n == 0, 12 if n is one digit, 14 for two digit n, 16 for three digit n, etc.

C++ return value without return statement

Strictly, this causes undefined behavior. In practice, since sqr has return type int, it will always return something, even if no return statement is present. That something can be any int value.

Add a return statement and turn on warnings in your compiler (g++ -Wall, for instance).

int sqr(int &x)
{
return x = x*x;
}

Checking return value of a function without return statement

Basically what gets returned is dumb luck. You get what happens to be in the CPU register when it comes back. If, for example, the returned value would be in AX, and the char* happens to be in AX, you lucked out. I believe this is an undefined behavior; i.e. the C language specifications don't tell what you should so, so it is left to the compiler. I'm surprised a modern compiler wouldn't at least throw a warning at you.



Related Topics



Leave a reply



Submit