Doc comments
- Dart course
- Intermediate
- Lesson 107/116
- 5 min
Learn documentation comments with ///.
Doc comments
Doc comments start with /// (three slashes). Unlike ordinary // comments, dartdoc reads them and turns them into HTML.
You write a single /// line or a multi-line /// block; each line must start with ///.
They go above classes, functions and variables. The first sentence becomes the summary, so keep it short and clear.
A doc comment example
Typical usage: under /// Adds two numbers. you add /// [a] and [b] are summed., then write the function.
The brackets [a] create a link to the parameter; dartdoc turns it into a clickable reference.
The first line should be a short summary; details go on the second and later lines.
Practice A multi-line doc comment
Write int add(int a, int b) => a + b;. Add a two-line doc comment above it: first line /// Sums two numbers., second line /// [a] and [b] are summed.. In main call print(add(2, 3)).
Starter code
// Write the add function and its doc comment
void main() {
// print the result of add(2, 3)
}Sample solution
/// Sums two numbers.
/// [a] and [b] are summed.
int add(int a, int b) => a + b;
void main() {
print(add(2, 3));
}Expected output
5
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 Doc comment marker
/// ____ Greets the name.
/// /// Greets the name.
Explanation: /// is the doc comment marker.
2 Which marker starts a doc comment?
- //
- /// Correct answer
- /*
- **
Explanation: /// is a doc comment, // is an ordinary comment.
3 Doc comment parts
| Term | Meaning |
|---|---|
/// |
Doc comment |
// |
Normal comment |
[a] |
Reference |
First sentence |
Summary |