Menu
Coddy logo textTech

Recap - Simple Functions

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

challenge icon

Challenge

Easy

In 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:

  1. Complete the add function that takes two numbers and returns their sum
  2. Implement the subtract function using arrow syntax
  3. Create a multiply function that returns the product of two numbers
  4. Implement the divide function that handles division
  5. Create a printResult void function that formats and displays results
  6. Complete the calculate function 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