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:
int add(int a, int b) {
return a + b;
}
void main() {
print(add(2, 3)); // 5
}Reading it piece by piece:
intbefore the name: the type of value the function gives back.add: the function's name.(int a, int b): two parameters, each typed asint(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":
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:
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:
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
voidfunction 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?