0
0
Kotlinprogramming~10 mins

Generic function declaration in Kotlin - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Generic function declaration
Start
Declare function with <T>
Use T as parameter type
Use T as return type
Call function with specific type
Function executes with that type
End
This flow shows how a generic function is declared with a type placeholder T, used in parameters and return type, then called with a specific type.
Execution Sample
Kotlin
fun <T> identity(value: T): T {
    return value
}

val result = identity(42)
This code declares a generic function identity that returns the same value it receives, then calls it with an Int.
Execution Table
StepActionEvaluationResult
1Declare generic function identity with type parameter TT is a placeholder for any typeFunction ready to use with any type
2Call identity with value 42T is inferred as IntFunction called with value=42 of type Int
3Inside function: return valueReturn 42Returns 42
4Assign return to resultresult = 42result holds Int value 42
💡 Function completes after returning the input value
Variable Tracker
VariableStartAfter CallFinal
valueN/A4242
resultN/AN/A42
Key Moments - 3 Insights
Why do we write <T> before the function name?
The <T> declares a generic type parameter T that can be used inside the function. This is shown in execution_table step 1 where T is a placeholder.
How does Kotlin know what type T is when calling the function?
Kotlin infers T from the argument type at the call site, as shown in step 2 where T is inferred as Int because 42 is an Int.
Can the function return a different type than the input?
No, because the return type is T, the same as the input parameter type, so it returns the same type it receives (step 3).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table step 2, what type is T inferred as when calling identity(42)?
AInt
BString
CAny
DDouble
💡 Hint
Check the 'Evaluation' column in step 2 where T is inferred as Int.
At which step does the function return the input value?
AStep 1
BStep 2
CStep 3
DStep 4
💡 Hint
Look for the step where the function returns the value inside the function.
If we call identity("hello"), how would the variable 'value' change in variable_tracker?
Avalue would be 42
Bvalue would be "hello"
Cvalue would be null
Dvalue would be Int
💡 Hint
Refer to variable_tracker where 'value' holds the input passed to the function.
Concept Snapshot
Generic function declaration syntax:
fun <T> functionName(param: T): T {
    // use T as type
}

- <T> declares a type parameter T
- T can be used as parameter and return type
- Kotlin infers T from the argument when called
- Enables writing flexible, reusable functions
Full Transcript
This visual trace shows how a generic function is declared and used in Kotlin. First, the function identity is declared with a generic type parameter T. This means T can be any type. When calling identity(42), Kotlin infers T as Int because 42 is an Int. Inside the function, the value parameter has type T, so it is Int here. The function returns the same value it received. The result variable receives the returned value 42. The variable tracker shows how 'value' and 'result' change during execution. Key moments clarify why <T> is needed, how Kotlin infers types, and why the return type matches the input type. The quiz questions test understanding of type inference, return step, and variable values when calling with different types.