Functions Kotlin

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 }

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 }

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 }

Extension function 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 } }

Add new function to the classes

* Can "addd" functions to a class without declaring it

* The new functions added behaves like static

Few Properties

* They can become part of you onw class

* Example: Student

* They can become part of preddfined clases

* Stringm Int, Array...

Benefits

* Reduces code

* Code is much cleaner and easy to read

Extension function 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 }

Add new function to the classes

* Can "addd" functions to a class without declaring it

* The new functions added behaves like static

Few Properties

* They can become part of you onw class

* Example: Student

* They can become part of preddfined clases

* Stringm Int, Array...

Benefits

* Reduces code

* Code is much cleaner and easy to read

Infix function

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 }

Infix Functions can be a Member Function or Extension Function

They have SINGLE Parameter

They have prefix of "infix"

Tailrec function

Fibonacci Series

0 1 1 2 3 5 8 13 21 ......

Good example to make a recursive function

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) }

Tailrec Function : Recursive Functions

* Prevents Stackoverflow Exception

Vararg

fun main(args: Array<String>) { fun foo(vararg strings: String) { for(letter in strings) { println(letter) } } foo(strings = *arrayOf("a", "b", "c")) }

Variable number of arguments (vararg)

can be passed in the named form by using the spread operator.