Invoking functions

Functions are not executed once they are defined. In order for the code within a function to be executed, the function must be invoked. Functions can be invoked as functions, as methods, and indirectly by utilizing the invoke() and call() methods. The following shows the direct functional invocation using the function itself:

fun repeat(word: String, times: Int) {   var i = 0  while (i < times) {    println(word)    i++  }}fun main(args: Array<String>) {  repeat("Hello!", 5)}

Compile and run the preceding code. The word Hello is printed on the screen five times. Hello was passed as our first value in the function and 5 as our second. As a result of this, the word and times arguments are set to hold the Hello and 5 values in our ...

Get Kotlin Programming By Example now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.