Function Definition (Return Type / Args / return)
In Dart, a function is defined by writing the return type first, followed by the parameter list and body wrapped in { }. Specifying types allows the compiler to catch mistakes early. Use return to return a value, and void when the function returns nothing.
Function definition syntax
// Define a function as: returnType functionName(paramType paramName, ...) { body }
returnType functionName(paramType paramName) {
// body
return value;
}
// Use void when the function has no return value
void functionName(paramType paramName) {
// body (no return needed)
}
// Use arrow syntax (=>) when the body is a single expression
returnType functionName(paramType paramName) => expression;
Parameter types
| Type | Syntax | Description |
|---|---|---|
| Positional (required) | type name | Must be passed when calling the function. Values are matched in declaration order. |
| Named (required) | {required type name} | Wrapped in {} with the required keyword. Passed by name at the call site. |
| Named (optional) | {type name = defaultValue} | Wrapped in {} with a default value. The default is used if the argument is omitted. |
| Positional (optional) | [type name = defaultValue] | Wrapped in []. Must be placed at the end. The default is used if the argument is omitted. |
Sample code
sg_function_basic.dart
// sg_function_basic.dart — basic examples of defining, calling, and returning values from functions
// Uses character data from Steins;Gate to demonstrate each concept
// -----------------------------------------------
// void function — no return value
// -----------------------------------------------
// Takes a character name and prints a greeting (no return value)
void greet(String name) {
print('$name: El Psy Kongroo');
}
// -----------------------------------------------
// Function with a return value
// -----------------------------------------------
// Takes an experiment name and researcher name, and returns a report string
String makeReport(String experimentName, String researcher) {
return '[$experimentName] Researcher: $researcher';
}
// -----------------------------------------------
// Arrow syntax — use when the body is a single expression
// -----------------------------------------------
// Returns the sum of two integers (arrow syntax)
int addLabMembers(int a, int b) => a + b;
// -----------------------------------------------
// Default parameters (named parameters)
// -----------------------------------------------
// Prints character info. divergence is optional and defaults to 1.000000
void printCharacter({
required String name,
required int labMemberNo,
double divergence = 1.000000,
}) {
print('Lab Member No.$labMemberNo $name (World line: $divergence)');
}
// -----------------------------------------------
// Default parameters (optional positional)
// -----------------------------------------------
// Takes a machine name and destination, and returns a launch message
// If destination is omitted, 'El Psy Kongroo' is used
String launch(String machineName, [String destination = 'El Psy Kongroo']) {
return '$machineName → Departing to $destination';
}
// -----------------------------------------------
// main function — entry point of the program
// -----------------------------------------------
void main() {
// Call a void function
greet('Okabe Rintaro');
greet('Makise Kurisu');
print('');
// Call functions that return a value
var report1 = makeReport('Time Leap Machine Experiment', 'Hashida Itaru');
var report2 = makeReport('Future Gadget No.8', 'Okabe Rintaro');
print(report1);
print(report2);
print('');
// Call a function using arrow syntax
var total = addLabMembers(8, 3);
print('Total lab members: $total');
print('');
// Call with named parameters (specify argument names explicitly)
printCharacter(name: 'Okabe Rintaro', labMemberNo: 1);
printCharacter(name: 'Makise Kurisu', labMemberNo: 4, divergence: 1.048596);
printCharacter(name: 'Shiina Mayuri', labMemberNo: 2, divergence: 0.571024);
print('');
// Call with optional positional parameters
print(launch('C204')); // uses the default value
print(launch('IBN 5100', '1975')); // provides destination explicitly
}
dart run sg_function_basic.dart Okabe Rintaro: El Psy Kongroo Makise Kurisu: El Psy Kongroo [Time Leap Machine Experiment] Researcher: Hashida Itaru [Future Gadget No.8] Researcher: Okabe Rintaro Total lab members: 11 Lab Member No.1 Okabe Rintaro (World line: 1.0) Lab Member No.4 Makise Kurisu (World line: 1.048596) Lab Member No.2 Shiina Mayuri (World line: 0.571024) C204 → Departing to El Psy Kongroo IBN 5100 → Departing to 1975
Returning multiple values with records
Since Dart 3, you can use record types ((type1, type2)) to return multiple values from a function.
sg_function_record.dart
// sg_function_record.dart — example of returning multiple values using a record type
// Uses world line data from Steins;Gate to demonstrate the concept
// -----------------------------------------------
// Function that returns multiple values as a record
// -----------------------------------------------
// Takes a divergence number and returns the world line name and branch year as a pair
(String name, String branchYear) getWorldLineInfo(double divergence) {
if (divergence >= 1.0) {
return ('Beta world line', '1974');
} else if (divergence >= 0.5) {
return ('Alpha world line', '1975');
} else {
return ('Gamma world line', '1973');
}
}
void main() {
// Receive the record and destructure it into variables
var (name, year) = getWorldLineInfo(1.048596);
print('World line: $name Branch year: $year');
var (name2, year2) = getWorldLineInfo(0.571024);
print('World line: $name2 Branch year: $year2');
// You can also store the whole record in a variable and access fields individually
var info = getWorldLineInfo(0.337187);
print('World line: ${info.$1} Branch year: ${info.$2}');
}
dart run sg_function_record.dart World line: Beta world line Branch year: 1974 World line: Alpha world line Branch year: 1975 World line: Gamma world line Branch year: 1973
Summary
Dart functions are defined as returnType functionName(params) { ... } and return a value with return. Use void when there is no return value, and the => arrow syntax when the body is a single expression. In addition to required positional parameters, you can use named parameters wrapped in {} and optional positional parameters wrapped in [] to improve readability at the call site. Since Dart 3, record types allow you to return multiple values from a single function. For variables and types, see Variables and Types (var / final / const). For how functions interact with null safety, see Null Safety.
If you find any errors or copyright issues, please contact us.