Menu
Coddy logo textTech

Recap - Function Parameters

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

challenge icon

Challenge

Easy

Create a TravelPlanner function that helps users plan their trips. The function should:

  • Take a required named parameter destination (String)
  • Take a required named parameter durationInDays (int)
  • Take an optional named parameter budget with a default value of 1000
  • Take an optional named parameter travelers with a default value of 1
  • Take an optional named parameter includeInsurance with a default value of false

The function should return a formatted string with the trip details in this exact format:

Trip to [destination] planned:
- Duration: [durationInDays] days
- Travelers: [travelers]
- Budget per person: $[calculated budget]
- Insurance: [Yes/No]

The calculated budget should be the total budget divided by the number of travelers, rounded to the nearest integer.

Try it yourself

// Complete the TravelPlanner function below
String TravelPlanner({
  // TODO: Add required named parameters for destination and durationInDays
  // TODO: Add optional named parameter for budget with default value 1000
  // TODO: Add optional named parameter for travelers with default value 1
  // TODO: Add optional named parameter for includeInsurance with default value false
}) {
  // TODO: Calculate budget per person by dividing budget by travelers
  // Hint: Use .round() to get a whole number
  
  // TODO: Return formatted trip details using multi-line string
  // Hint: Use triple quotes ''' or """ for multi-line strings
  // The output should include:
  // - Destination
  // - Duration in days
  // - Number of travelers
  // - Budget per person (with $ symbol)
  // - Insurance (Yes/No)
  
}

void main() {
  // Test with only required parameters
  print(TravelPlanner(destination: 'Paris', durationInDays: 7));
  
  // Test with all parameters
  print(TravelPlanner(
    destination: 'Tokyo',
    durationInDays: 10,
    budget: 5000,
    travelers: 3,
    includeInsurance: true,
  ));
}

All lessons in Fundamentals