← Dart tutorial

Dart

Functions

Define a function with a return type, name, and parameters: String greet(String name) { ... }.

Example: a regular function and the arrow shorthand

String greet(String name) {
  return 'Hello, $name!';
}

int square(int n) => n * n;

void main() {
  print(greet('Chidi'));
  print('Square of 6 is ${square(6)}');
}
Hello, Chidi!
Square of 6 is 36

int square(int n) => n * n; is Dart's short arrow syntax for a one-line function that immediately returns an expression, no curly braces or explicit return needed. It's purely a shorthand for the exact same thing as the longer greet function above; reach for it whenever a function's whole body is one simple expression.

Example
String greet(String name) {
  return 'Hello, $name!';
}

void main() {
  print(greet('Chidi'));
}
Output
Hello, Chidi!
Ready to go further?

These free tutorials are a preview of what a Code Secure Academy cohort covers, project work and all.

Explore our tracks
Don't miss out

Ready to build something real?

Create a free account to enroll in a track, and get notified the moment a cohort opens.

Create free account