Skip to content

Commit

Permalink
Kotlin Functions
Browse files Browse the repository at this point in the history
  • Loading branch information
smartherd committed Sep 16, 2018
1 parent 5bfed24 commit 0a47854
Show file tree
Hide file tree
Showing 7 changed files with 153 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/17_functions_basics.kt
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
}
20 changes: 20 additions & 0 deletions src/18_functions_as_expressions.kt
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
}
15 changes: 15 additions & 0 deletions src/21_named_parameters.kt
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
}
22 changes: 22 additions & 0 deletions src/22_extension_function_one.kt
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
}
}
33 changes: 33 additions & 0 deletions src/23_extension_function_two.kt
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
}
27 changes: 27 additions & 0 deletions src/24_infix_function.kt
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
* */
21 changes: 21 additions & 0 deletions src/25_tailrec_function.kt
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)
}

0 comments on commit 0a47854

Please sign in to comment.