-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDart_Functions.dart
54 lines (46 loc) · 1.11 KB
/
Dart_Functions.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
//This Dart program defines functions for each task
//and includes a main function for testing.
// Task 1
int addTwo(int a, int b) {
return a + b;
}
// Task 2
int subtractTwo(int a, int b) {
return a - b;
}
// Task 3
int multiplyTwo(int a, int b) {
return a * b;
}
// Task 4
double divideTwo(double a, double b) {
if (b != 0) {
return a / b;
} else {
print("Error: Cannot divide by zero.");
return double.nan;
}
}
// Task 5
int stringLength(String str) {
return str.length;
}
// Task 6
dynamic getFirstElement(List list) {
if (list.isNotEmpty) {
return list[0];
} else {
print("Error: The list is empty.");
return null;
}
}
void main() {
// Testing the functions
print("Task 1 - Add Two Numbers: ${addTwo(6, 3)}");
print("Task 2 - Subtract Two Numbers: ${subtractTwo(6, 3)}");
print("Task 3 - Multiply Two Numbers: ${multiplyTwo(6, 3)}");
print("Task 4 - Divide Two Numbers: ${divideTwo(6, 3)}");
print("Task 5 - String Length: ${stringLength("PLP Training")}");
List numbers = [1, 2, 3, 4, 5];
print("Task 6 - Get First Element of List: ${getFirstElement(numbers)}");
}