How to Return Multiple Values

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 in JavaScript?

No, but you could return an array containing your values:

function getValues() {
return [getFirstValue(), getSecondValue()];
}

Then you can access them like so:

var values = getValues();
var first = values[0];
var second = values[1];

With the latest ECMAScript 6 syntax*, you can also destructure the return value more intuitively:

const [first, second] = getValues();

If you want to put "labels" on each of the returned values (easier to maintain), you can return an object:

function getValues() {
return {
first: getFirstValue(),
second: getSecondValue(),
};
}

And to access them:

var values = getValues();
var first = values.first;
var second = values.second;

Or with ES6 syntax:

const {first, second} = getValues();

* See this table for browser compatibility. Basically, all modern browsers aside from IE support this syntax, but you can compile ES6 code down to IE-compatible JavaScript at build time with tools like Babel.

How to return multiple values without it being cast as an array?

You could use About Splatting. I was not able to get the function work as you wished. The following example does what you wish with one line more code. Maybe someone else knows another way.

function Return-Values{
return "One", "Two", "Three"
}

[System.Array]$InputArray = Return-Values #Get the input values as an array

function Print-Args{
param($One,$Two,$Three)
Write-Host "1" $One
Write-Host "2" $Two
Write-Host "3" $Three
}

Print-Args @InputArray #Use splatting for the input parameters

How can I return multiple values without using arrays or pointers or structures?

If you want multiple values but not at once, you can just generalize both operations and provide base to which a number should be converted:

unsigned long Dec2AnotherBase(unsigned long n, int base){
unsigned long place=1, result=0;
for(; n; place*=10, n/=base)
result += n % base * place;
return result;
}

In your main function you replace your loops with those:

bin=Dec2AnotherBase(n, 2);
o=Dec2AnotherBase(n, 8);

I also simplified your code a little. Here's what I did:

  • Replaced bin=bin+(r*place) and place=place*10 with shorter bin+=r*place and place*=10.
  • Eliminated r variable because it's only used once (also no brackets around % are needed as % operator is the same precedence as * and evaluated left-to-right).
  • Moved g=g/2 inside increment statement using , operator.
  • Removed curly brackets which were no longer required in a loop reduced to a single statement.
  • Removed g since it is created unnecessarily (you can operate directly on n as it is already a copy of a variable passed to the function).
  • Contentious: Replaced n!=0 in condition with n as every non-zero value is implicitly converted to logical true.

How do you properly return multiple values from a Promise?

You can't resolve a promise with multiple properties just like you can't return multiple values from a function. A promise conceptually represents a value over time so while you can represent composite values you can't put multiple values in a promise.

A promise inherently resolves with a single value - this is part of how Q works, how the Promises/A+ spec works and how the abstraction works.

The closest you can get is use Q.spread and return arrays or use ES6 destructuring if it's supported or you're willing to use a transpilation tool like BabelJS.

As for passing context down a promise chain please refer to Bergi's excellent canonical on that.



Related Topics



Leave a reply



Submit