On this page10 sections
  1. Prerequisites
  2. Learning Objectives
  3. 1. Required Positional Parameters
  4. 2. Order Matters
  5. 3. When to Use Positional Parameters
  6. Code Examples
  7. Common Beginner Mistakes
  8. Practice Questions
  9. Practice Answers
  10. Key Takeaways

Lecture 026: Required & Positional Parameters

Day10: Functions & Parameters
Lecture026 of 057
Estimated video length12 min
TargetDart 3.12
LevelBeginner

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:

dart
int add(int a, int b) {
  return a + b;
}

print(add(3, 4));   // 7

These 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.

dart
void printBill(String item, int quantity, double price) {
  print('$quantity × $item = Rs ${quantity * price}');
}

printBill('Notebook', 3, 120);   // 3 × Notebook = Rs 360.0

2. Order Matters

Dart matches by position, not by meaning:

dart
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 fitBetter 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

dart
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

code
BMI: 22.9 (Normal)
Swapped arguments: 0.00036

Both 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

dart
int add(int a, int b) => a + b;
print(add(3));
code
Error: Too few positional arguments: 2 required, 1 given.

2. Too many arguments

dart
print(add(1, 2, 3));
code
Error: Too many positional arguments: 2 allowed, but 3 found.

3. The wrong type

dart
print(add(3, '4'));
code
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

  1. Write int multiply(int a, int b) that returns the product.
  2. void info(String name, int age). Is info(20, 'Ali') valid?
  3. What does fullName('Ali', 'Raza') return?
  4. How many arguments must you pass to void f(int a, int b, int c)?
  5. Why can swapped double arguments be dangerous?

Practice Answers

  1. int multiply(int a, int b) => a * b;
  2. No. The first argument must be a String and the second an int.
  3. Ali Raza
  4. Exactly three.
  5. 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.