CodeBlanks Logo Icon CodeBlanks

Doc comments

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

main.dart
// Write the add function and its doc comment

void main() {
  // print the result of add(2, 3)
}

Sample solution

main.dart
/// 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

Question
/// ____ Greets the name.
Answer
/// /// 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

All lessons in Intermediate