KotlinJava

fun printIt(count: Int) {
println(count)
}
fun printIt(first: Int, second: Int) {
println(first + second)
}
printIt(10)
printIt(40, 50)

Our next lesson continues our exploration of functions. We’ll reinforce material covered in our last lesson, and go through some example of exactly what happens during function execution. We’ll also learn about overloading, when two methods share the same name. Let’s get started!

Tracing Function ExecutionTracing Function Execution

While we’ve started to use and even write functions, we may be still a bit fuzzy about exactly what happens when a function runs. So let’s go through that slowly and carefully using a few examples.

fun completelyEven(values: IntArray): Boolean {
for (i in values.indices) {
if (values[i] % 2 != 0) {
return false
}
}
return true
}
println(completelyEven(intArrayOf(1, 9, 9)))
println(completelyEven(intArrayOf(2, 4, 8)))

Next, let’s think about what happens when one of our functions itself calls another function!

fun isEven(value: Int): Boolean {
// Finish me
}
fun completelyEven(values: IntArray): Boolean {
for (i in values.indices) {
// TODO: Use isEven function
if (values[i] % 2 != 0) {
return false
}
}
return true
}
println(completelyEven(intArrayOf(1, 9, 9)))
println(completelyEven(intArrayOf(2, 4, 8)))

Common Algorithm PatternsCommon Algorithm Patterns

Computer scientists and programmers love patterns. Particularly in code. Frequently, once you’ve learned how to do one thing with code, you can modify that skeleton to solve other similar problems.

Let’s take a concrete example. The following code determines the count of the number of elements in an array. Note that this is not a good way to do this—it’s better to just use `array.size`. But we can modify this basic template to solve other problems. Let’s see how.

fun arrayLength(values: IntArray): Int {
var len = 0
for (i in values.indices) {
len++
}
return len
}
println(arrayLength(intArrayOf(1, 9, 9)))

Searching arrays produces another common pattern. It’s different from the example above, because we don’t necessarily need to complete the loop. Again, let’s walk through the basic template and then consider how to expand it.

fun arrayFindFour(values: IntArray): Boolean {
for (i in values.indices) {
if (values[i] == 4) {
return true
}
}
return false
}

Through the rest of our lessons we’ll continue to point out patterns that emerge in our programming. And ways to modify the basic pattern to produce more interesting designs!

Perhaps you noticed something unsettling about the example that starts this lesson:

fun printIt(count: Int) {
println(count)
}
fun printIt(first: Int, second: Int) {
println(first + second)
}
printIt(10)
printIt(40, 50)

Note only do we have two functions with the same name—`printIt`—but they both work! Distinguish this from variables, where we can never use the same name twice in the same scope:

// This (still) doesn't work
var first = 0
var first = '8'

Having multiple methods with the same name is known as method overloading. We don’t use this feature often, but you may see it in other Kotlin code that you work with.

When two methods have the same name Kotlin must be able to distinguish them from each other when they are used. So, if two methods have the same name, something must be different about them. The walkthrough below describes how Kotlin can tell them apart.

fun printIt(count: Int) {
println(count)
}
fun printIt(first: Int, second: Int) {
println(first + second)
}
printIt(10)
printIt(40, 50)

`Unit` Return`Unit` Return

If you have been watching carefully, you’ve already seen something new in this lesson: methods without a return type! When you omit the return type of your Kotlin method declaration, Kotlin declares it to return a special type `Unit`. We don’t use `Unit` explicitly: we just omit the return type. Like this:

// Look, no return type!
fun printCount(count: Int) {
println("The count is \$count")
}
printCount(5)
printCount(8)

Functions that don’t return a value can still use `return` to complete their execution, but can’t `return` a value. The result of calling a `Unit` function can also not be used in an assignment or statement:

fun printCount(count: Int) {
println("The count is \$count")
}
printCount(5)
printCount(8)

More Practice

Need more practice? Head over to the practice page.