forked from smartherd/KotlinTutorial
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
153 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
|
||
/* | ||
* FUNCTIONS Basics | ||
* */ | ||
fun main(args: Array<String>) { | ||
|
||
|
||
var sum = add(2, 4) | ||
|
||
println("Sum is " + sum) | ||
} | ||
|
||
fun add(a: Int, b: Int): Int { | ||
return a + b | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
|
||
|
||
/* | ||
* FUNCTIONS as Expressions | ||
* */ | ||
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
|
||
|
||
/* | ||
* Named Parameters | ||
* */ | ||
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
|
||
/* | ||
* Extension Functions: EXAMPLE ONE | ||
* */ | ||
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
|
||
/* | ||
* Extension Functions: EXAMPLE TWO | ||
* */ | ||
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
|
||
/* | ||
* INFIX FUNCTIONS | ||
* */ | ||
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 | ||
} | ||
|
||
/* | ||
* 1. All INFIX Functions are Extension functions | ||
* But all Extension functions are not INFIX | ||
* 2. INFIX Functions just have ONE PARAMETER | ||
* */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import java.math.BigInteger | ||
|
||
/* | ||
* Tailrec Function : Recursive Functions | ||
* -> Prevents Stackoverflow Exception | ||
* | ||
* Fibonacci Series | ||
* 0 1 1 2 3 5 8 13 21 ...... | ||
* */ | ||
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) | ||
} |