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. Read Parts 1 & 2 of this article: Exploring Dart Fundamentals - Part 1 Exploring Dart Fundamentals - Part 2: Dart Control Flow Statement Done? Let’s get started. 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 function, and are positional arguments. When calling , will be assigned the value and will be assigned , resulting in being printed. add x y add(3, 5) x 3 y 5 8 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 function is a one-line equivalent of the function. It takes two positional arguments and and prints their sum. addsingleLineFunction add x y 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, implicitly returns the sum of and . It does not use the function, making it suitable for returning values directly. addsingleLineFunctionMethod x y print 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 the function takes one required positional argument and one optional positional argument with a default value of . This means you can call or . addPostional x y 0 addPostional(3) addPostional(3, 5) Named Optional calculateArea({double width = 0.0, double height = 0.0}) => width * height; computes the area of a rectangle. It uses named optional parameters and with default values of . This allows for clear and explicit function calls like . calculateArea width height 0.0 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; calculates the area of a rectangle. The parameters and are named and required, meaning you must specify them during the function call. calculateAreaRequired width height 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. Also published here.