-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctions.html
More file actions
251 lines (213 loc) · 9.55 KB
/
functions.html
File metadata and controls
251 lines (213 loc) · 9.55 KB
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
<!DOCTYPE html>
<html lang="en">
<head>
<title>Functions Kotlin</title>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css" type="text/css">
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Merriweather+Sans:400,300,700" type="text/css">
<link rel="stylesheet" href="static/app.css" type="text/css">
<link rel="shortcut icon" href="static/logo.png" />
<meta name="theme-color" content="#111111">
<meta name="og:type" content="website">
<meta name="og:title" content="MoraSoftware.github.io">
<meta name="og:description" content="Mora Software">
<meta name="og:site_name" content="Mora Software">
<meta name="og:url" content="{{ site.url }}">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
<div class="block">
<form action="index.html">
<input type="image" src="static/white.jpg" />
</form>
</div>
<div class="block">
<h2>Functions Kotlin</h2>
</div>
<ul class="nav nav-tabs">
<li class="active"><a data-toggle="tab" href="#functions_basics">Functions basics</a></li>
<li><a data-toggle="tab" href="#functions_as_expressions">Functions as expressions</a></li>
<li><a data-toggle="tab" href="#named_parameters">Named parameters</a></li>
<li><a data-toggle="tab" href="#extension_function_one">Extension function one</a></li>
<li><a data-toggle="tab" href="#extension_function_two">Extension function two</a></li>
<li><a data-toggle="tab" href="#infix_function">Infix function</a></li>
<li><a data-toggle="tab" href="#tailrec_function">Tailrec function</a></li>
<li><a data-toggle="tab" href="#vararg_function">Vararg function</a></li>
</ul>
<div class="tab-content">
<div id="functions_basics" class="tab-pane fade in active">
<h3>Functions basics</h3>
<xmp>
fun main(args: Array<String>) {
var sum = add(2, 4)
println("Sum is " + sum)
}
fun add(a: Int, b: Int): Int {
return a + b
}
</xmp>
</div>
<div id="functions_as_expressions" class="tab-pane fade">
<h3>Functions as expressions</h3>
<xmp>
fun main(args: Array<String>) {
var largeValue = max(4, 6)
println("The greater number is $largeValue")
}
fun max(a: Int, b: Int): Int = if (a > b) {
println("$a is greater")
a
} else {
println("$b is greater")
b
}
</xmp>
</div>
<div id="named_parameters" class="tab-pane fade">
<h3>Named parameters</h3>
<xmp>
fun main(args: Array<String>) {
var result = findTheVolume(breadth = 2, length = 3)
print(result)
}
fun findTheVolume(length: Int, breadth: Int, height: Int = 10): Int {
return length * breadth * height
}
</xmp>
</div>
<div id="extension_function_one" class="tab-pane fade">
<h3>Extension function one</h3>
<xmp>
fun main(args: Array<String>) {
var student = Studentt()
println("Pass status: " + student.hasPassed(57))
println("Scholarship Status: " + student.isScholar(57))
}
fun Studentt.isScholar(marks: Int): Boolean {
return marks > 95
}
class Studentt { // OUR OWN CLASS
fun hasPassed(marks: Int): Boolean {
return marks > 40
}
}
</xmp>
<hr>
<p>Add new function to the classes</p>
<p>* Can "addd" functions to a class without declaring it</p>
<p>* The new functions added behaves like static</p>
<p>Few Properties</p>
<p>* They can become part of you onw class</p>
<p> * Example: Student</p>
<p>* They can become part of preddfined clases</p>
<p> * Stringm Int, Array...</p>
<p>Benefits</p>
<p>* Reduces code</p>
<p>* Code is much cleaner and easy to read</p>
</div>
<div id="extension_function_two" class="tab-pane fade">
<h3>Extension function two</h3>
<xmp>
fun main(args: Array<String>) {
var str1: String = "Hello "
var str2: String = "World"
var str3: String = "Hey "
println(str3.add(str1, str2))
val x: Int = 6
val y: Int = 10
val greaterVal = x.greaterValue(y)
println(greaterVal)
}
fun String.add(s1: String, s2: String): String {
return this + s1 + s2
}
fun Int.greaterValue(other: Int): Int {
if (this > other)
return this
else
return other
}
</xmp>
<hr>
<p>Add new function to the classes</p>
<p>* Can "addd" functions to a class without declaring it</p>
<p>* The new functions added behaves like static</p>
<p>Few Properties</p>
<p>* They can become part of you onw class</p>
<p> * Example: Student</p>
<p>* They can become part of preddfined clases</p>
<p> * Stringm Int, Array...</p>
<p>Benefits</p>
<p>* Reduces code</p>
<p>* Code is much cleaner and easy to read</p>
<a href="https://www.youtube.com/watch?v=6oFvZKF6KKg&list=PLlxmoA0rQ-LwgK1JsnMsakYNACYGa1cjR&index=32"><img src="static/youtube.png"></a>
<p></p>
</div>
<div id="infix_function" class="tab-pane fade">
<h3>Infix function</h3>
<xmp>
fun main(args: Array<String>) {
val x: Int = 6
val y: Int = 10
val greaterVal = x findGreaterValue y // x.findGreaterValue(y)
println(greaterVal)
}
infix fun Int.findGreaterValue(other: Int): Int { // INFIX and Extension Func
if (this > other)
return this
else
return other
}
</xmp>
<hr>
<p>Infix Functions can be a Member Function or Extension Function</p>
<p>They have SINGLE Parameter</p>
<p>They have prefix of "infix"</p>
<a href="https://www.youtube.com/watch?v=ikUF1z_WOZc&list=PLlxmoA0rQ-LwgK1JsnMsakYNACYGa1cjR&index=33"><img src="static/youtube.png"></a>
<p></p>
</div>
<div id="tailrec_function" class="tab-pane fade">
<h3>Tailrec function</h3>
<p>Fibonacci Series</p>
<p>0 1 1 2 3 5 8 13 21 ......</p>
<p>Good example to make a recursive function</p>
<xmp>
fun main(args: Array<String>) {
println(getFibonacciNumber(10000, BigInteger("1"), BigInteger("0")))
}
tailrec fun getFibonacciNumber(n: Int, a: BigInteger, b: BigInteger): BigInteger {
if (n == 0)
return b
else
return getFibonacciNumber(n - 1, a + b, a)
}
</xmp>
<hr>
<p>Tailrec Function : Recursive Functions</p>
<p>* Prevents Stackoverflow Exception</p>
</div>
<div id="vararg_function" class="tab-pane fade">
<h3>Vararg</h3>
<xmp>
fun main(args: Array<String>) {
fun foo(vararg strings: String) {
for(letter in strings) {
println(letter)
}
}
foo(strings = *arrayOf("a", "b", "c"))
}
</xmp>
<hr>
<p>Variable number of arguments (vararg) </p>
<p>can be passed in the named form by using the spread operator.</p>
<a href="https://www.youtube.com/watch?v=HaoggPuyoqg"><img src="static/youtube.png"></a>
</div>
</div>
</div>
</body>
</html>