DEV Community

Cover image for Learn Function in Dart
Oluborode Akintunde Paul
Oluborode Akintunde Paul

Posted on

Learn Function in Dart

What is a function?
In Dart, a function is a block of code that takes inputs, performs some operations, and returns an output. Functions help to encapsulate a specific behavior or calculation and make it reusable throughout your code. Functions can be declared either as a standalone function or as a method within a class.
``
Examples on function

`
void main(List args) {
// this is a string declared function
String eatBeans() {
return "i cooked beans";
}

// nesting a function to another function
String? newFunction() {
return eatBeans();
}

//print(newFunction());
// this is a positional function
void myEat(int x, double y, String? food) {
return print("$x , $y, $food ");
}

myEat(1, 2.5, "bread");
// this is optional positional argument
void optionalMyEat(int x, double y, [String? food]) {
return print("$x , $y, $food ");
}

optionalMyEat(2, 3.5);
// this is named optional positional argument
void myNameEat({int? x, double? y, String? food}) {
return print("$x , $y, $food ");
}

//combine named parameters with positional parameters
myNameEat(x: 5, y: 4.5, food: "rice");

int myAdd(int x) {
return x + 10;
}

print(myAdd(20));

// we can program easily using arrow function

int plus(int y, int x) => y + x;
print(plus(4, 3));
}

// creating something different using typedef
typedef transform = int Function(int);
transform twice(transform f) {
return (int x) {
return f(f(x));
};
}
`

Top comments (0)