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 8Challenge
BeginnerIn 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: 50Cheat 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
}This lesson includes a short quiz. Start the lesson to answer it and track your progress.
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 Code11Functions Basics
What are Functions?Defining a Basic FunctionCalling a FunctionFunctions with ParametersFunctions Returning ValuesThe 'void' KeywordArrow SyntaxRecap - Simple Functions3Operators 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