Exploring Kotlin Functions: Key Concepts and Examples
Learn Function Basics, Lambda Expressions, and Higher-Order Functions in Kotlin
Introduction
Functions are a set of statements designed to perform a specific task and are clubbed under one block for easier usage, and improved code readability.
Functions are declared using the fun
keyword in Kotlin :
fun hello1(){ //keyword followed by function-name
//statements
}
fun hello2(/*paremeters*/){ //parameters passed in paranthesis
//statements
}
fun hello3(/*paremeters*/) : returnType{
//statements
//return value
}
//Calling a function :
hello1()
hello2(/*pass parameters*/)
print(hello3(/*pass parameters*/))
var ans = hello3(/*pass parameters*/)
Parameters
Parameters are defined as parameters just by defining their names and the type, if the function has a return type it should be mentioned after a :
in the function header.
fun sayHello(
name : String,
age : Int
){ /*..*/ }
fun raisedTo(num : Int, power : Int) : Int { /* .. */ }
passing variable number of arguments to the function using the vararg
keyword:
fun add(vararg values : Int){
var sum = 0
for(i in values) sum += i //treated variable arguments as a collection
print(sum)
}
Default & Named Arguments
Parameters can be given default values using the =
operator, which are then taken by the parameter if that particular value is not passed during function call.
If we omit a variable during function call, or when the order of parameters in function header is different from the order of parameters in the function call, then named arguments can be used.
fun sayHello(name : String, age : Int = 0, genderMale : Boolean) {..}
//Calling Function :
sayHello("Vedant", 18, true)
sayHello("Vedant", true)
sayHello(genderMale = true, name = "Vedant") //using named arguements
Returning from a Function
The return type of a function should be explicitly mentioned in the function header, and is considered as Unit
by default.
fun sayHello() : Unit { }
fun sayHello() { }
fun sum(a: Int, b: Int) : Int {
return (a + b)
}
fun sum(a: Int, b: Int) : Int = a + b
Lambda & Higher Order Functions
Functions in Kotlin can be stored in variables, passed as an argument, and can be returned from another function. This is achieved through Lambda Expressions provided by Kotlin.
- Lambda Expression Syntax :
the parameters are defined before ->
and the return body is defined after ->
val myVar = { print("..") /*statements*/} //Unit Return Type By Default
myVar() //calling function
var square = {x: Int -> x*x} //square(3)
var sum : (Int, Int) -> Int = {X: Int, Y: Int -> X + Y}
var sum = {x: Int, y: Int -> x + y}
- A function that takes another function as parameter or returns another function is called Higher Order Function.
//passing a function as an argument
fun sum(a: Int, b: Int, callBack: () -> Unit){
//here the third parameter callback is a function
//that takes no parameter and has the returntype as Unit
//hence sum() is a higher order function
//STATEMENTS..
callback()
}
sum(10, 12, {print("Hi")}) //here lambda expression is passed
//if last parameter is a function, then lambda expression can be placed outside ()
//can omit () if only lambda expression passed
sum(10, 12){print("Hi")}
val callBack = {print("Hi")}
sum(10, 12, callBack)
it
: parameter name
fun cube(num: Int,
callBack: (msg: String) -> Unit){
//statements
callBack("Hi")
}
//Calling Function :
cube(3, {print(it)})
//it : Implicit name of parameter if a single parameter is present in function.
//it refers to -> "Hi" here
- returning from a function
fun cube(num : Int): (msg : String) -> Unit {
//statements
return {str : String -> print(str)}
}
Practice Code
fun main() {
val actions = listOf("title", "year", "author")
val prefix = "https://example.com/book-info"
val id = 5
//Method 1 :
val urls = actions.map{it -> "$prefix/$id/$it"}
println(urls)
//Method 2 :
val urlsHard = {act : List<String>, pre : String, idd : Int ->
val urlList : MutableList<String> = mutableListOf()
for(i in 0 until act.size){
urlList.add("$pre/$idd/${act[i]}")
}
urlList
}
println(urlsHard(actions, prefix, id))
}