Dart Return Values
Dart Return Values
A function can send a value back to the code that called it.
This is called returning a value, and you do it with the return keyword:
Example
int multiplyByFive(int x) {
return x * 5;
}
void main() {
print(multiplyByFive(3));
print(multiplyByFive(5));
print(multiplyByFive(9));
}
Try it Yourself »
Result:
15
25
45
You can store the returned value in a variable, and use it later:
Example
double area(double width, double height) {
return width * height;
}
void main() {
double kitchen = area(3.5, 4.0);
double bedroom = area(3.0, 3.0);
print('Total: ${kitchen + bedroom} square meters');
}
Try it Yourself »
Result:
Total: 23.0 square meters
The Return Type
The type before the function name is the return type.
It tells Dart what kind of value the function returns.
The value after return must match this type:
Example
String fullName(String first, String last) {
return '$first $last';
}
bool isAdult(int age) {
return age >= 18;
}
void main() {
print(fullName('Anna', 'Smith'));
print(isAdult(15));
print(isAdult(30));
}
Try it Yourself »
Result:
Anna Smith
false
true
If you return a value of the wrong type, the program does not run:
Result:
main.dart:2:10: Error: A value of type 'String' can't be returned from a function with return type 'int'.
return 'ten';
^
A function with a return type like int must always return a value.
If one path through the function can end without return, the program does not run:
Example
String grade(int score) {
if (score >= 50) {
return 'Passed';
}
}
void main() {
print(grade(70));
}
Try it Yourself »
Result:
main.dart:1:8: Error: A non-null value must be returned since the return type 'String' doesn't allow null.
String grade(int score) {
^
Early Return
return stops the function at once.
The code after it is not run.
This lets you return early when you already know the answer:
Example
String grade(int score) {
if (score >= 90) {
return 'A';
}
if (score >= 70) {
return 'B';
}
if (score >= 50) {
return 'C';
}
return 'Failed';
}
void main() {
print(grade(95));
print(grade(72));
print(grade(30));
}
Try it Yourself »
Result:
A
B
Failed
An early return inside a loop stops both the loop and the function:
Example
int findIndex(List<String> items, String target) {
for (var i = 0; i < items.length; i++) {
if (items[i] == target) {
return i;
}
}
return -1;
}
void main() {
var cars = ['Volvo', 'BMW', 'Ford'];
print(findIndex(cars, 'BMW'));
print(findIndex(cars, 'Mazda'));
}
Try it Yourself »
Result:
1
-1
Returning Nothing (void)
A function with the return type void does not return a value.
You can still use return; without a value, to stop the function early:
Example
void checkAge(int age) {
if (age < 0) {
print('Not a valid age');
return;
}
print('Age is $age');
}
void main() {
checkAge(25);
checkAge(-3);
}
Try it Yourself »
Result:
Age is 25
Not a valid age
You cannot use the result of a void function, because there is no result:
Example
void sayHello() {
print('Hello!');
}
void main() {
var result = sayHello();
print(result);
}
Try it Yourself »
Result:
main.dart:7:9: Error: This expression has type 'void' and can't be used.
print(result);
^
Return Several Values
A function can only return one value.
To return several values, you can put them in a list and return the list:
Example
List<int> minAndMax(List<int> numbers) {
var smallest = numbers[0];
var largest = numbers[0];
for (var n in numbers) {
if (n < smallest) smallest = n;
if (n > largest) largest = n;
}
return [smallest, largest];
}
void main() {
var result = minAndMax([4, 9, 1, 7]);
print('Smallest: ${result[0]}');
print('Largest: ${result[1]}');
}
Try it Yourself »
Result:
Smallest: 1
Largest: 9
This works, but you must remember which position holds which value.
Dart has a better tool for this, called records, which you will learn about in Dart Records.