Lesson 3
Kotlin Functions: Mastering Return Values
Introduction to Function Returns in Kotlin

Welcome, learners! Today, we're diving into function return values in Kotlin programming. Function return values, often simply referred to as "returns," are results given back by a function after performing its assigned task. This concept is signified in Kotlin with the keyword return.

Here's a simple example of a function greet() that returns a String:

Kotlin
1fun greet(): String { 2 // This function will return the following string 3 return "Hello, there!" 4}
Using Function Returns: Working examples

Now, let's get more practical. Suppose we want our function to perform a calculation—an addition operation in a calculator program, for instance. Here's how we can write a function that adds two numbers and returns the sum:

Kotlin
1fun addNumbers(num1: Int, num2: Int): Int { 2 // This function adds num1 and num2 and returns the sum. 3 return num1 + num2 4}

Let's see this function in action within Kotlin's main function:

Kotlin
1fun main() { 2 // Call addNumbers with 5 and 7 as parameters, print the returned value 3 println(addNumbers(5, 7)) // Output: 12 4}
Exploring Data Types in Returns

Returns are not always simple numbers. A function can return any type of data from the range Kotlin supports, including Int, String, Double, Boolean, Char, and Long. Let's explore this further using the calculation of a circle's area as an example, which is πr².

Kotlin
1fun circleArea(radius: Double): Double { 2 // This function calculates the circle's area and returns it. 3 return 3.14 * radius * radius 4}
Creating Multi-line Functions

Functions don't have to be limited to one line. Kotlin functions can span multiple lines and still return a value. Suppose, for instance, that we want to determine whether a student has passed or failed based on their score:

Kotlin
1fun passOrFail(score: Int): String { 2 var result = "Fail" 3 4 if (score > 75) { 5 result = "Pass" 6 } 7 8 return result 9}
Multiple Returns

It's possible that a function has multiple return statements. When a return statement is reached, the function stops running, and all subsequent code within it is ignored. This ensures that the function exits as soon as it fulfills a condition that leads to a return:

Kotlin
1fun passOrFail(score: Int): String { 2 if (score > 75) { 3 return "Pass" // If this condition is met, the function immediately exits with "Pass" 4 } 5 6 return "Fail" // If the condition above is not met, the function returns "Fail" 7}
A Look at Void Functions

Some functions have no return value. In Kotlin, these are referred to as void functions and are signified by the Unit return type. Here's an example of a void function that prints the multiplication table for a specific number:

Kotlin
1fun printMultiplicationTable(num: Int): Unit { 2 for (i in 1..10) { 3 // Multiply num with numbers from 1 to 10, print results 4 println("$num x $i = ${num * i}") 5 } 6}

This function has no return value. Its purpose is to print the multiplication table. If function has a Unit return type, there is no need to define it explicitly.

Lesson Summary

Well done! We've learned all about function return values in Kotlin, the return keyword, how to combine it with different data types, and even how to create void functions. Now, it's time for some practical, hands-on practice to reinforce these concepts. Happy coding!

Enjoy this lesson? Now it's time to practice with Cosmo!
Practice is how you turn knowledge into actual skills.