Dart Functions and Parameter Types — Part 3
Exploring Dart Functions and Parameter Types — Positional Arguments, One-Line Function, Optional Parameters.
Functions in Dart provide a powerful way to organize code and encapsulate functionality. In this blog post, we’ll explore different aspects of Dart functions, including positional arguments, one-line functions, optional parameters, and named parameters.
1. Positional Arguments
Positional arguments are the most basic type of arguments in Dart functions. Their position in the function call defines them.
void add(int x, int y) {
print(x + y);
}
In the add
function, x
and y
are positional arguments. When calling add(3, 5)
, x
will be assigned the value 3
and y
will be assigned 5
, resulting in 8
being printed.
2. One-Line Function
One-line functions provide a concise way to define simple functions in Dart. They are often used for short and straightforward operations.
void addsingleLineFunction(int x, int y) => print(x + y);
The addsingleLineFunction
function is a one-line equivalent of the add
function. It takes two positional arguments x
and y
and prints their sum.
Another Method
Another method demonstrates another way to define a function in Dart, which implicitly returns a value.
addsingleLineFunctionMethod(int x, int y) => x + y;
Unlike the previous functions, addsingleLineFunctionMethod
implicitly returns the sum of x
and y
. It does not use the print
function, making it suitable for returning values directly.
3. Optional Parameters
Optional parameters allow for flexibility in function calls by making certain parameters optional.
Positionally Optional
addPostional(int x, [int y = 0]) => x + y;
The addPostional
the function takes one required positional argument x
and one optional positional argument y
with a default value of 0
. This means you can call addPostional(3)
or addPostional(3, 5)
.
Named Optional
calculateArea({double width = 0.0, double height = 0.0}) => width * height;
calculateArea
computes the area of a rectangle. It uses named optional parameters width
and height
with default values of 0.0
. This allows for clear and explicit function calls like calculateArea(width: 10.0, height: 5.0)
.
5. Named Required
Named required parameters ensure that certain arguments are provided during function calls.
calculateAreaRequired({required double width , required double height}) => width * height;
calculateAreaRequired
calculates the area of a rectangle. The parameters width
and height
are named and required, meaning you must specify them during the function call.
Putting It All Together
void main() {
// Function with no parameters
void greet() {
print("Hello, Dart!");
}
// Function with required parameters
void greetWithName(String name) {
print("Hello, $name!");
}
// Function with optional parameters
void greetWithOptional({String name = 'sadanand gadwal'}) {
print("Hello, $name!");
}
// Function with positional parameters
void greetWithPositional([String name = 'sadanand gadwal']) {
print("Hello, $name!");
}
// Function with return value
int add(int a, int b) {
return a + b;
}
greet(); // Hello, Dart!
greetWithName("sadanand gadwal"); // Hello, sadanand gadwal!
greetWithOptional(name: "sadanand gadwal"); // Hello,sadanand gadwal!
greetWithOptional(); // Hello, sadanand gadwal!
greetWithPositional("sadanand gadwal"); // Hello, sadanand gadwal!
greetWithPositional(); // Hello, sadanand gadwal!
print(add(3, 5)); // 8
}
Conclusion
Understanding these different types of parameters and function definitions in Dart is essential for writing clean, readable, and flexible code. They enable you to express complex logic concisely and organized, making your Dart programs more maintainable and efficient.