0% found this document useful (0 votes)
2 views2 pages

Dart Typdefcalc

The document defines a typedef for mathematical operations that return a double value and implements four basic operations: addition, subtraction, multiplication, and division. It includes error handling for division by zero. The main function tests these operations and prints the results, including an error message for division by zero.

Uploaded by

soundhuk1806
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views2 pages

Dart Typdefcalc

The document defines a typedef for mathematical operations that return a double value and implements four basic operations: addition, subtraction, multiplication, and division. It includes error handling for division by zero. The main function tests these operations and prints the results, including an error message for division by zero.

Uploaded by

soundhuk1806
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

// Calculator typedef with a more descriptive name and return type

typedef Math Operation = double Function (double first, double second);

// Math operations that return results


double add (double a, double b) => a + b;w
double subtract (double a, double b) => a - b;
double multiply (double a, double b) => a * b;
double divide (double a, double b) {
if (b == 0) throw Argument Error ('Division by zero');
return a / b;
}

void main() {
// Define operations
Math Operation addition = add;
MathOperation subtraction = subtract;
MathOperation multiplication = multiply;
MathOperation division = divide;

// Test operations
try {
print("Addition: ${addition(10, 20)}");
print("Subtraction: ${subtraction(20, 10)}");
print("Multiplication: ${multiplication(5, 4)}");
print("Division: ${division(20, 5)}");

// Test division by zero error handling


print("Division by zero: ${division(10, 0)}");
} catch (e) {
print("Error: $e");
}
}

You might also like