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
Prevent Direct Access to a PHP Include File
How to Solve PHP Error 'Notice: Array to String Conversion In...'
Convert Multidimensional Array into Single Array
How to Access PHP Variables in JavaScript or Jquery Rather Than ≪PHP Echo $Variable ≫
How to Get the Cookies from a PHP Curl into a Variable
How to Create Websockets Server in PHP
Laravel Csrf Token Mismatch For Ajax Post Request
Access Array Returned by a Function in PHP
Allow PHP Sessions to Carry Over to Subdomains
PHP Elvis Operator VS Null Coalescing Operator
How to Get a Hex Dump of a String in PHP
Giving My Function Access to Outside Variable
Passing Arrays as Url Parameter
Return Single Column from a Multi-Dimensional Array
Create a CSV File For a User in PHP
Difference Between Require, Include, Require_Once and Include_Once
Doctrine2: Best Way to Handle Many-to-many With Extra Columns in Reference Table