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 Functions


Share

Dart Functions

A function is a block of code that only runs when it is called.

You can pass data, known as parameters, into a function.

A function can also give data back as a result.

Functions let you write code once and use it many times.


Create a Function

To create (define) a function, write the return type, the name of the function, parentheses () and curly brackets {}:

void sayHello() {
  print('Hello!');
}
  • void is the return type, and it means that the function does not give back a value
  • sayHello is the name of the function
  • The code inside the curly brackets is the body of the function

Function names use lowerCamelCase, like variable names.



Call a Function

To call (run) a function, write its name followed by parentheses and a semicolon:

Example

void sayHello() {
  print('Hello!');
}

void main() {
  sayHello();
}
Try it Yourself »

Result:

Hello!

A function can be called as many times as you want:

Example

void sayHello() {
  print('Hello!');
}

void main() {
  sayHello();
  sayHello();
  sayHello();
}
Try it Yourself »

Result:

Hello!
Hello!
Hello!

If you forget the parentheses, the function is not called.


The main() Function

You have used one function in every program so far: main().

main() is a special function, because Dart calls it for you when the program starts.

All other functions only run when your code calls them:

Example

void greet() {
  print('Welcome!');
}

void unused() {
  print('This line is never printed');
}

void main() {
  print('Program starts');
  greet();
  print('Program ends');
}
Try it Yourself »

Result:

Program starts
Welcome!
Program ends

Top-Level Functions

A function that is written outside of any other function or class is called a top-level function.

main(), sayHello() and greet() above are all top-level functions.

The order does not matter, so you can call a top-level function that is written further down in the file:

Example

void main() {
  printLine();
  print('Menu');
  printLine();
}

void printLine() {
  print('----------');
}
Try it Yourself »

Result:

----------
Menu
----------

Return a Value

A function can give a value back to the code that called it.

Instead of void, you write the type of the value, and use the return keyword:

Example

int getMaxScore() {
  return 100;
}

String getGreeting() {
  return 'Good morning';
}

void main() {
  int maxScore = getMaxScore();
  print(maxScore);
  print(getGreeting());
}
Try it Yourself »

Result:

100
Good morning

The function call is replaced by the value it returns, so you can store it in a variable or print it directly.

A function becomes more useful when you can give it data.

A small example, with a parameter called name:

Example

void greet(String name) {
  print('Hello, $name!');
}

void main() {
  greet('Tom');
  greet('Anna');
}
Try it Yourself »

Result:

Hello, Tom!
Hello, Anna!

You will learn about parameters in the next chapter, Parameters, and more about return values in Return Values.



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.