Recap - Simple Functions
Part of the Fundamentals section of Coddy's Dart journey — lesson 71 of 94.
Challenge
EasyIn this recap challenge, you'll create a simple calculator program that demonstrates your understanding of Dart functions. You'll implement several functions using different techniques you've learned.
Your task is to:
- Complete the
addfunction that takes two numbers and returns their sum - Implement the
subtractfunction using arrow syntax - Create a
multiplyfunction that returns the product of two numbers - Implement the
dividefunction that handles division - Create a
printResultvoid function that formats and displays results - Complete the
calculatefunction that performs operations based on an operator symbol
The program should display the results of various calculations exactly as shown in the expected output.
Try it yourself
// Complete the add function that returns the sum of two numbers
int add(int a, int b) {
// Your code here
}
// Implement the subtract function using arrow syntax
int subtract(int a, int b) /* Your code here */
// Create a multiply function that returns the product of two numbers
// Your code here
// Implement the divide function that returns the result of a / b
double divide(int a, int b) {
// Your code here
}
// Create a void function that prints the result in the format:
// "Result of {operation}: {result}"
void printResult(String operation, dynamic result) {
// Your code here
}
// Complete the calculate function that performs different operations
// based on the operator provided (+, -, *, /)
dynamic calculate(int a, int b, String operator) {
// Your code here
}
void main() {
// Test your functions
printResult('5 + 3', calculate(5, 3, '+'));
printResult('10 - 4', calculate(10, 4, '-'));
printResult('6 * 7', calculate(6, 7, '*'));
printResult('20 / 4', calculate(20, 4, '/'));
}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