# Generic Function

DevFeed: [Generic Function](<https://devfeed.tech/articles/generic-function-25015.md>)

Original publisher: [Read original article](<https://typealias.com/concepts/generic-function/>)

Author: author@typealias.com (Dave Leeds)

Published: 2017-09-21T02:07:39Z

Content type: tutorial

Language: en

Sources: [Dave Leeds on Kotlin - typealias.com](<https://devfeed.tech/sources/dave-leeds-on-kotlin-typealias-com.md>)

Topics: [Kotlin](<https://devfeed.tech/topics/kotlin.md>), [Code](<https://devfeed.tech/topics/code.md>), [Java](<https://devfeed.tech/topics/java.md>)

Tags: [code](<https://devfeed.tech/tags/code.md>), [comparison](<https://devfeed.tech/tags/comparison.md>), [function](<https://devfeed.tech/tags/function.md>), [java](<https://devfeed.tech/tags/java.md>), [kotlin](<https://devfeed.tech/tags/kotlin.md>), [kotlin-generic-function](<https://devfeed.tech/tags/kotlin-generic-function.md>), [list](<https://devfeed.tech/tags/list.md>), [programming](<https://devfeed.tech/tags/programming.md>), [type-parameter](<https://devfeed.tech/tags/type-parameter.md>)

## AI overview

A tutorial explaining Kotlin generic functions, including type parameters, type preservation, usage with different item types, comparison with Java, and when to use generic functions instead of abstract argument and return types.

## Source excerpt

Overview Generic functions are functions that accept a type parameter, which allows different call sites to pass different types, without sacrificing type safety. Yeah, that's a mouthful, so let's just take a look at an example. Example Definition Here's a Kotlin generic function that returns the middle-most item out of a List: fun <T> middleItem(list: List<T>): T = list[list.size / 2] This example specifies: A type parameter of type T. This type parameter declaration goes before the name of the function, and it's written here as <T>.