Skip to main content
ubuntuask.com

Back to all posts

How to Return an Object From A Function In Kotlin?

Published on
3 min read
How to Return an Object From A Function In Kotlin? image

Best Kotlin Programming Books 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 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
4 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
5 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)
6 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
7 Kotlin: An Illustrated Guide

Kotlin: An Illustrated Guide

BUY & SAVE
$49.62
Kotlin: An Illustrated Guide
8 Kotlin Design Patterns and Best Practices: Elevate your Kotlin skills with classical and modern design patterns, coroutines, and microservices

Kotlin Design Patterns and Best Practices: Elevate your Kotlin skills with classical and modern design patterns, coroutines, and microservices

BUY & SAVE
$30.53 $44.99
Save 32%
Kotlin Design Patterns and Best Practices: Elevate your Kotlin skills with classical and modern design patterns, coroutines, and microservices
9 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
+
ONE MORE?

In Kotlin, you can return an object from a function by simply specifying the object's type as the return type of the function. Inside the function, you can create an instance of the object using the return keyword followed by the object itself. For example, you can create a function that returns a Person object as follows:

data class Person(val name: String, val age: Int)

fun createPerson(): Person { return Person("John Doe", 30) }

fun main() { val person = createPerson() println("Name: ${person.name}, Age: ${person.age}") }

In this example, the createPerson function creates and returns a Person object with the name "John Doe" and age 30. The main function then calls createPerson to get the returned Person object and prints out its name and age.

What are the different ways to return an object from a function in Kotlin?

In Kotlin, there are multiple ways to return an object from a function:

  1. Using the "return" keyword: You can use the "return" keyword followed by the object you want to return in a function. For example:

fun getObject(): Any { return SomeObject() }

  1. Using the "return" keyword with a label: You can also use a label to specify which function to return from. For example:

fun getObject(): Any { return@getObject SomeObject() }

  1. Using the "return" keyword with a lambda function: You can use a lambda function to return an object from a function. For example:

fun getObject(): Any { return { SomeObject() }() }

  1. Using the "return" keyword with a single-expression function: You can create a single-expression function that returns an object without using the "return" keyword. For example:

fun getObject(): Any = SomeObject()

  1. Using the "run" function: You can use the "run" function to return an object from a lambda function. For example:

fun getObject(): Any { return run { SomeObject() } }

These are some of the common ways to return an object from a function in Kotlin.

What is the syntax for returning a sealed class object from a function in Kotlin?

To return a sealed class object from a function in Kotlin, you can simply use the return keyword followed by the sealed class object you want to return. Here is an example:

sealed class Result { data class Success(val data: String) : Result() data class Error(val message: String) : Result() }

fun getResult(isSuccess: Boolean): Result { return if (isSuccess) { Result.Success("Operation successful") } else { Result.Error("An error occurred") } }

In this example, the getResult function takes a boolean flag isSuccess and returns a sealed class object of type Result, which can be either a Success or an Error. The function uses a simple conditional expression to determine which type of object to return based on the isSuccess flag.

What is the scope of an object returned from a function in Kotlin?

The scope of an object returned from a function in Kotlin is determined by where the object is stored. If the object is assigned to a local variable within the function, its scope is limited to that function. If the object is returned from the function and assigned to a variable in a higher scope, such as a global variable or passed as an argument to another function, then its scope extends to wherever it is used.