Recap - Function Parameters
Part of the Fundamentals section of Coddy's Dart journey — lesson 76 of 94.
Challenge
EasyCreate 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
budgetwith a default value of 1000 - Take an optional named parameter
travelerswith a default value of 1 - Take an optional named parameter
includeInsurancewith 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
4Operators Part 2
Comparison OperatorsLogical ANDLogical ORLogical NOTType Test OperatorsRecap - Making Comparisons7Working with Strings
String ConcatenationString InterpolationMulti-line StringsString PropertiesBasic String Methods10Collections - Maps Basics
What are Maps?Creating a MapAccessing Values by KeyKey-Value PairsGetting Map SizeGetting KeysGetting ValuesChecking if a Key Exists13Null Safety In Depth
Understanding NullNullable TypesNon-Nullable TypesNull Assertion OperatorLate InitializationRecap - Handling Null Safely16Fundamentals Challenges
Challenge: List of calculationChallenge: Sum of numbersChallenge: Find product2Variables and Basic Data Types
What are Variables?StringsIntegers (int)Doubles (double)Booleans (bool)Type Inference with 'var'Final VariablesConstant VariablesNaming ConventionsBasic Null SafetyRecap - Declaring Variables8Control Flow - Loops
The 'for' LoopThe 'while' LoopThe 'do-while' LoopUsing 'break' in LoopsUsing 'continue' in LoopsRecap - Repeating Code3Operators Part 1
Arithmetic OperatorsInteger DivisionModulo OperatorIncrement and DecrementAssignment ShortcutsRecap - Simple Calculations6Control Flow - Decision Making
The 'if' StatementThe 'else' StatementThe 'else if' StatementRecap - Simple DecisionsNested 'if' StatementsThe 'switch' Statement9Collections - Lists Basics
What are Lists?Creating a ListAccessing by IndexGetting List LengthAdding ElementsRemoving ElementsChecking if a List is EmptyIterating Over a List12Functions Advanced
Optional Positional ParametersNamed ParametersRequired Named ParametersDefault Parameter ValuesRecap - Function Parameters15Project: Simple Calculator
Setting UpDeclaring Number