| 
 | 1 | +# Functions  | 
 | 2 | + | 
 | 3 | +### Default parameter values and named arguments  | 
 | 4 | + | 
 | 5 | +<div class="language-kotlin" theme="idea">  | 
 | 6 | + | 
 | 7 | +```kotlin  | 
 | 8 | +fun printMessage(message: String): Unit {                           // 1  | 
 | 9 | +    println(message)  | 
 | 10 | +}  | 
 | 11 | + | 
 | 12 | +fun printMessageWithPrefix(message: String, prefix : String = "") { // 2  | 
 | 13 | +    println("[$prefix] $message")  | 
 | 14 | +}  | 
 | 15 | + | 
 | 16 | +fun sum(x: Int, y: Int): Int {                                      // 3  | 
 | 17 | +    return x + y  | 
 | 18 | +}  | 
 | 19 | + | 
 | 20 | +fun multiply(x: Int, y: Int) = x * y                                // 4  | 
 | 21 | + | 
 | 22 | +fun main() {  | 
 | 23 | +    printMessage("Hello")                                           // 5                      | 
 | 24 | +    printMessageWithPrefix("Hello", "Log")                          // 6  | 
 | 25 | +    printMessageWithPrefix("Hello")                                 // 7  | 
 | 26 | +    printMessageWithPrefix(prefix = "Log", message = "Hello")       // 8  | 
 | 27 | +    sum(1, 2)  | 
 | 28 | +}  | 
 | 29 | +```  | 
 | 30 | + | 
 | 31 | +</div>  | 
 | 32 | + | 
 | 33 | +1. Create a function that takes a parameter of type string and returns Unit (i.e. no return value).  | 
 | 34 | +2. Create a function that takes a second optional parameter with default value empty String. Also return Unit, which can be omitted.  | 
 | 35 | +3. Create a function that returns an integer.  | 
 | 36 | +4. Create a single-expression function that returns an integer (inferred).  | 
 | 37 | +5. Call a function passing as argument `Hello`.  | 
 | 38 | +6. Call a function with two parameters, passing a value for the second optional parameter.  | 
 | 39 | +7. Call a function with two parameters, ignoring the second one.   | 
 | 40 | +8. Call a function using named parameters and changing order of arguments.  | 
 | 41 | + | 
 | 42 | +### Infix functions  | 
 | 43 | + | 
 | 44 | +Member functions and extensions with a single parameter can be turned into infix functions.  | 
 | 45 | + | 
 | 46 | +<div class="language-kotlin" theme="idea">  | 
 | 47 | + | 
 | 48 | +```kotlin  | 
 | 49 | +fun main() {  | 
 | 50 | + | 
 | 51 | +  infix fun Int.times(str: String) = str.repeat(this)        // 1  | 
 | 52 | +  println(2 times "Bye ")                                    // 2  | 
 | 53 | + | 
 | 54 | +  val pair = "Ferrari" to "Katrina"                          // 3  | 
 | 55 | +  println(pair)  | 
 | 56 | + | 
 | 57 | +  infix fun String.onto(other: String) = Pair(this, other)   // 4  | 
 | 58 | +  val myPair = "McLaren" onto "Lucas"  | 
 | 59 | +  println(myPair)  | 
 | 60 | + | 
 | 61 | +  val sophia = Person("Sophia")  | 
 | 62 | +  val claudia = Person("Claudia")  | 
 | 63 | +  sophia likes claudia                                       // 5  | 
 | 64 | +}  | 
 | 65 | + | 
 | 66 | +class Person(val name: String) {  | 
 | 67 | +  val likedPeople = mutableListOf<Person>()  | 
 | 68 | +  infix fun likes(other: Person) { likedPeople.add(other) }  // 6  | 
 | 69 | +}  | 
 | 70 | +```  | 
 | 71 | + | 
 | 72 | +</div>  | 
 | 73 | + | 
 | 74 | +1. Defines an infix extension function on `Int`  | 
 | 75 | +2. Call infix function `1`  | 
 | 76 | +3. The infix function `to` from the standard library lets you easily create `Pair`s  | 
 | 77 | +4. Here's your own implementation of `to` creatively called `onto`  | 
 | 78 | +5. Extensions also work on members functions (methods).  | 
 | 79 | +6. The containing class becomes the first parameter.  | 
 | 80 | + | 
 | 81 | +Note that the example uses _local functions_ (functions nested into another function).  | 
 | 82 | + | 
 | 83 | +### Operator functions  | 
 | 84 | + | 
 | 85 | +Certain functions can be "upgraded" to operators, allowing to use them with the corresponding operator symbol.  | 
 | 86 | + | 
 | 87 | +<div class="language-kotlin" theme="idea">  | 
 | 88 | + | 
 | 89 | +```kotlin  | 
 | 90 | +fun main() {  | 
 | 91 | +//sampleStart  | 
 | 92 | + | 
 | 93 | +  operator fun Int.times(str: String) = str.repeat(this)       // 1  | 
 | 94 | +  println(2 * "Bye ")                                          // 2  | 
 | 95 | + | 
 | 96 | +  operator fun String.get(range: IntRange) = substring(range)  // 3  | 
 | 97 | +  val str = "Always forgive your enemies; nothing annoys them so much."  | 
 | 98 | +  println(str[0..14])                                          // 4  | 
 | 99 | +//sampleEnd  | 
 | 100 | +}  | 
 | 101 | +```  | 
 | 102 | + | 
 | 103 | +</div>  | 
 | 104 | + | 
 | 105 | +1. This takes the infix function from above one step further using the `operator` modifier.  | 
 | 106 | +2. The operator symbol for `times()` is `*` so that you can call the function using `2 * "Bye"`  | 
 | 107 | +3. An operator function allowing easy range access on strings  | 
 | 108 | +4. The `get()` operator enables bracket-access syntax  | 
 | 109 | + | 
 | 110 | +### Functions with `vararg` parameters  | 
 | 111 | + | 
 | 112 | +Varargs allow passing any number of arguments by comma-separating them.  | 
 | 113 | + | 
 | 114 | +<div class="language-kotlin" theme="idea">  | 
 | 115 | + | 
 | 116 | +```kotlin  | 
 | 117 | +fun main() {  | 
 | 118 | +//sampleStart  | 
 | 119 | +  fun printAll(vararg messages: String) {                            // 1  | 
 | 120 | +    for (m in messages) println(m)  | 
 | 121 | +  }  | 
 | 122 | +  printAll("Hello", "Hallo", "Salut", "Hola", "你好")                 // 2  | 
 | 123 | + | 
 | 124 | +  fun printAllWithPrefix(vararg messages: String, prefix: String) {  // 3  | 
 | 125 | +    for (m in messages) println(prefix + m)  | 
 | 126 | +  }  | 
 | 127 | +  printAllWithPrefix("Hello", "Hallo", "Salut", "Hola", "你好",  | 
 | 128 | +    prefix = "Greeting: ")                                           // 4  | 
 | 129 | + | 
 | 130 | +  fun log(vararg entries: String) {  | 
 | 131 | +    printAll(*entries)                                               // 5  | 
 | 132 | +  }  | 
 | 133 | +//sampleEnd  | 
 | 134 | +}  | 
 | 135 | +```  | 
 | 136 | + | 
 | 137 | +</div>  | 
 | 138 | + | 
 | 139 | +1. The `vararg` modifier turns a parameter into a vararg.  | 
 | 140 | +2. This allows calling `printAll` with any number of string arguments.  | 
 | 141 | +3. Thanks to named parameters, you can even add another parameter of the same type after the vararg. This wouldn't be allowed in Java because there's no way to pass a value.  | 
 | 142 | +4. Using named parameters, you can set a value to `prefix` despite the vararg.  | 
 | 143 | +5. At runtime, a vararg is simply an array. To pass it along into a vararg parameter, you can use the special spread operator `*foo` to pass in `*entries` (a vararg of `String`) instead of `entries` (an `Array<String>`).  | 
0 commit comments