AndroidBites #Metas: fun in functions?

Let's explore what is the meaning of functions as first-class citizen in Kotlin

Image for post
Image for post

Hi! Welcome to my ongoing series of AndroidBites where we discuss bite-size information on some core concepts of programming, Today we are exploring some fun in the functions of Kotlin,

Kotlin has functions as a first-class citizen if you're wondering what that means then it's simply a function is primitive type in kotlin, which means it can be stored into variables and data structures, passed as arguments to and returned from other functions, i.e support for Higher-Order Functions.

Structure of a function

Image for post
Image for post
basic structure of a function

A function consists of some components :

  1. fun as keyword to declare the start of the function.
  2. Function name which is used for its invocation.
  3. Some arguments/parameters as a dependency for its operations.
  4. A Return type to return a result of the computation.
  5. A function body { . . . }that computes the result.
  6. A Signature of the function: if you observe closely, you can see another construct of the function i.e (A,B)->C , where A and B is the type of parameter, and C is the return type, this defines the signature of the function and Kotlin uses it to provide high order functionality.

Fun into variables.

  1. All function types have a parenthesized parameter types list and a return type. (A,B)->C , where A and B is the type of parameter, and C is the return type


val sum = fun(param1:Int,param2:Int):Int {return param1+param2 }

val shortHandSum: (Int,Int)->Int = {p1,p2 -> p1+p2 }

2. The parameter types list may be empty, as in ()->A. The Unit return type cannot be omitted.


val hello = fun():Unit {println("Hello functions") }

val shortHello: ()->Unit = println("Hello functions")

3. Function types can have an additional receiver type, which is specified before a dot in the notation: the type A.(B)->C


val printInt = fun Int.():Unit { println(this) }

val shortPrintInt : Int.()->Unit = { println(this) }

4. Suspending functions start with a suspend modifier in the notation, such as suspend ()->Unit OR suspend A.(B)->C

suspend ()->Unit is not supported for variable holding, but shortHand way is supported.


val plus = suspend { fun Int.(other:Int):Unit { println(this+other) } }

val suspendSum : suspend (Int,Int)->Int = {p1,p2-> println(this+that) }

Fun into Functions.

function returning a function signature can be used for a function returning a function.

var functionA: ((Int) -> Int) = {
println("func a")
var functionB: (Int) -> ((Int) -> Int) = {
println("func b")
// function call
val resultA = functionB(1) // return functionA
val result = resultA(5) // return 2
val shortCall = functionB(1)(5) // return 2

passing a function signature into a function parameter is used for passing a function into a function

val sum = { p1:Int,p2:Int-> p1+p2 }
sub= { p1:Int,p2:Int-> p1-p2 }
fun calculate(p1: Int, p2: Int, operation: (Int, Int) -> Int):Int {
return operation.invoke(p1,p2)
//function call
calculate(2,4){ p1 , p2-> sum22.invoke(p1,p2) }

the type of (Int->((Int)->Unit)) =(Int)->(Int)->Unit) != ((Int)->(Int))->Unit

Function type

The way your storing function can define what kind of type it would end up in.

  • function declared directly into the variable using function body { . . . } are called Lambda functions. exampleval sum = {p1,p2 -> p1+p2 }
  • function declaration, whose just name is omitted out end up Anonymous function. an example val sum = fun(p1:Int,p2:Int):Int = p1 + p2
  • function that uses additional receivers is called Extension function. example val printInt = fun Int.():Unit { println(this) }

most of the time lambda functions ~ anonymous function,

Key difference in Kotlin is that

  • when you want a return to a block of code to from the enclosing function lambda use lambda
  • when you want to return from the block but not from the enclosing function use anonymous function



Closures are functions that can access and modify properties defined outside the scope of the function. example:

var total = 0
total = total+it

The End.

Congratulations you made it till the end!. if you found anything interesting feel free to share in response. Happy Coding!

Follow me on Twitter and LinkedIn

Written by

Coding enthusiast! love Android #kotlinAlltheWay, want to explore all opportunity around it! CodingMantra: #cleanCoder #TDD #SOLID #designpatterns

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store