Menu
Coddy logo textTech

The 'void' Keyword

Part of the Fundamentals section of Coddy's Dart journey — lesson 69 of 94.

The void keyword in Dart indicates a function returns no value. It's used when a function performs actions without producing a result.

void printGreeting() {
  print('Hello, Dart programmer!');
}

void main() {
  printGreeting();
}

Output:

Hello, Dart programmer!

Compare with a value-returning function:

void main() {
  void sayHello() {
    print('Hello!');
  }
  
  int getNumber() {
    return 42;
  }
  
  sayHello();
  int result = getNumber();
  print(result);
}

Output:

Hello!
42
challenge icon

Challenge

Beginner

In this challenge, you'll practice using the void keyword in Dart. The void keyword is used to indicate that a function doesn't return any value.

Complete the code below by creating a simple function called printWelcomeMessage that:

  1. Uses the void keyword to indicate it doesn't return a value
  2. Prints the message Welcome to Dart Programming! when called

Then call this function from the main function.

Expected output:

Welcome to Dart Programming!

Cheat sheet

The void keyword indicates a function returns no value:

void printGreeting() {
  print('Hello, Dart programmer!');
}

void main() {
  printGreeting();
}

Compare void functions with value-returning functions:

void sayHello() {
  print('Hello!');
}

int getNumber() {
  return 42;
}

// Usage
sayHello();           // prints but returns nothing
int result = getNumber(); // returns a value

Try it yourself

// TODO: Create a function called printWelcomeMessage
// The function should use the void keyword and print a welcome message

void main() {
  // TODO: Call the printWelcomeMessage function here
}
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Fundamentals