Logo

Programming-Idioms

Write a function foo that returns a string and a boolean value.
Implementation
C++

Implementation edit is for fixing errors and enhancing with metadata. Please do not replace the code below with a different implementation.

Instead of changing the code of the snippet, consider creating another C++ implementation.

Be concise.

Be useful.

All contributions dictatorially edited by webmasters to match personal tastes.

Please do not paste any copyright violating material.

Please try to avoid dependencies to third-party libraries and frameworks.

Other implementations
import std.typecons;
auto foo()
{
    return tuple("theString", true);
}
sub foo {
    return "bar", 1;
}
def foo
  string, boolean  = "bar", false
  [string, boolean]
end
function foo()
	return "bar", false
end
type
  TFooRec = record
    S: String;
    B: Boolean;
  end;

function Foo: TFooRec;
begin
  Result.S := 'Some string';
  Result.B := False;
end;
func foo() (string, bool) {
	return "Too good to be", true
}
fn foo() -> (String, bool) {
    (String::from("bar"), true)
}
(define (foo) (cons "bar" false))
foo :: (String, Bool)   -- optional signature
foo = ("String", True)
def foo():
    return 'string', True
<?php
function foo() {
    return ['string', FALSE];
}

list($string, $boolean) = foo();
const foo = () => ({string: 'string', bool: true})
def foo(): (String, Boolean) = ("bar", true)
def foo, do: {"bar", true}
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);
}
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]);
}
subroutine foo(c, b)
  character(len=:), allocatable, intent(out) :: c
  logical, intent(out) :: b
  c = 'string'
  b = .false.
end subroutine foo
#include <tuple>
std::tuple<std::string, bool> foo() {
  return std::make_tuple("someString", true);
}
const foo = () => ['string', true];
fun foo() : Pair<String, Boolean> = Pair(5, true)

fun useFoo() {
  val a, b = foo()
}
(define (foo) 
  (values "foo" #t))
foo() => ['a string', true];
#include <stdbool.h>
typedef struct {
    const char * const a;
    const bool b;
} RetStringBool;

RetStringBool foo() {
    return (RetStringBool) {.a = "Hello", .b = true};
}
(defn foo
  []
  ["bar" true])
(defun foo () (format t "bar") t)
private static class Tuple<S, B>
{
	private S s;
	private B b;
	
	public Tuple(S s, B b)
	{
		this.setS(s);
		this.setB(b);
	}
	
	public void setS(S s)
	{
		this.s = s;
	}
	
	public void setB(B b)
	{
		this.b = b;
	}
	
	public S getS()
	{
		return s;
	}
	
	public B getB()
	{
		return b;
	}
}

private static Tuple foo()
{
	return new Tuple<String, Boolean>("value", true);
}