Menu
×
   ❮   
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP C C++ C# AWS W3.CSS HOW TO BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS DSA TYPESCRIPT ANGULAR ANGULARJS GIT POSTGRESQL SQLITE MONGODB ASP AI R GO KOTLIN SWIFT SASS VUE GEN AI SCIPY CYBERSECURITY DATA SCIENCE INTRO TO PROGRAMMING HTML & CSS BASH RUST DART PLAYWRIGHT TOOLS MATH SHEETS POWER BI

Dart Tutorial

Dart HOME Dart Intro Dart Get Started Dart Syntax Dart Output Dart Comments Dart Variables Dart final and const Dart Data Types Dart Numbers Dart Strings Dart String Interpolation Dart String Methods Dart Booleans Dart Type Conversion Dart Null Safety Dart late Dart Operators Dart Arithmetic Dart Assignment Dart Comparison Dart Logical Dart Type Test Dart Null Operators Dart Cascade Dart If...Else Dart Switch Dart While Loop Dart For Loop Dart Break/Continue

Dart Collections

Dart Lists Dart Access List Items Dart Change List Items Dart Loop Lists Dart List Methods Dart Sets Dart Maps Dart Access Map Items Dart Loop Maps Dart Collection if/for Dart Spread Operator

Dart Functions

Dart Functions Dart Parameters Dart Named Parameters Dart Return Values Dart Arrow Functions Dart Anonymous Functions Dart Closures Dart Scope Dart Recursion

Dart Classes

Dart OOP Dart Classes/Objects Dart Constructors Dart Named Constructors Dart Factory Constructors Dart Getters and Setters Dart Static Members Dart Inheritance Dart Abstract Classes Dart Interfaces Dart Mixins Dart Extension Methods Dart Enums Dart Generics

Dart 3 Features

Dart Records Dart Patterns Dart Switch Expressions Dart Sealed Classes Dart Class Modifiers

Dart Errors

Dart Exceptions Dart Custom Exceptions

Dart Async

Dart Async Intro Dart Futures Dart async/await Dart Streams Dart Isolates

Dart Libraries

Dart Imports Dart Packages Dart Math Dart Dates Dart JSON Dart RegExp Dart User Input Dart Files Dart Testing

Dart Examples

Dart Examples Dart Exercises


Dart Return Values


Share

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:

Example

int getScore() {
  return 'ten';
}

void main() {
  print(getScore());
}
Try it Yourself »

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.



Free account

Track your progress

XP earned 0 Day streak 0 Spaces 0 League —

New

Learn in Adventure Mode W3Schools Adventure App

Coding fundamentals as bite-sized lessons and challenges.

×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
sales@w3schools.com

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
help@w3schools.com

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookies and privacy policy.

Copyright 1999-2026 by W3Schools. All Rights Reserved. W3Schools is Powered by W3.CSS.