CodeBlanks Logo Icon CodeBlanks

Your first program

Learn to write your first Dart program with main and print.

The main function

Every Dart program starts from the void main() { ... } function. The code between the curly braces runs top to bottom when the program starts.

The void return type means the function returns no value. main is the special name the Dart runtime looks for; it cannot be changed.

The parentheses must be written even when empty. This structure is the skeleton of the program.

Practice Print two lines

Write void main() with curly braces containing two print calls. The first line prints Started, the second prints Done.

Starter code

main.dart
// Add main and two prints

Sample solution

main.dart
void main() {
  print("Started");
  print("Done");
}

Expected output

Started
Done

The print function

The print function writes its argument to the screen (standard output) and automatically adds a newline. So each print call produces a new line.

You can pass a string (double or single quoted), a number or a boolean inside the parentheses. Dart converts it to text automatically.

It is the tool you will use most while debugging: you add a print call to see a variable's value.

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 Which is a correct main definition?

  • void main() { } Correct answer
  • function main() { }
  • void main -> { }
  • def main():

Explanation: main is defined with void return type and empty parentheses.

2 Output command

Question
____('Learning Dart');
Answer
print('Learning Dart');

Explanation: print is used to write output to the screen.

3 Match the terms

Term Meaning
print Writes to screen
void Empty return type
main Entry point
{ } Code block

All lessons in Beginner