1
Lesson 2: Functions
This work is licensed under the Apache 2 license.
Android Development with Kotlin v1.0
This work is licensed under the Apache 2 license.
About this lesson
Lesson 2: Functions
2
Android Development with Kotlin
This work is licensed under the Apache 2 license.
Programs in Kotlin
3
Android Development with Kotlin
This work is licensed under the Apache 2 license.
Setting up
4
Before you can write code and run programs, you need to:
Android Development with Kotlin
This work is licensed under the Apache 2 license.
Create a new Kotlin file
In IntelliJ IDEA's Project pane, under Hello World, right-click the src folder.
5
Android Development with Kotlin
This work is licensed under the Apache 2 license.
Create a Kotlin file
You should now see a file in the src folder called Hello.kt.
6
Android Development with Kotlin
This work is licensed under the Apache 2 license.
Create a main() function
In the Hello.kt file:
fun main(args: Array<String>) {
println("Hello, world!")
}
7
The args in the main() function are optional.
main() is the entry point for execution for a Kotlin program.
Android Development with Kotlin
This work is licensed under the Apache 2 license.
Run your Kotlin program
To run your program, click the Run icon ( ) to the left of the main() function.
IntelliJ IDEA runs the program, and displays the results in the console.
8
Android Development with Kotlin
This work is licensed under the Apache 2 license.
Pass arguments to main()
Select Run > Edit Configurations to open the Run/Debug Configurations window.
9
Android Development with Kotlin
This work is licensed under the Apache 2 license.
Use arguments in main()
Use args[0] to access the first input argument passed to main().
10
fun main(args: Array<String>) {
println("Hello, ${args[0]}")
}
⇒ Hello, Kotlin!
Android Development with Kotlin
This work is licensed under the Apache 2 license.
(Almost) Everything has a value
11
Android Development with Kotlin
This work is licensed under the Apache 2 license.
(Almost) Everything is an expression
12
val temperature = 20
val isHot = if (temperature > 40) true else false
println(isHot)
⇒ false
In Kotlin, almost everything is an expression and has a value. Even an if expression has a value.
Android Development with Kotlin
This work is licensed under the Apache 2 license.
Expression values
Sometimes, that value is kotlin.Unit.
13
⇒ This is an expression
kotlin.Unit
val isUnit = println("This is an expression")
println(isUnit)
Android Development with Kotlin
This work is licensed under the Apache 2 license.
Functions in Kotlin
14
Android Development with Kotlin
This work is licensed under the Apache 2 license.
About functions
15
Android Development with Kotlin
This work is licensed under the Apache 2 license.
Parts of a function
Earlier, you created a simple function that printed "Hello World".
16
fun printHello() {
println("Hello World")
}
printHello()
Android Development with Kotlin
This work is licensed under the Apache 2 license.
Unit returning functions
If a function does not return any useful value, its return type is Unit.
17
fun printHello(name: String?): Unit {
println("Hi there!")
}
Unit is a type with only one value: Unit.
Android Development with Kotlin
This work is licensed under the Apache 2 license.
Unit returning functions
18
The Unit return type declaration is optional.
fun printHello(name: String?) {
println("Hi there!")
}
fun printHello(name: String?): Unit {
println("Hi there!")
}
is equivalent to:
Android Development with Kotlin
This work is licensed under the Apache 2 license.
Function arguments
19
Functions may have:
Android Development with Kotlin
This work is licensed under the Apache 2 license.
Default parameters
fun drive(speed: String = "fast") {
println("driving $speed")
}
20
Default values provide a fallback if no parameter value is passed.
drive() ⇒ driving fast
drive("slow") ⇒ driving slow
drive(speed = "turtle-like") ⇒ driving turtle-like
Use "=" after the type
to define default values
Android Development with Kotlin
This work is licensed under the Apache 2 license.
Required parameters
If no default is specified for a parameter, the corresponding argument is required.
21
fun tempToday(day: String, temp: Int) {
println("Today is $day and it's $temp degrees.")
}
Required parameters
Android Development with Kotlin
This work is licensed under the Apache 2 license.
Default versus required parameters
Functions can have a mix of default and required parameters.
Pass in required arguments.
22
fun reformat(str: String,
divideByCamelHumps: Boolean,
wordSeparator: Char,
normalizeCase: Boolean = true){
Has default value
reformat("Today is a day like no other day", false, '_')
Android Development with Kotlin
This work is licensed under the Apache 2 license.
Named arguments
To improve readability, use named arguments for required arguments.
reformat(str, divideByCamelHumps = false, wordSeparator = '_')
23
It's considered good style to put default arguments after positional arguments, that way callers only have to specify the required arguments.
Android Development with Kotlin
This work is licensed under the Apache 2 license.
Compact functions
24
Android Development with Kotlin
This work is licensed under the Apache 2 license.
Single-expression functions
Compact functions, or single-expression functions, make your code more concise and readable.
25
fun double(x: Int): Int {
x * 2
}
fun double(x: Int):Int = x * 2
Complete version
Compact version
Android Development with Kotlin
This work is licensed under the Apache 2 license.
Lambdas and higher-order functions
26
Android Development with Kotlin
This work is licensed under the Apache 2 license.
Kotlin functions are first-class
27
Android Development with Kotlin
This work is licensed under the Apache 2 license.
Lambda functions
var dirtLevel = 20
val waterFilter = {level: Int -> level / 2}
println(waterFilter(dirtLevel))
⇒ 10
28
A lambda is an expression that makes a function that has no name.
Function arrow
Code to execute
Parameter and type
Android Development with Kotlin
This work is licensed under the Apache 2 license.
Syntax for function types
val waterFilter: (Int) -> Int = {level -> level / 2}
29
Kotlin's syntax for function types is closely related to its syntax for lambdas. Declare a variable that holds a function.
Data type of variable
(function type)
Variable name
Function
Android Development with Kotlin
This work is licensed under the Apache 2 license.
Higher-order functions
Higher-order functions take functions as parameters, or return a function.
30
fun encodeMsg(msg: String, encode: (String) -> String): String {
return encode(msg)
}
The body of the code calls the function that was passed as the second argument, and passes the first argument along to it.
Android Development with Kotlin
This work is licensed under the Apache 2 license.
Higher-order functions
To call this function, pass it a string and a function.
31
val enc1: (String) -> String = { input -> input.toUpperCase() }
println(encodeMsg("abc", enc1))
Using a function type separates its implementation from its usage.
Android Development with Kotlin
This work is licensed under the Apache 2 license.
Passing a function reference
Use the :: operator to pass a named function as an argument to another function.
32
fun enc2(input:String): String = input.reversed()
The :: operator lets Kotlin know that you are passing the function reference as an argument, and not trying to call the function.
encodeMessage("abc", ::enc2)
Passing a named function,
not a lambda
Android Development with Kotlin
This work is licensed under the Apache 2 license.
Last parameter call syntax
Kotlin prefers that any parameter that takes a function is the last parameter.
33
encodeMessage("acronym", { input -> input.toUpperCase() })
You can pass a lambda as a function parameter without putting it inside the parentheses.
encodeMsg("acronym") { input -> input.toUpperCase() }
Android Development with Kotlin
This work is licensed under the Apache 2 license.
Using higher-order functions
Many Kotlin built-in functions are defined using last parameter call syntax.
34
inline fun repeat(times: Int, action: (Int) -> Unit)
repeat(3) {
println("Hello")
}
Android Development with Kotlin
This work is licensed under the Apache 2 license.
List filters
35
Android Development with Kotlin
This work is licensed under the Apache 2 license.
List filters
36
Get part of a list based on some condition
red | red-orange | dark red | orange | bright orange | saffron |
red | red-orange | dark red |
Apply filter() on list
Condition: element contains “red”
Android Development with Kotlin
This work is licensed under the Apache 2 license.
Iterating through lists
If a function literal has only one parameter, you can omit its declaration and the "->". The parameter is implicitly declared under the name it.
37
val ints = listOf(1, 2, 3)
ints.filter { it > 0 }
ints.filter { n -> n > 0 }
ints.filter { n: Int -> n > 0 }
OR
Filter iterates through a collection, where it is the value of the element during the iteration. This is equivalent to:
Android Development with Kotlin
This work is licensed under the Apache 2 license.
List filters
val books = listOf("nature", "biology", "birds")
println(books.filter { it[0] == 'b' })
38
⇒ [biology, birds]
The filter condition in curly braces {} tests each item as the filter loops through. If the expression returns true, the item is included.
Android Development with Kotlin
This work is licensed under the Apache 2 license.
Eager and lazy filters
39
Lazy evaluation of lists is useful if you don't need the entire result, or if the list is exceptionally large and multiple copies wouldn't wouldn't fit into RAM.
Evaluation of expressions in lists:
Android Development with Kotlin
This work is licensed under the Apache 2 license.
Eager filters
Filters are eager by default. A new list is created each time you use a filter.
40
val instruments = listOf("viola", "cello", "violin")
val eager = instruments.filter { it [0] == 'v' }
println("eager: " + eager)
⇒ eager: [viola, violin]
Android Development with Kotlin
This work is licensed under the Apache 2 license.
Lazy filters
Sequences are data structures that use lazy evaluation, and can be used with filters to make them lazy.
41
⇒ filtered: kotlin.sequences.FilteringSequence@386cc1c4
val instruments = listOf("viola", "cello", "violin")
val filtered = instruments.asSequence().filter { it[0] == 'v'}
println("filtered: " + filtered)
Android Development with Kotlin
This work is licensed under the Apache 2 license.
Sequences -> lists
Sequences can be turned back into lists using toList().
42
val newList = filtered.toList()
⇒ new list: [viola, violin]
val filtered = instruments.asSequence().filter { it[0] == 'v'}
println("new list: " + newList)
Android Development with Kotlin
This work is licensed under the Apache 2 license.
Other list transformations
val numbers = setOf(1, 2, 3)
println(numbers.map { it * 3 })
=> [3, 6, 9]
val numberSets = listOf(setOf(1, 2, 3), setOf(4, 5), setOf(1, 2))�println(numberSets.flatten())
=> [1, 2, 3, 4, 5, 1, 2]
43
Android Development with Kotlin
This work is licensed under the Apache 2 license.
Summary
44
Android Development with Kotlin
This work is licensed under the Apache 2 license.
Summary
45
In Lesson 2, you learned how to:
Android Development with Kotlin
This work is licensed under the Apache 2 license.
Pathway
46
Android Development with Kotlin
This work is licensed under the Apache 2 license.