> For the complete documentation index, see [llms.txt](https://des-vu-technologies.gitbook.io/blogs-by-shankar/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://des-vu-technologies.gitbook.io/blogs-by-shankar/functions/functionparameters.md).

# functionParameters

```
In Dart, function parameters are used to pass values into functions.
Parameters allow you to make your functions more flexible and reusable
by accepting input data that can be used within the function's body.
Dart functions can have zero or more parameters, and each parameter has a data type and a name.
```

Here's how you define function parameters in Dart:

```dart
returnType functionName(parameter1Type parameter1Name, parameter2Type parameter2Name, ...) {
  // Function body
  // Code that uses the parameters
}
```

Let's break down the key components of function parameters in Dart:

1. **returnType**: This specifies the data type of the value that the function will return. If the function doesn't return a value, you can use `void`.
2. **functionName**: This is the name of the function.
3. **parameterType**: This is the data type of the parameter. It specifies what kind of values the parameter can accept.
4. **parameterName**: This is the name of the parameter. It is used within the function to refer to the value passed as an argument when the function is called.

Here are some examples of functions with different types of parameters in Dart:

#### Function with No Parameters:

```dart
void sayHello() {
  print('Hello, Dart!');
}
```

In this example, the `sayHello` function does not have any parameters.

#### Function with One Parameter:

```dart
void greet(String name) {
  print('Hello, $name!');
}
```

In this example, the `greet` function has one parameter of type `String` named `name`. It accepts a name as input and uses it within the function.

#### Function with Multiple Parameters:

```dart
int add(int a, int b) {
  return a + b;
}
```

In this example, the `add` function has two parameters, both of type `int`. It accepts two integers (`a` and `b`) as input, performs addition, and returns the result.

#### Function with Optional Parameters:

You can make parameters optional by enclosing them in square brackets `[]`. Optional parameters allow you to call a function without providing values for all parameters.

```dart
void printMessage(String message, [int? times]) {
  for (var i = 0; i < times ?? 1; i++) {
    print(message);
  }
}
```

In this example, the `printMessage` function has one required parameter (`message`) and one optional parameter (`times`). The `times` parameter is enclosed in square brackets, making it optional. If `times` is not provided when calling the function, it defaults to `1`.

#### Function with Named Parameters:

You can use named parameters by enclosing them in curly braces `{}`. Named parameters allow you to pass arguments to a function in any order, making function calls more expressive.

```dart
void greet({String? firstName, String? lastName}) {
  print('Hello, ${firstName ?? 'User'} ${lastName ?? ''}!');
}
```

In this example, the `greet` function has two named parameters, `firstName` and `lastName`. You can call the function with named arguments like this: `greet(firstName: 'John', lastName: 'Doe')`.

Understanding how to define and use function parameters in Dart is essential for creating flexible and versatile functions that can handle a variety of inputs.
