Skip to main content

Menu

System onlineAI Tutorials / 2026

AI Tutorials
Knowledge search

Recent · Popular · Tutorials

  • dart3 tutorials
  • java1 tutorial
  • Javascript3 tutorials
  • Node.js Introduction2 tutorials
  • dartdart · General · In depth
  • JavascriptJavascript · Advanced · In depth
  • JavascriptJavascript · Advanced · Mid
  • JavascriptJavascript · Advanced · Short

Find a tutorial

Tell the assistant what you want to learn. It suggests published chapters.

For example: “I want to learn Rust ownership from the start.” The conversation is kept only in this tab.

0 / 2,000 characters

Enter to send, Shift+Enter for a new line. Do not include personal information.

Chapter 6

Functions and Parameters

A function is a named block of code that does one job. You give it inputs (called parameters), it hands back an output (called the return value). Functions let you write a piece of logic once and reuse it everywhere, instead of copying and pasting the same lines around your program.
Level: GeneralLength: Mid2 min read

Read at another depth

Functions and Parameters

A function is a named block of code that does one job. You give it inputs (called parameters), it hands back an output (called the return value). Functions let you write a piece of logic once and reuse it everywhere, instead of copying and pasting the same lines around your program.

You have already seen one function in every Dart program so far: main. Dart starts running your program by calling it.

Writing a Simple Function

Here is a function that adds two numbers:

Code example in dart
int add(int a, int b) {
  return a + b;
}

void main() {
  print(add(2, 3)); // 5
}

Reading it piece by piece:

  • int before the name: the type of value the function gives back.
  • add: the function's name.
  • (int a, int b): two parameters, each typed as int (a whole number).
  • return: the keyword that hands back the result.

If a function returns nothing, its return type is void, which means "no value."

Dart also has a shortcut for one-line functions. The => symbol means "return this":

Code example in dart
int add(int a, int b) => a + b;

Both forms do the same thing. The arrow form is common for short functions.

Named Parameters

Dart supports two styles of parameters. Positional parameters are filled in by order, like add(2, 3). Named parameters are written inside curly braces and are called by name, so the caller can see what each value means:

Code example in dart
void greet(String name, {String prefix = 'Hello'}) {
  print('$prefix, $name!');
}

void main() {
  greet('Ada');              // Hello, Ada!
  greet('Ada', prefix: 'Hi'); // Hi, Ada!
}

Notice prefix = 'Hello'. That is a default value: if the caller does not supply prefix, Dart uses 'Hello'. The $name inside the string is interpolation — Dart replaces it with the variable's value.

Functions as Values

In Dart, a function is a value too. You can store one in a variable and pass one into another function. A function passed to a function is often called a callback:

Code example in dart
void runTwice(void Function() action) {
  action();
  action();
}

void main() {
  runTwice(() => print('tick')); // tick tick
}

void Function() describes the type: a function that takes no arguments and returns nothing. The () => print('tick') part is a small unnamed function, called an anonymous function or lambda.

caption: How arguments, return values, and callbacks flow in and out of a function.

alt: A box labeled Function receives arguments from the caller and a callback function, and returns a value back to the caller.

Common Mistakes to Avoid

  • Forgetting that named parameters are optional only if they have a default or a nullable type (a type that allows no value, written with a ?). Otherwise Dart requires them.
  • Mixing up parameter order with positional arguments — order matters for positional parameters.
  • Expecting a void function to give back a value.

Exercise

How would you rewrite String shout(String word) so the exclamation mark is optional, controlled by the caller?

Show answer

Add a named boolean parameter with a default: String shout(String word, {bool excited = false}) and return excited ? '$word!' : word. Callers write shout('hi') or shout('hi', excited: true).

Spotted a mistake, or code that does not work?

Report an issue

Chapter: Functions and Parameters

0 / 2,000 characters

Plain text, for example: the second code sample does not compile. Do not include personal information.

You can't do that