Skip to main content
freelanceshack.com

Back to all posts

How to Pass Any Enum As A Function Argument In Kotlin?

Published on
3 min read
How to Pass Any Enum As A Function Argument In Kotlin? image

Best Kotlin Programming Guides to Buy in October 2025

1 Kotlin in Action, Second Edition

Kotlin in Action, Second Edition

BUY & SAVE
$45.98 $59.99
Save 23%
Kotlin in Action, Second Edition
2 Head First Kotlin: A Brain-Friendly Guide

Head First Kotlin: A Brain-Friendly Guide

BUY & SAVE
$50.36 $79.99
Save 37%
Head First Kotlin: A Brain-Friendly Guide
3 Android Programming with Kotlin for Beginners: Build Android apps starting from zero programming experience with the new Kotlin programming language

Android Programming with Kotlin for Beginners: Build Android apps starting from zero programming experience with the new Kotlin programming language

BUY & SAVE
$33.00 $38.99
Save 15%
Android Programming with Kotlin for Beginners: Build Android apps starting from zero programming experience with the new Kotlin programming language
4 Head First Android Development: A Learner's Guide to Building Android Apps with Kotlin

Head First Android Development: A Learner's Guide to Building Android Apps with Kotlin

BUY & SAVE
$59.30 $89.99
Save 34%
Head First Android Development: A Learner's Guide to Building Android Apps with Kotlin
5 Programming Android with Kotlin: Achieving Structured Concurrency with Coroutines

Programming Android with Kotlin: Achieving Structured Concurrency with Coroutines

BUY & SAVE
$48.00 $65.99
Save 27%
Programming Android with Kotlin: Achieving Structured Concurrency with Coroutines
6 Kotlin In-Depth: A Guide to a Multipurpose Programming Language for Server-Side, Front-End, Android, and Multiplatform Mobile (English Edition)

Kotlin In-Depth: A Guide to a Multipurpose Programming Language for Server-Side, Front-End, Android, and Multiplatform Mobile (English Edition)

BUY & SAVE
$29.95 $32.95
Save 9%
Kotlin In-Depth: A Guide to a Multipurpose Programming Language for Server-Side, Front-End, Android, and Multiplatform Mobile (English Edition)
7 Kotlin from Scratch: A Project-Based Introduction for the Intrepid Programmer

Kotlin from Scratch: A Project-Based Introduction for the Intrepid Programmer

BUY & SAVE
$36.20 $59.99
Save 40%
Kotlin from Scratch: A Project-Based Introduction for the Intrepid Programmer
+
ONE MORE?

To pass any enum as a function argument in Kotlin, you simply need to declare the parameter type of the function as the enum class. For example, if you have an enum class called Colors and a function that takes a Colors enum as an argument, you would define the function like this:

enum class Colors { RED, BLUE, GREEN }

fun printColor(color: Colors) { println("The selected color is $color") }

fun main() { val selectedColor = Colors.RED printColor(selectedColor) }

In this example, the printColor function takes a Colors enum as its parameter, and you can pass any value from the Colors enum when calling this function. The key is to make sure that the parameter type matches the enum class that you want to pass as an argument.

What is the purpose of ordinal() method in enums?

The ordinal() method in enums is used to retrieve the position of a particular enum constant in the enum declaration. It returns an integer value which represents the position of the enum constant, starting from 0 for the first constant, 1 for the second constant, and so on. This method can be useful when you need to compare enum constants based on their positions or when you need to iterate over all the constants in a specific order.

What is the alternative to enums in Kotlin?

Sealed classes are a common alternative to enums in Kotlin. Sealed classes allow you to represent restricted class hierarchies, meaning that all subclasses must be defined within the same file as the sealed class declaration. This allows for more flexibility and customization compared to enums, which have a fixed set of values. Additionally, sealed classes support data classes, which provide automatic implementations of methods like equals(), hashCode(), and toString().

What is the purpose of enum in programming?

The purpose of an enum (short for enumeration) in programming is to define a type with a fixed number of constant values. Enums are used to create a set of named constants that can be used to represent a set of possible values for a variable. This makes code more readable, maintainable, and less error-prone by providing a clear and concise way to work with a limited set of options. Enums are commonly used in switch statements, to represent states/status, and in many other scenarios where a set of predefined values is needed.

How to access enum values in Kotlin?

To access enum values in Kotlin, you can use the name of the enum class followed by a dot and the name of the enum value. For example, if you have an enum class called Color with values RED, GREEN, and BLUE, you can access them like this:

enum class Color { RED, GREEN, BLUE }

fun main() { val myColor = Color.RED println(myColor) // Output: RED }

You can also use enum values in when expressions, for example:

fun printColor(color: Color) { when (color) { Color.RED -> println("Red color") Color.GREEN -> println("Green color") Color.BLUE -> println("Blue color") } }

fun main() { val myColor = Color.GREEN printColor(myColor) // Output: Green color }

These are some examples of how you can access enum values in Kotlin.