What Happens When a Function That Returns an Object Ends Without a Return Statement

What happens when a function that returns an object ends without a return statement

What gets returned?

We don't know. According to the standard, the behavior is undefined.

§6.6.3/2 The return statement
[stmt.return]:

(emphasis mine)

Flowing off the end of a constructor, a destructor, or a function with a cv void return type is equivalent to a return with no operand. Otherwise, flowing off the end of a function other than main (basic.start.main) results in undefined behavior.

In fact most compilers would give a warning for it, like Clang:

warning: control reaches end of non-void function [-Wreturn-type]

What does javascript function return in the absence of a return statement?

A function without a return statement (or one that ends its execution without hitting one) will return undefined.

And if you use the unary negation operator twice on an undefined value, you will get false.

You are not seeing anything on the console because Firebug doesn't prints the result of an expression when it's undefined (just try typing undefined; at the console, and you will see nothing).

However if you call the console.log function directly, and you will be able to see it:

function foo(){}

console.log(foo()); // will show 'undefined'

In python, if a function doesn't have a return statement, what does it return?

If a function doesn't specify a return value, it returns None.

In an if/then conditional statement, None evaluates to False. So in theory you could check the return value of this function for success/failure. I say "in theory" because for the code in this question, the function does not catch or handle exceptions and may require additional hardening.

return, return None, and no return at all?

On the actual behavior, there is no difference. They all return None and that's it. However, there is a time and place for all of these.
The following instructions are basically how the different methods should be used (or at least how I was taught they should be used), but they are not absolute rules so you can mix them up if you feel necessary to.

Using return None

This tells that the function is indeed meant to return a value for later use, and in this case it returns None. This value None can then be used elsewhere. return None is never used if there are no other possible return values from the function.

In the following example, we return person's mother if the person given is a human. If it's not a human, we return None since the person doesn't have a mother (let's suppose it's not an animal or something).

def get_mother(person):
if is_human(person):
return person.mother
else:
return None

Using return

This is used for the same reason as break in loops. The return value doesn't matter and you only want to exit the whole function. It's extremely useful in some places, even though you don't need it that often.

We've got 15 prisoners and we know one of them has a knife. We loop through each prisoner one by one to check if they have a knife. If we hit the person with a knife, we can just exit the function because we know there's only one knife and no reason the check rest of the prisoners. If we don't find the prisoner with a knife, we raise an alert. This could be done in many different ways and using return is probably not even the best way, but it's just an example to show how to use return for exiting a function.

def find_prisoner_with_knife(prisoners):
for prisoner in prisoners:
if "knife" in prisoner.items:
prisoner.move_to_inquisition()
return # no need to check rest of the prisoners nor raise an alert
raise_alert()

Note: You should never do var = find_prisoner_with_knife(), since the return value is not meant to be caught.

Using no return at all

This will also return None, but that value is not meant to be used or caught. It simply means that the function ended successfully. It's basically the same as return in void functions in languages such as C++ or Java.

In the following example, we set person's mother's name and then the function exits after completing successfully.

def set_mother(person, mother):
if is_human(person):
person.mother = mother

Note: You should never do var = set_mother(my_person, my_mother), since the return value is not meant to be caught.

Returning a value without an explicit return statement

The definitive material on this subject is the latest ES Harmony specification draft, and specifically the part derived from the arrow function syntax proposal. For convenience, an unofficial HTML version can be found here.

In short, this new syntax will allow the definition of functions much more concisely. The ES spec draft has all the detail, I 'll explain very roughly here.

The syntax is

ArrowParameters => ConciseBody

The ArrowParameters part defines the arguments that the function takes, for example:

()                   // no arguments
arg // single argument (special convenience syntax)
(arg) // single argument
(arg1, arg2, argN) // multiple arguments

The ConciseBody part defines the body of the function. This can be defined either as it has always been defined, e.g.

{ alert('Hello!'); return 42; }

or, in the special case where the function returns the result of evaluating a single expression, like this:

theExpression

If this sounds rather abstract, here's a concrete example. All of these function definitions would be identical under the current draft spec:

var inc = function(i) { return i + 1; }
var inc = i => i + 1;
var inc = (i) => i + 1;
var inc = i => { return i + 1; };

As an aside, this new syntax is exactly the same great syntax that C# uses to allow the definition of lambda functions.

Does every Python function have to return at the end?

So I was making a diagram of the recursive function to wrap my head around recursion, and I noticed that apparently every function executes return at the end?

A return itself simply means that the call frame is popped from the call stack, the program counter is set back to state where it was before you made the call, etc. Based on your comment it looks a bit like "backtracking" but mind that - in contrast to backtracking - side effects the functions have, are not reverted/undone. For instance appending to a list, drawing a line, writing to a file, etc. are not reverted. This is the desired behavior in recursion.

Now sometimes it is useful to return a certain value. For instance:

def foo(x,y):
return x+y

here foo does not only go back to the caller, but first evaluates x+y and the result is returned, such that you can state z = foo(2,3). So here foo is called, 2+3 is evaluated and 5 is returned and assigned to z.

Another question, what exactly does a function return? All of parameters passed to it (assuming multiple parameters)?

In Python all functions return a value. If you specify it like:

return <expression>

the <expression> part is evaluated and that value is returned. If you specify:

return

or no return at all (but you reach the end of the function), None will be returned.

Is there a benefit to using a return statement that returns nothing?

Using return without a value will return the value undefined.

If the value is evaluated as a boolean, undefined will work as false, but if the value for example is compared to false, you will get a different behaviour:

var x; // x is undefined
alert(x); // shows "undefined"
alert(!x); // shows "true"
alert(x==false); // shows "false"

So, while the code should logically return true or false, not true or undefined, you can't just change return; to return false; without checking how the return value is used.

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.



Related Topics



Leave a reply



Submit