On this page10 sections
Lecture 026: Required & Positional Parameters
| Day | 10: Functions & Parameters |
| Lecture | 026 of 057 |
| Estimated video length | 12 min |
| Target | Dart 3.12 |
| Level | Beginner |
Most functions need some information to do their job: the numbers to add, the name to greet. The simplest kind of parameter is required and positional. You must always pass it, and Dart matches values to parameters by their order.
Contents
Prerequisites · Objectives · Lesson · Code Examples · Common Mistakes · Practice Questions · Answers · Takeaways
Prerequisites
Lecture 025 (functions).
Learning Objectives
- Write a function with several parameters
- Pass arguments in the right order
- Understand why order matters
- Read Dart's "too few / too many arguments" errors
1. Required Positional Parameters
List the parameters inside the brackets, each with a type:
int add(int a, int b) {
return a + b;
}
print(add(3, 4)); // 7These are positional: the first argument goes to the first parameter, the second to the second. They're also required: every call must pass exactly two values.
void printBill(String item, int quantity, double price) {
print('$quantity × $item = Rs ${quantity * price}');
}
printBill('Notebook', 3, 120); // 3 × Notebook = Rs 360.02. Order Matters
Dart matches by position, not by meaning:
String fullName(String first, String last) => '$first $last';
print(fullName('Sara', 'Khan')); // Sara Khan
print(fullName('Khan', 'Sara')); // Khan Sara (oops)Both calls are valid, because both are strings, but the second gives the wrong result. With parameters of different types, Dart catches the mix-up (see Mistakes). With the same types, it can't.
3. When to Use Positional Parameters
| Good fit | Better with named parameters (Lecture 027) |
|---|---|
one to three parameters whose order is obvious: add(a, b), square(n) | many parameters, or several of the same type: createUser(name, city, email, phone) |
Code Examples
Example 1: BMI Calculator
double bmi(double weightKg, double heightM) => weightKg / (heightM * heightM);
String bmiCategory(double value) {
if (value < 18.5) return 'Underweight';
if (value < 25) return 'Normal';
if (value < 30) return 'Overweight';
return 'Obese';
}
void main() {
var value = bmi(70, 1.75);
print('BMI: ${value.toStringAsFixed(1)} (${bmiCategory(value)})');
var swapped = bmi(1.75, 70);
print('Swapped arguments: ${swapped.toStringAsFixed(5)}');
}Output
BMI: 22.9 (Normal)
Swapped arguments: 0.00036Both arguments are doubles, so swapping them compiles fine but gives nonsense. That's the risk
with positional parameters, and why naming them clearly (weightKg, heightM) matters.
Common Beginner Mistakes
1. Too few arguments
int add(int a, int b) => a + b;
print(add(3));Error: Too few positional arguments: 2 required, 1 given.2. Too many arguments
print(add(1, 2, 3));Error: Too many positional arguments: 2 allowed, but 3 found.3. The wrong type
print(add(3, '4'));Error: The argument type 'String' can't be assigned to the parameter type 'int'.'4' is text. Convert it first: int.parse('4').
4. Swapping arguments of the same type: no error, just a wrong answer. Double-check the order, or use named parameters (Lecture 027).
Practice Questions
- Write
int multiply(int a, int b)that returns the product. void info(String name, int age). Isinfo(20, 'Ali')valid?- What does
fullName('Ali', 'Raza')return? - How many arguments must you pass to
void f(int a, int b, int c)? - Why can swapped
doublearguments be dangerous?
Practice Answers
int multiply(int a, int b) => a * b;- No. The first argument must be a
Stringand the second anint. Ali Raza- Exactly three.
- Dart can't tell they're swapped (same type), so you get a wrong result with no error.
Key Takeaways
- Positional parameters are matched by order.
- They're required: pass exactly as many arguments as there are parameters.
- Dart checks the count and the types, but not the meaning.
- Use clear parameter names; switch to named parameters when there are many.