free geoip
69

Dart Functions Explained for Beginners

If you are new to Dart programming, understanding how functions work is one of the most important steps in mastering…

If you are new to Dart programming, understanding how functions work is one of the most important steps in mastering the language. In Dart, functions are building blocks that allow developers to organize and reuse code effectively.

Dart functions for beginners

What is a Function in Dart?

A function is a block of code that performs a specific task and can be called multiple times throughout your Dart program. Functions help reduce repetition and improve code readability.

Basic Syntax of Dart Function

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
void greet() {
print('Hello, Dart!');
}
void greet() { print('Hello, Dart!'); }
void greet() {
  print('Hello, Dart!');
}

In this example:

  • void means the function doesn’t return any value.
  • greet is the function name.
  • The body of the function contains a print statement.

To run the function:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
void main() {
greet(); // Output: Hello, Dart!
}
void main() { greet(); // Output: Hello, Dart! }
void main() {
  greet(); // Output: Hello, Dart!
}

Dart Function with Parameters

You can also pass data to functions using parameters.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
void greetUser(String name) {
print('Hello, $name!');
}
void main() {
greetUser('Alice'); // Output: Hello, Alice!
}
void greetUser(String name) { print('Hello, $name!'); } void main() { greetUser('Alice'); // Output: Hello, Alice! }
void greetUser(String name) {
  print('Hello, $name!');
}

void main() {
  greetUser('Alice'); // Output: Hello, Alice!
}

Dart Function with Return Value

A function can return a value using the return keyword.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
int add(int a, int b) {
return a + b;
}
void main() {
int result = add(3, 5);
print('Sum is: $result'); // Output: Sum is: 8
}
int add(int a, int b) { return a + b; } void main() { int result = add(3, 5); print('Sum is: $result'); // Output: Sum is: 8 }
int add(int a, int b) {
  return a + b;
}

void main() {
  int result = add(3, 5);
  print('Sum is: $result'); // Output: Sum is: 8
}

Arrow Functions in Dart

Dart allows you to write concise functions using the => syntax.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
int multiply(int x, int y) => x * y;
void main() {
print(multiply(4, 2)); // Output: 8
}
int multiply(int x, int y) => x * y; void main() { print(multiply(4, 2)); // Output: 8 }
int multiply(int x, int y) => x * y;

void main() {
  print(multiply(4, 2)); // Output: 8
}

Why Functions Matter

Using functions:

  • Improves code modularity
  • Makes debugging easier
  • Encourages code reuse

Learn More

You can find the full Dart documentation on dart.dev for an in-depth explanation of Dart functions and other language features.

rysasahrial

Leave a Reply

Your email address will not be published. Required fields are marked *