1
+ //outer function that calcultaes the price of an item
2
+ double calculatePrice (double dollar){
3
+
4
+ // inner function that converts the item to lira
5
+ double convertDollarToLira (double d) {
6
+ return d * 18.64 ;
7
+ }
8
+ //returns the price of the item
9
+ return 100 + convertDollarToLira (dollar);//adds 100 lira for taxes
10
+ }
11
+
12
+ //scope of local variables
13
+ void scopeExample (){
14
+
15
+ //scope of the local variables
16
+ int localVar= 10 ;//mainVar's scope is inside scopeExample function
17
+
18
+ //loopVar's scope is only inside the loop
19
+ for (int loopVar= 0 ; loopVar< 3 ; loopVar++ )
20
+ {
21
+ print ('loopVar is: $loopVar ' );
22
+ print ('localVar is: $localVar ' );
23
+ }
24
+ //print('loopVar is: $loopVar'); error!
25
+ print ('localVar is: $localVar ' );
26
+ //print('fooVar is: $fooVar'); error!
27
+ }
28
+ void foo (){
29
+ var fooVar;
30
+ }
31
+
32
+ //example function demonstrating pass by value
33
+ void newString (String param1){
34
+ param1= "ENG 401" ;
35
+ }
36
+
37
+ //required positional parameter
38
+ int product (int x, int y){
39
+
40
+ return x * y;
41
+ }
42
+
43
+ //positional parameters, second one is optional
44
+ void printNames (String name1,[String ? name2]) {
45
+ print ("First name is: $name1 " );
46
+
47
+ if (name2 != null ){
48
+ print ("Second name is: $name2 " );
49
+ }
50
+ }
51
+
52
+ //required positional parameters
53
+ int subtraction ({required int x, required int y}) {
54
+ return x- y;
55
+ }
56
+
57
+ //named parameters, second one is optional
58
+ void printMovies ({required String movie1, String ? movie2}) {
59
+ print ("First movie is: $movie1 " );
60
+
61
+ if (movie2 != null ){
62
+ print ("Second movie is: $movie2 " );
63
+ }
64
+ }
65
+
66
+ //example of a closure
67
+ //a function that multiplies two numbers
68
+ Function mul (num firstOperand) {
69
+ return (num x) => firstOperand * x;
70
+ }
71
+
72
+ void main (){
73
+ double dollarPrice= 15 ;//price of the product in dollars
74
+ double liraPrice;//price of the product in liras
75
+
76
+ //calculate the price of the product in liras using nested suprograms
77
+ liraPrice= calculatePrice (dollarPrice);
78
+
79
+ //print the price to the console
80
+ print ('The price of the product in lira is : $liraPrice ' );
81
+
82
+ //calling the scopeExample function
83
+ scopeExample ();
84
+
85
+ //example of pass by value in Dart
86
+ //defining a new string
87
+ String courseName= "CS 319" ;
88
+ newString (courseName);
89
+ print ('The course name is : $courseName ' );//course name is unchanged
90
+
91
+
92
+ //calling a function with required positional parameters
93
+ int result= product (2 ,3 );
94
+ print (result);
95
+ //product(2); too few positional arguments!
96
+
97
+ printNames ("Elif" ,"Selin" );
98
+ printNames ("Ayşe" );
99
+
100
+ //calling a function with required named parameters
101
+ int sub = subtraction (x: 5 ,y: 1 );
102
+ //sub=subtraction(x:5); error! an argument for y should be given
103
+ print ('$sub ' );
104
+
105
+ //calling a function with named parameters
106
+ //second parameter is optional while the first is required
107
+ printMovies (movie1: "Forrest Gump" , movie2: "Godfather" );
108
+ printMovies (movie1: "Triangle of Sadness" );
109
+
110
+ //create a function that multiplies 10
111
+ var secondOperand = mul (10 );//mul closes over secondOperand
112
+
113
+ //pass 7 to that function
114
+ print (secondOperand (7 ));
115
+
116
+
117
+ }
0 commit comments