Skip to content

Commit 0a47854

Browse files
committed
Kotlin Functions
1 parent 5bfed24 commit 0a47854

7 files changed

+153
-0
lines changed

src/17_functions_basics.kt

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
2+
/*
3+
* FUNCTIONS Basics
4+
* */
5+
fun main(args: Array<String>) {
6+
7+
8+
var sum = add(2, 4)
9+
10+
println("Sum is " + sum)
11+
}
12+
13+
fun add(a: Int, b: Int): Int {
14+
return a + b
15+
}

src/18_functions_as_expressions.kt

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
2+
3+
/*
4+
* FUNCTIONS as Expressions
5+
* */
6+
fun main(args: Array<String>) {
7+
8+
var largeValue = max(4, 6)
9+
10+
println("The greater number is $largeValue")
11+
}
12+
13+
fun max(a: Int, b: Int): Int
14+
= if (a > b) {
15+
println("$a is greater")
16+
a
17+
} else {
18+
println("$b is greater")
19+
b
20+
}

src/21_named_parameters.kt

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
2+
3+
/*
4+
* Named Parameters
5+
* */
6+
fun main(args: Array<String>) {
7+
8+
var result = findTheVolume(breadth = 2, length = 3)
9+
print(result)
10+
}
11+
12+
fun findTheVolume(length: Int, breadth: Int, height: Int = 10): Int {
13+
14+
return length * breadth * height
15+
}

src/22_extension_function_one.kt

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
2+
/*
3+
* Extension Functions: EXAMPLE ONE
4+
* */
5+
fun main(args: Array<String>) {
6+
7+
var student = Studentt()
8+
println("Pass status: " + student.hasPassed(57))
9+
10+
println("Scholarship Status: " + student.isScholar(57))
11+
}
12+
13+
fun Studentt.isScholar(marks: Int): Boolean {
14+
return marks > 95
15+
}
16+
17+
class Studentt { // OUR OWN CLASS
18+
19+
fun hasPassed(marks: Int): Boolean {
20+
return marks > 40
21+
}
22+
}

src/23_extension_function_two.kt

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
2+
/*
3+
* Extension Functions: EXAMPLE TWO
4+
* */
5+
fun main(args: Array<String>) {
6+
7+
var str1: String = "Hello "
8+
var str2: String = "World"
9+
10+
var str3: String = "Hey "
11+
12+
println(str3.add(str1, str2))
13+
14+
val x: Int = 6
15+
val y: Int = 10
16+
17+
val greaterVal = x.greaterValue(y)
18+
19+
println(greaterVal)
20+
}
21+
22+
fun String.add(s1: String, s2: String): String {
23+
24+
return this + s1 + s2
25+
}
26+
27+
fun Int.greaterValue(other: Int): Int {
28+
29+
if (this > other)
30+
return this
31+
else
32+
return other
33+
}

src/24_infix_function.kt

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
2+
/*
3+
* INFIX FUNCTIONS
4+
* */
5+
fun main(args: Array<String>) {
6+
7+
val x: Int = 6
8+
val y: Int = 10
9+
10+
val greaterVal = x findGreaterValue y // x.findGreaterValue(y)
11+
12+
println(greaterVal)
13+
}
14+
15+
infix fun Int.findGreaterValue(other: Int): Int { // INFIX and Extension Func
16+
17+
if (this > other)
18+
return this
19+
else
20+
return other
21+
}
22+
23+
/*
24+
* 1. All INFIX Functions are Extension functions
25+
* But all Extension functions are not INFIX
26+
* 2. INFIX Functions just have ONE PARAMETER
27+
* */

src/25_tailrec_function.kt

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import java.math.BigInteger
2+
3+
/*
4+
* Tailrec Function : Recursive Functions
5+
* -> Prevents Stackoverflow Exception
6+
*
7+
* Fibonacci Series
8+
* 0 1 1 2 3 5 8 13 21 ......
9+
* */
10+
fun main(args: Array<String>) {
11+
12+
println(getFibonacciNumber(10000, BigInteger("1"), BigInteger("0")))
13+
}
14+
15+
tailrec fun getFibonacciNumber(n: Int, a: BigInteger, b: BigInteger): BigInteger {
16+
17+
if (n == 0)
18+
return b
19+
else
20+
return getFibonacciNumber(n - 1, a + b, a)
21+
}

0 commit comments

Comments
 (0)