Kotlin miniguide #5 Functions declaration


//typical function declaration

fun sum(a: Int, b: Int) : Int {

    return a+b

}

//function expression
fun sum(a: Int, b: Int) = a+b

//default parameters

fun payClerck(pay: Double , tip: Double = 1.0) : Double {

    return pay+ tip

}

//calling

payClerck(10.)

payClerck(10., 2.)

//using named parameters it is possible to change their

// order when calling the function

payClerck(tip = 1., pay = 2.0)

In kotlin functions are first class citizens:
A first-class citizen […] is an entity which supports all the operations generally available to other entities. These operations typically include being passed as an argument, returned from a function, modified, and assigned to a variable. (Wikipedia)

Functions do not need to be declared inside a class.

Functions can be declared locally

Lascia un commento