Sling Academy
Home/Kotlin/How to Return Booleans from Functions in Kotlin

How to Return Booleans from Functions in Kotlin

Last updated: November 29, 2024

In Kotlin, returning booleans from functions is a straightforward process thanks to the language's concise syntax. Kotlin allows you to define functions that return a Boolean value in various ways. Here's how you can implement this in your application.

Defining a Function that Returns a Boolean

To begin with, you may define a function that performs a certain check and returns either true or false. Functions returning boolean values can be directly related to comparison operations or logical checks.

fun isEven(number: Int): Boolean {
    return number % 2 == 0
}

In the code above, the function isEven accepts an integer as an input and returns true if the number is divisible by 2, and false otherwise.

Using Expression Body for Functions

Kotlin also allows you to use a more concise syntax called expression body for single-expression functions. This can make your function easier to read and maintain:

fun isPositive(number: Int) = number > 0

This function isPositive checks if a number is greater than 0 and returns the appropriate boolean value. The return type Boolean is inferred, so it is optional to declare it explicitly.

Using Boolean Return in Conditionals

You often use boolean functions as conditions in if-statements. Here is how you can integrate them directly:


fun checkNumber(number: Int) {
    if (isEven(number)) {
        println("$number is even")
    } else {
        println("$number is odd")
    }
}

In the checkNumber function, the boolean return value of the isEven function is used to determine how the message should be printed.

Complex Conditions with Boolean Functions

Boolean-returning functions allow you to encapsulate complex conditions, leading to cleaner and more maintainable code. You can even chain multiple boolean functions together.


fun isEligibleForDiscount(age: Int, isStudent: Boolean): Boolean {
    return age >= 12 && age <= 25 || isStudent
}

In this function, a person is eligible for a discount if they are between 12 and 25 years old or if they are a student.

Conclusion

Returning booleans from functions in Kotlin aids greatly in simplifying the conditions you use in your application. Understanding how to use this feature means your code can become cleaner, more logical, and easily maintainable.

Next Article: Working with Nullable Booleans in Kotlin

Previous Article: Negating Booleans: When to Use `!` in Kotlin

Series: Primitive data types in Kotlin

Kotlin

You May Also Like

  • How to Use Modulo for Cyclic Arithmetic in Kotlin
  • Kotlin: Infinite Loop Detected in Code
  • Fixing Kotlin Error: Index Out of Bounds in List Access
  • Setting Up JDBC in a Kotlin Application
  • Creating a File Explorer App with Kotlin
  • How to Work with APIs in Kotlin
  • What is the `when` Expression in Kotlin?
  • Writing a Script to Rename Multiple Files Programmatically in Kotlin
  • Using Safe Calls (`?.`) to Avoid NullPointerExceptions in Kotlin
  • Chaining Safe Calls for Complex Operations in Kotlin
  • Using the Elvis Operator for Default Values in Kotlin
  • Combining Safe Calls and the Elvis Operator in Kotlin
  • When to Avoid the Null Assertion Operator (`!!`) in Kotlin
  • How to Check for Null Values with `if` Statements in Kotlin
  • Using `let` with Nullable Variables for Scoped Operations in Kotlin
  • Kotlin: How to Handle Nulls in Function Parameters
  • Returning Nullable Values from Functions in Kotlin
  • Safely Accessing Properties of Nullable Objects in Kotlin
  • How to Use `is` for Nullable Type Checking in Kotlin