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

Rispondi

Inserisci i tuoi dati qui sotto o clicca su un'icona per effettuare l'accesso:

Logo di WordPress.com

Stai commentando usando il tuo account WordPress.com. Chiudi sessione /  Modifica )

Google photo

Stai commentando usando il tuo account Google. Chiudi sessione /  Modifica )

Foto Twitter

Stai commentando usando il tuo account Twitter. Chiudi sessione /  Modifica )

Foto di Facebook

Stai commentando usando il tuo account Facebook. Chiudi sessione /  Modifica )

Connessione a %s...