Kotlin Functions

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
 

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
  1. fun main(args: Array < String > ) {  
  2.     var number = 25  
  3.     var result = Math.sqrt(number.toDouble())  
  4.     print("Square root of $number is $result")  
  5. }  
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
  1. fun main(args: Array < String > ) {  
  2.     val result = sum(5, 6)  
  3.     print(result)  
  4. }  
  5. fun sum(number1: Int, number2: Int): Int {  
  6.     val add = number1 + number2  
  7.     return add  
  8. }  
Output
 
11 

 
Recursion Function

 
Recursion function is a function which calls itself continuously.
 
Example
  1. fun main(args: Array < String > ) {  
  2.     val number = 5  
  3.     val result: Long  
  4.     result = factorial(number)  
  5.     println("Factorial of $number = $result")  
  6. }  
  7. fun factorial(n: Int): Long {  
  8.     return  
  9.         if (n == 1) {  
  10.             n.toLong()  
  11.         }  
  12.     else {  
  13.         n * factorial(n - 1)  
  14.     }  
  15. }  
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
  1. fun main(args: Array < String > ) {  
  2.     val mylambda: (String) - > Unit = {  
  3.         s: String - > print(s)  
  4.     }  
  5.     val v: String = "www.ittrident.com"  
  6.     mylambda(v)  
  7.  
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
  1. fun main(args: Array < String > ) {  
  2.     inlineFunction({  
  3.         println("www.ittrident.com")  
  4.     })  
  5. }  
  6. inline fun inlineFunction(myFun: () - > Unit) {  
  7.     myFun()  
  8.     print("www.csharpcorner.com")  
  9. }  
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
  1. fun main(args: Array < String > ) {  
  2.     run(‘Abu’, 'shithik')  
  3. }  
  4. fun run(first: char = ‘A’, last: Char = 'S') {  
  5.     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
  1. fun main(args: Array < String > ) {  
  2.     run(first = 'abu')  
  3. }  
  4. fun run(num: Int = 100, latter: Char = 'x') {  
  5.     print(" The parameter in function definition $num and $first")  
  6. }  
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
  1. fun myFun(org: String, portal: String, fn: (String, String) - > String): Unit {  
  2.     val result = fn(org, portal)  
  3.     println(result)  
  4. }  
  5. fun main(args: Array < String > ) {  
  6.     val fn: (String, String) - > String = {  
  7.         org,  
  8.         portal - > "$org develop $portal"  
  9.     }  
  10.     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. 
 
Next in this seires: Kotlin Strings and Arrays