Kotlin Functions

As we know, A function is a group of statements that together perform a task. ... A function declaration tells the compiler about a function's name, return type, and parameters. A function definition provides the actual body of the function. The Kotlin standard library provides numerous built-in functions that your program can call.

We will discuss here user defined function's. Below syntax show you to create function.

Syntax:
// here task is function name
fun task() {
	// body of function
}


We can define function in Kotlin using fun keyword, and define name of function accordingly. Let's create an example to work with function:


Example:
package com.theitbulls.kotlinex

fun main(args: Array<String>) {
	printArmstrong()
}

fun printArmstrong() {
	var num = 153
	
	var sum = 0
	
	while(num > 0) {
		var n = (num % 10)
		sum += (n * n * n)
		
		num /= 10
	}
	
	println("Armstrong sum: ${sum}")
}


Parameterize function: most important we need function with parameters, where we pass some parameter and perform some task. For example we create a program where a function will check, given parameter value is Armstrong number or not. Let's create an example:

Example:
package com.theitbulls.kotlinex

fun main(args: Array<String>) {
	printArmstrong(153)
	printArmstrong(157)
}

fun printArmstrong(num: Int) {
	var temp = num
	
	var sum = 0
	
	while(temp > 0) {
		var n = (temp % 10)
		sum += (n * n * n)
		
		temp /= 10
	}
	
	println("Is ${num} Armstrong Number: ${sum == num}")
}


Output:
Is 153 Armstrong Number: true
Is 157 Armstrong Number: false


Note: We can create more than one parameter in function separated by comma(,).


Function with return statement, now we will see how to create function with return some data, If a function does not returns any value than its return type is Unit. It is optional to specify the return type of function definition which does not returns any value.

Syntax of function with return type:

// after task() ': Int' define return type of function
fun task(): Int {
    // this method return 0 i.e. an integer
    return 0
}

Let's create an example, with return type boolean
package com.theitbulls.kotlinex

fun main(args: Array<String>) {
	if(printArmstrong(153)) {
		println("153 is an Armstrong number")
	} else {
		println("153 is not an Armstrong number")
	}
	
	if(printArmstrong(157)) {
		println("157 is an Armstrong number")
	} else {
		println("157 is not an Armstrong number")
	}
}

fun printArmstrong(num: Int): Boolean {
	var temp = num
	
	var sum = 0
	
	while(temp > 0) {
		var n = (temp % 10)
		sum += (n * n * n)
		
		temp /= 10
	}
	
	return sum == num
}


Generic Function

In Kotlin, we can create generic function too, so that we can reduce that code and increase code usability. For example if we create two type of array one is of String type and another is of Int type, and want to display array with same method. Then we create a generic method which will display array for both.

For doing this after fun keyword add <T> and in parameter also define type T. please go through below code:

Example:
package com.theitbulls.kotlinex

import java.util.ArrayList

fun main(args: Array<String>) {
	// create ArrayList object using empty constructor
	// creating arrayList of type String
	var arrayList1 = arrayListOf<String>()
	arrayList1.add("John")
	arrayList1.add("Marry")
	printList(arrayList1)
	
	// creating arrayList of type Int
	var arrayList3 = arrayListOf<Int>()
	arrayList3.add(23);
	arrayList3.add(11);
	arrayList3.add(99);
	printList(arrayList3)
}

fun <T> printList(arrList: ArrayList<T>) {
	println()
	for(item in arrList) {
		when(item) {
			is String -> println(item)
			is Int -> println(item)
		}
	}
}

Output:
John
Marry

23
11
99

In above code snippet, we use printList() function to println ArrayList of type String and Int.