> ## Documentation Index
> Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Dart Function

A function in Dart is a first-class object of type `Function` that encapsulates a reusable block of code. Because Dart is a true object-oriented language, functions can be assigned to variables, passed as arguments to other functions, and returned as results from other functions.

## Basic Syntax

A standard function declaration includes a return type, an identifier, a parameter list, and a block body. Type annotations are optional but recommended for static type checking.

```dart theme={"dark"}
ReturnType functionName(ParameterType parameterName) {
  // execution block
  return expression;
}
```

If no return type is explicitly defined, it defaults to `dynamic`. If a function does not explicitly return a value, it implicitly returns `null`.

## Parameter Structures

Dart categorizes parameters into two primary types: positional and named. A function can utilize both, but positional parameters must precede named parameters.

### 1. Positional Parameters

Arguments are mapped to parameters based on their exact position in the signature.

* **Required Positional:** Must be provided by the caller.

```dart theme={"dark"}
void executeTask(int a, int b) {}
```

* **Optional Positional:** Wrapped in square brackets `[]`. They must be nullable or have a default value.

```dart theme={"dark"}
void executeTask(int a, [int? b, int c = 0]) {}
```

### 2. Named Parameters

Arguments are passed by name, making the call site order-independent. They are wrapped in curly braces `{}`. Named parameters are optional by default unless explicitly annotated with the `required` keyword.

```dart theme={"dark"}
void configureSettings({required String host, int port = 8080, bool? isSecure}) {}

// Invocation
configureSettings(host: 'localhost', isSecure: true);
```

## Arrow Syntax (Expression Bodies)

For functions containing only a single expression, Dart provides the `=>` (fat arrow) shorthand. The `=> expr` syntax is strictly equivalent to `{ return expr; }`. It accepts only an expression, not a statement.

```dart theme={"dark"}
int multiply(int a, int b) => a * b;
```

## Anonymous Functions

Functions can be declared without an identifier. These are known as anonymous functions, lambdas, or closures. They consist of a parameter list followed by a block body or an arrow expression.

```dart theme={"dark"}
(Type param) {
  // execution block
};

// Or using arrow syntax
(Type param) => expression;
```

## Lexical Scope and Closures

Dart is a lexically scoped language, meaning the scope of variables is determined statically by the layout of the code.

A **closure** is a function object that captures variables from its lexical scope. The function retains access to those variables even if it is executed outside of their original scope.

```dart theme={"dark"}
Function makeAdder(int addBy) {
  return (int i) => addBy + i; // Captures 'addBy' from the outer scope
}
```

## Generator Functions

Dart supports generator functions to lazily produce a sequence of values.

* **Synchronous Generator:** Returns an `Iterable`. Marked with `sync*` and yields values using the `yield` keyword.

```dart theme={"dark"}
Iterable<int> generateSequence(int max) sync* {
  for (int i = 0; i < max; i++) yield i;
}
```

* **Asynchronous Generator:** Returns a `Stream`. Marked with `async*` and yields values asynchronously.

```dart theme={"dark"}
Stream<int> generateAsyncSequence(int max) async* {
  for (int i = 0; i < max; i++) yield i;
}
```

<div
  style={{ 
display: "flex", 
justifyContent: "space-between", 
alignItems: "center", 
maxWidth: "754px", 
padding: "1rem 0",
marginBottom: "24px"
}}
>
  <span style={{ fontWeight: "bold", fontSize: "1.25rem", color: "var(--tw-prose-headings)", fontFamily: "Inter, ui-sans-serif, system-ui, sans-serif" }}>Tired of Poor Dart Skills? Fix That With Deep Grasping!</span>

  <a
    href="https://syntblaze.com"
    target="_blank"
    style={{ 
  marginLeft: "24px",
  textDecoration: "none", 
  backgroundColor: "#007AFF",
  color: "#ffffff", 
  padding: "6px 16px", 
  borderRadius: "16px",
  fontSize: "0.9rem",
  fontWeight: "600",
  textAlign: "center",
  transition: "background-color 0.2s ease"
}}
  >
    Learn More
  </a>
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/skill-tracking.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=b9b0305c93bb501c9e767b5c76c88835" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/skill-tracking.png" />

  <img src="https://mintcdn.com/syntblazellc/23tyuOzaWS88qFlc/images/nuggets.png?fit=max&auto=format&n=23tyuOzaWS88qFlc&q=85&s=c86c80197299762989e9b882419b2109" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/nuggets.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/bite-sized-exercises.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=a65f9a38c37ff28ab73ed783c53c60e3" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/bite-sized-exercises.png" />
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap", marginTop: "12px" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/mastery-chain.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=748a1763454713e679260fbb95f154a2" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/mastery-chain.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-previews.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=242f61448ff5dd6deaaab2dccc13b507" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-previews.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-explanations.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=cf0fc1c31f9cd0fc26716781be05fbc9" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-explanations.png" />
</div>
