Multiple Returns from a Function

Multiple returns from a function

There is no way of returning 2 variables. Although, you can propagate an array and return it; create a conditional to return a dynamic variable, etc.

For instance, this function would return $var2

function wtf($blahblah = true) {
$var1 = "ONe";
$var2 = "tWo";

if($blahblah === true) {
return $var2;
}
return $var1;
}

In application:

echo wtf();
//would echo: tWo
echo wtf("not true, this is false");
//would echo: ONe

If you wanted them both, you could modify the function a bit

function wtf($blahblah = true) {
$var1 = "ONe";
$var2 = "tWo";

if($blahblah === true) {
return $var2;
}

if($blahblah == "both") {
return array($var1, $var2);
}

return $var1;
}

echo wtf("both")[0]
//would echo: ONe
echo wtf("both")[1]
//would echo: tWo

list($first, $second) = wtf("both")
// value of $first would be $var1, value of $second would be $var2

Should a function have only one return statement?

I often have several statements at the start of a method to return for "easy" situations. For example, this:

public void DoStuff(Foo foo)
{
if (foo != null)
{
...
}
}

... can be made more readable (IMHO) like this:

public void DoStuff(Foo foo)
{
if (foo == null) return;

...
}

So yes, I think it's fine to have multiple "exit points" from a function/method.

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.

Handle multiple returns from a function in Python

Technically, every function returns exactly one value; that value, however, can be a tuple, a list, or some other type that contains multiple values.

That said, you can return something that uses something other than just the order of values to distinguish them. You can return a dict:

def testFunction(...):
...
return dict(diff1=..., diff2=..., sameCount=..., venn=...)

x = testFunction(...)
print(x['diff1'])

or you can define a named tuple:

ReturnType = collections.namedtuple('ReturnType', 'diff1 diff2 sameCount venn')


def testFunction(...):
...
return ReturnType(diff1=..., diff2=..., sameCount=..., venn=...)

x = testFunction(...)
print(x.diff1) # or x[0], if you still want to use the index

Multiple return values, just one 'if' checking functions return

When you return multiple values, you're actually returning a tuple containing each of those values.

Your if test will return True regardless of the values (even if they are both None)

How to return multiple values ​with a function?

You can return an array from the first function and pass each element to another other function as independent argument using Spread Operator.

var resultado;function num_2(repet){  return [...Array(repet)].map((x,i) => i)}function sumarr(a,b,c){ return a + b + c}console.log("\n" + "callback 2: " + sumarr(...num_2(3)))

Return 1 value to the function with multiple return values in golang

Use the zero value for the other return parameters:

func myFunc() (int, int){
return 0, 3
}

If you use named result parameters, you may also do:

func myFunc() (x, y int){
y = 3
return
}

In this case x will also be the zero value of its type, 0 in case of int.

You could also write a helper function which adds a dummy return value, e.g.:

func myFunc() (int, int) {
return extend(3)
}

func extend(i int) (int, int) {
return 0, i
}

But personally I don't think it's worth it. Just return the zero value for "unused" return parameters.



Related Topics



Leave a reply



Submit