Menu
Coddy logo textTech

Functions with Parameters

Part of the Fundamentals section of Coddy's Dart journey — lesson 67 of 94.

Positional parameters allow functions to accept input values that can be used inside the function body. They're called "positional" because their order matters.

Create a function with a positional parameter:

void greet(String name) {
  print('Hello, $name!');
}

void main() {
  greet('Dart');
}

After executing the above code, the output will be:

Hello, Dart!

Create a function with multiple positional parameters:

void printSum(int a, int b) {
  int sum = a + b;
  print('The sum of $a and $b is $sum');
}

void main() {
  printSum(5, 3);
}

After executing the above code, the output will be:

The sum of 5 and 3 is 8
challenge icon

Challenge

Beginner

In this challenge, you'll practice creating a function with positional parameters. Positional parameters are values you pass to a function in a specific order.

Complete the calculateArea function that takes two parameters: length and width. The function should calculate and return the area of a rectangle (length × width).

Expected output:

The area of the rectangle is: 50

Cheat sheet

Positional parameters allow functions to accept input values in a specific order.

Function with one positional parameter:

void greet(String name) {
  print('Hello, $name!');
}

void main() {
  greet('Dart');
}

Function with multiple positional parameters:

void printSum(int a, int b) {
  int sum = a + b;
  print('The sum of $a and $b is $sum');
}

void main() {
  printSum(5, 3);
}

Try it yourself

void main() {
  // These values are already defined for you
  double length = 10.0;
  double width = 5.0;
  
  // TODO: Call the calculateArea function with length and width parameters
  // and store the result in the variable 'area'
  double area = 0.0;
  
  // This will display the result
  print('The area of the rectangle is: $area');
}

// TODO: Complete this function to calculate and return the area of a rectangle
// The function should take two parameters: length and width
double calculateArea() {
  // Your code here
  
}
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Fundamentals