Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Return multiple values from function

Is there a way to return several values in a function return statement (other than returning an object) like we can do in Go (or some other languages)?

For example, in Go we can do:

func vals() (int, int) {
    return 3, 7
}

Can this be done in Dart? Something like this:

int, String foo() {
    return 42, "foobar";
} 
like image 444
Alexi Coard Avatar asked Jul 26 '17 11:07

Alexi Coard


People also ask

Can we return multiple values from a function?

We can return more than one values from a function by using the method called “call by address”, or “call by reference”. In the invoker function, we will use two variables to store the results, and the function will take pointer type data.

Can I return multiple values from a function in C?

We know that the syntax of functions in C doesn't allow us to return multiple values.

Can I return multiple values from a function in Python?

Python functions can return multiple values. These values can be stored in variables directly. A function is not restricted to return a variable, it can return zero, one, two or more values.

What are the two ways of returning multiple values from function?

Below are the methods to return multiple values from a function in C: By using pointers. By using structures. By using Arrays.


4 Answers

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.

like image 65
Günter Zöchbauer Avatar answered Oct 23 '22 02:10

Günter Zöchbauer


I'd like to add that one of the main use-cases for multiple return values in Go is error handling which Dart handle's in its own way with Exceptions and failed promises.

Of course this leaves a few other use-cases, so let's see how code looks when using explicit tuples:

import 'package:tuple/tuple.dart';

Tuple2<int, String> demo() {
  return new Tuple2(42, "life is good");
}

void main() {
  final result = demo();
  if (result.item1 > 20) {
    print(result.item2);
  }
}

Not quite as concise, but it's clean and expressive code. What I like most about it is that it doesn't need to change much once your quick experimental project really takes off and you start adding features and need to add more structure to keep on top of things.

class FormatResult {
  bool changed;
  String result;
  FormatResult(this.changed, this.result);
}

FormatResult powerFormatter(String text) {
  bool changed = false;
  String result = text;
  // secret implementation magic
  // ...
  return new FormatResult(changed, result);
}

void main() {
  String draftCode = "print('Hello World.');";
  final reformatted = powerFormatter(draftCode);
  if (reformatted.changed) {
    // some expensive operation involving servers in the cloud.
  }
}

So, yes, it's not much of an improvement over Java, but it works, it is clear, and reasonably efficient for building UIs. And I really like how I can quickly hack things together (sometimes starting on DartPad in a break at work) and then add structure later when I know that the project will live on and grow.

like image 25
Robert Jack Will Avatar answered Oct 23 '22 01:10

Robert Jack Will


Create a class:

import 'dart:core';

class Tuple<T1, T2> {
  final T1 item1;
  final T2 item2;

  Tuple({
    this.item1,
    this.item2,
  });

  factory Tuple.fromJson(Map<String, dynamic> json) {
    return Tuple(
      item1: json['item1'],
      item2: json['item2'],
    );
  }
}

Call it however you want!

Tuple<double, double>(i1, i2);
or
Tuple<double, double>.fromJson(jsonData);
like image 10
Edi Avatar answered Oct 23 '22 01:10

Edi


You can create a class to return multiple values Ej:

class NewClass {
  final int number;
  final String text;

  NewClass(this.number, this.text);
}

Function that generates the values:

 NewClass buildValues() {
        return NewClass(42, 'foobar');
      }

Print:

void printValues() {
    print('${this.buildValues().number} ${this.buildValues().text}');
    // 42 foobar
  }
like image 5
Juan Diego Garcia Avatar answered Oct 23 '22 02:10

Juan Diego Garcia