Introduction
In this part, we are going to learn about Kotlin Functions which play a major role in Kotlin programming. Those who new to Kotlin and willing to learn, go through this article series starting here: for the
Introduction to Kotlin.
Kotlin Function
A function in Kotlin is used to break a program into different sub-modules. It makes code reusable and makes the program more manageable. In Kotlin, functions are declared using fun keyword.
Types Of Kotlin Function
- Standard Library Function
- User-Defined Function
- Recursion Function
- Lambda Function
- Inline Function
- Default Argument Function
- Named Argument Function
- Higher-Order Function
Standard Library Function
Kotlin Standard library functions are built-in library functions which are implicitly present in the library and available for use.
Example
- fun main(args: Array < String > ) {
- var number = 25
- var result = Math.sqrt(number.toDouble())
- print("Square root of $number is $result")
- }
Output
The square root of 25 is 5.
User-Defined Library Function
A user-defined function is a function which is created by the user. User-defined functions take the parameter(s), perform an action, and return the result of that action as a value.
Example
- fun main(args: Array < String > ) {
- val result = sum(5, 6)
- print(result)
- }
- fun sum(number1: Int, number2: Int): Int {
- val add = number1 + number2
- return add
- }
Output
11
Recursion Function
Recursion function is a function which calls itself continuously.
Example
- fun main(args: Array < String > ) {
- val number = 5
- val result: Long
- result = factorial(number)
- println("Factorial of $number = $result")
- }
- fun factorial(n: Int): Long {
- return
- if (n == 1) {
- n.toLong()
- }
- else {
- n * factorial(n - 1)
- }
- }
Output
Factorial of 5 = 120
Lambda Function
Lambda is a high-level function that drastically reduces the boilerplate code while declaring a function and defining the same. Kotlin allows you to define your own lambda. Lambda is a function which has no name. Lambda is defined with a curly brace {} which take variable as a parameter (if any) and body of the function. The body of the function is written after the variable (if any) followed by -> operator.
Example
- fun main(args: Array < String > ) {
- val mylambda: (String) - > Unit = {
- s: String - > print(s)
- }
- val v: String = "www.ittrident.com"
- mylambda(v)
- }
Output
www.ittrident.com
Inline Function
An inline function is declared with a keyword inline. The inline function tells the compiler to copy parameters and functions to the call site. The virtual function or local function cannot be declared as inline.
Example
- fun main(args: Array < String > ) {
- inlineFunction({
- println("www.ittrident.com")
- })
- }
- inline fun inlineFunction(myFun: () - > Unit) {
- myFun()
- print("www.csharpcorner.com")
- }
Output
www.ittrident.com
www.csharpcorner.com
Default Argument Function
If a function is called without passing any argument than default argument are used as a parameter of the function definition. And when a function is called using argument, then the passing argument is used as a parameter in the function definition.
Example
- fun main(args: Array < String > ) {
- run(‘Abu’, 'shithik')
- }
- fun run(first: char = ‘A’, last: Char = 'S') {
- print("parameter in function definition $first and $last")
- }
Output
parameter in function definition Abu and Shithik
Named Argument Function
A named argument is an argument in which we define the name of an argument in the function call. The name defined to the argument of function call checks the name in the function definition and assign to it.
Example
- fun main(args: Array < String > ) {
- run(first = 'abu')
- }
- fun run(num: Int = 100, latter: Char = 'x') {
- print(" The parameter in function definition $num and $first")
- }
Output
The parameter in function definition 5 and Abu
Higher-Order Function
High order function (Higher level function) is a function which accepts a function as a parameter or returns a function or can do both. This means, instead of passing Int, String, or other types as a parameter in a function we can pass a function as a parameter in other function.
Example
- fun myFun(org: String, portal: String, fn: (String, String) - > String): Unit {
- val result = fn(org, portal)
- println(result)
- }
- fun main(args: Array < String > ) {
- val fn: (String, String) - > String = {
- org,
- portal - > "$org develop $portal"
- }
- myFun("www.ittrident.com", "Android Application", fn)
- }
Output
ww.ittrident.com develop Android Application
Conclusion
In this article, we learned about various Kotlin functions. In the next part, we will learn about Array, String, Exception Handling.