This language bar is your friend. Select your favorite languages!

Idiom #126 Multiple return values

Write a function foo that returns a string and a boolean value.

using System;
public Tuple<string, bool> foo_PreCSharp7()
{
    // Only accessed via .Item1 and .Item2
    return new Tuple<string, bool>("string", true);
}

public (string, bool) foo_CSharp7UnnamedTuples()
{
    // Only accessed via .Item1 and .Item2
    return ("string", true);
}

public (string NamedStringArg, bool NamedBooleanArg) foo_CSharp7()
{
    // Can be accessed via .NamedStringArg or .NamedBooleanArg
    return ("string", true);
}
import std.typecons;
auto foo()
{
    return tuple("theString", true);
}
def foo, do: {"bar", true}
subroutine foo(c, b)
  character(len=:), allocatable, intent(out) :: c
  logical, intent(out) :: b
  c = 'string'
  b = .false.
end subroutine foo
func foo() (string, bool) {
	return "Too good to be", true
}
foo :: (String, Bool)   -- optional signature
foo = ("String", True)
const foo = () => ({string: 'string', bool: true})
static Object[] returnAnything() {
    return new Object[]{"a string", true};
}

public static void main (String[] args) {
    Object[] array = returnAnything();
    System.out.println(array[0]);
    System.out.println(array[1]);
}
function foo()
	return "bar", false
end
<?php
function foo() {
    return ['string', FALSE];
}

list($string, $boolean) = foo();
type
  TFooRec = record
    S: String;
    B: Boolean;
  end;

function Foo: TFooRec;
begin
  Result.S := 'Some string';
  Result.B := False;
end;
sub foo {
    return "bar", 1;
}
def foo():
    return 'string', True
def foo
  string, boolean  = "bar", false
  [string, boolean]
end
fn foo() -> (String, bool) {
    (String::from("bar"), true)
}
def foo(): (String, Boolean) = ("bar", true)
(define (foo) (cons "bar" false))

Do you know the best way to do this in your language ?
New implementation...

Idiom created by

MLKo

Related idioms