How to Assign from a Function Which Returns More Than One Value

How to assign from a function which returns more than one value?

(1) list[...]<- I had posted this over a decade ago on r-help. Since then it has been added to the gsubfn package. It does not require a special operator but does require that the left hand side be written using list[...] like this:

library(gsubfn)  # need 0.7-0 or later
list[a, b] <- functionReturningTwoValues()

If you only need the first or second component these all work too:

list[a] <- functionReturningTwoValues()
list[a, ] <- functionReturningTwoValues()
list[, b] <- functionReturningTwoValues()

(Of course, if you only needed one value then functionReturningTwoValues()[[1]] or functionReturningTwoValues()[[2]] would be sufficient.)

See the cited r-help thread for more examples.

(2) with If the intent is merely to combine the multiple values subsequently and the return values are named then a simple alternative is to use with :

myfun <- function() list(a = 1, b = 2)

list[a, b] <- myfun()
a + b

# same
with(myfun(), a + b)

(3) attach Another alternative is attach:

attach(myfun())
a + b

ADDED: with and attach

Return multiple values from function

Dart doesn't support multiple return values.

You can return an array,

List foo() {
return [42, "foobar"];
}

or if you want the values be typed use a Tuple class like the package https://pub.dartlang.org/packages/tuple provides.

See also either for a way to return a value or an error.

Return multiple values from a function

In R you can only return one object. You could put the three values in a list or a vector and then return that list or vector.

Returning multiple objects in an R function

Unlike many other languages, R functions don't return multiple objects in the strict sense. The most general way to handle this is to return a list object. So if you have an integer foo and a vector of strings bar in your function, you could create a list that combines these items:

foo <- 12
bar <- c("a", "b", "e")
newList <- list("integer" = foo, "names" = bar)

Then return this list.

After calling your function, you can then access each of these with newList$integer or newList$names.

Other object types might work better for various purposes, but the list object is a good way to get started.

how to return multiple values from a function and assign it to a array?

Since it seems you want your result as an array, why not return an array?

func aFunction(value: Int) -> [Int] {
return [ value * 2, value * 4, value * 8 ]
}

let theArray = aFunction(value: 2)

JavaScript: How do I return two values from a function and call those two variables in another function?

You can't return two values like this:

return (num1, num2);

Javascript doesn't work that way. If you're trying to do a Python-like tuple, Javascript doesn't have a data structure with that syntax.

You can put two values in an array and return the array or two values in an object and return the object.

And, then when you call the function, you have to assign the result to something in order to use the returned result.

Here's one way returning an array:

function getValues(){
var num1 = document.getElementById("firstNum").value;
var num2 = document.getElementById("secondNum").value;
// return array of two values
return [num1, num2];
}

function add(){
// get values and assign the returned array to a local variable named values
var values = getValues();
return parseFloat(values[0]) + parseFloat(values[1]);
}

Or, you could put both values into an object with named properties:

function getValues(){
var num1 = document.getElementById("firstNum").value;
var num2 = document.getElementById("secondNum").value;
// return array of two values
return {firstNum: num1, secondNum: num2};
}

function add(){
// get values and assign the returned array to a local variable named values
var values = getValues();
return parseFloat(values.firstNum) + parseFloat(values.secondNum);
}

The object syntax is more descriptive and verbose because each property has a relevant name rather than just a numeric index, but it's also less compact. In this particular case, you could use either the object or the array.

ES6 Destructuring Update

When returning two values in an array or an object, you can also use ES6 syntax to shorten the code to do that. For example, when returning an object, you can use the shorthand syntax that assigns the property name as the same name as the variable as in:

return {num1, num2};

This actually returns an object that is like this:

{ num1: num1, num2: num2 }

And, then when you call that function, you can use destructuring to assign both values to variables:

function getValues(){
var num1 = document.getElementById("firstNum").value;
var num2 = document.getElementById("secondNum").value;
// return array of two values
return {num1, num2};
}

let {num1, num2} = getValues();
console.log(num1);
console.log(num2);

Or, you can use destructuring similarly with an array:

function getValues(){
var num1 = document.getElementById("firstNum").value;
var num2 = document.getElementById("secondNum").value;
// return array of two values
return [num1, num2];
}

let [num1, num2] = getValues();
console.log(num1);
console.log(num2);

How to access multiple values from a function while only calling the function one time?

Yes.
Assign the return value of the function to a variable.
Then access the variable as many times as you need to -- the function need not be called again (unless it is a function which will give you a different return value when called again -- such as getting the current time. But you wouldn't be asking the question if you were doing something like that.)



Related Topics



Leave a reply



Submit