In JavaScript, is there any mechanism that would make it possible to set a function return value without using the keyword "return"?
If no return statement appears in a function definition, control automatically returns to the calling function after the last statement of the called function is executed. In this case, the return value of the called function is undefined.
To put it simply a continuation is a function which is used in place of a return statement. More formally: A continuation is a function which is called by another function. The last thing the other function does is call the continuation.
If no return value is specified, JavaScript will return undefined . In many languages the result of the last statement is returned (more useful, IMO). These are called implicit returns.
Of course you can. All the other answers talk about:
There is however a third method and this method is very powerful: CONTINUATIONS.
For a moment let's assume that return
is a function. For example:
function add(a, b) {
return(a + b);
}
Now what if you didn't want to use return
? We could do something like this:
function add(a, b, ret) {
ret(a);
}
To put it simply a continuation is a function which is used in place of a return statement. More formally:
A continuation is a function which is called by another function. The last thing the other function does is call the continuation. It doesn't do anything else after that.
Hence a continuation is like the functional equivalent of the return
statement.
Continuations have a lot of advantages:
throw
continuation).However continuations have two main disadvantages:
For example:
function fib(n, k) {
switch (n) {
case 0: k(0); break;
case 1: k(1); break;
default:
fib(n - 1, function (x) {
fib(n - 2, function (y) {
k(x + y);
});
});
}
}
fib(20, console.log); // maximum recursion depth exceeded
You can use setTimeout
to eliminate tail call recursion:
Function.prototype.async = function () {
return setTimeout.bind(null, this, 0).apply(null, arguments);
};
function fib(n, k) {
switch (n) {
case 0: k(0); break;
case 1: k(1); break;
default:
fib.async(n - 1, function (x) {
fib.async(n - 2, function (y) {
k(x + y);
});
});
}
}
fib(20, console.log); // 6765
You can use backcalls from LiveScript to make your code look better:
Function::async = -> setTimeout.bind(null, @, 0) ...
fib = (n, k) -->
| n == 0 => k 0
| n == 1 => k 1
| otherwise =>
x <- fib.async n - 1
y <- fib.async n - 2
k x + y
fib 20, alert # 6765
You can try it out for yourself on the http://livescript.net/ site. Just use a value smaller than 20
. Preferably 10
.
There is no other way to return a value, that's what return
is for.
But since we have side effects, you can edit a variable in a wider scope like this:
var a = 5;
function setA(x)
{
a = x;
}
setA(6);
Alternatively you can pass an object to the function and by using pass by reference change variables in that object. This page details how that works.
As mentioned above, this is not the same as a result value, as the following will fail:
var newA = setA(a); // undefined instead of 5
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With