Functions
Define a function with a return type, name, and parameters:
String greet(String name) { ... }.
Example: a regular function and the arrow shorthand
String greet(String name) {
return 'Hello, $name!';
}
int square(int n) => n * n;
void main() {
print(greet('Chidi'));
print('Square of 6 is ${square(6)}');
}
Hello, Chidi!
Square of 6 is 36
int square(int n) => n * n; is Dart's short arrow syntax
for a one-line function that immediately returns an expression — no
curly braces or explicit return needed. It's purely a
shorthand for the exact same thing as the longer greet
function above; reach for it whenever a function's whole body is one
simple expression.
String greet(String name) {
return 'Hello, $name!';
}
void main() {
print(greet('Chidi'));
}
Hello, Chidi!