functionParameters
Here's how you define function parameters in Dart:
Let's break down the key components of function parameters in Dart:
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
.functionName: This is the name of the function.
parameterType: This is the data type of the parameter. It specifies what kind of values the parameter can accept.
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:
In this example, the sayHello
function does not have any parameters.
Function with One Parameter:
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:
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.
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.
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.
Last updated