0
0
Swiftprogramming~10 mins

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

Choose your learning style9 modes available
Concept Flow - Generic function declaration
Start
Declare generic function with <T>
Call function with specific type
Function uses T as placeholder
Function returns or processes T
End
This flow shows how a generic function is declared with a placeholder type T, then called with a specific type, and how T is used inside the function.
Execution Sample
Swift
func echo<T>(value: T) -> T {
    return value
}

let result = echo(value: 42)
This code declares a generic function echo that returns the same value it receives, then calls it with an integer 42.
Execution Table
StepActionGeneric Type TInput valueReturn value
1Function echo<T> declaredT is a placeholderN/AN/A
2Call echo with value 42T inferred as Int42N/A
3Inside echo, return valueT = Int4242
4Assign return to resultT = Int4242
5EndN/AN/AN/A
💡 Function completes after returning the input value as output.
Variable Tracker
VariableStartAfter callFinal
valueN/A4242
resultN/AN/A42
TPlaceholderIntInt
Key Moments - 3 Insights
Why do we write <T> after the function name?
The <T> declares a placeholder type that the function can use for any type. See execution_table step 1 where T is a placeholder.
How does the function know what type T is when called?
The compiler infers T from the argument type when the function is called, as shown in execution_table step 2 where T is inferred as Int.
Can the function return a different type than the input?
In this example, the function returns the same type T it receives, so input and output types match, as seen in step 3 and 4.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the type of T when echo is called with 42?
AString
BDouble
CInt
DPlaceholder only, no type yet
💡 Hint
Check execution_table step 2 where T is inferred as Int.
At which step does the function return the input value?
AStep 3
BStep 2
CStep 1
DStep 4
💡 Hint
Look at execution_table step 3 where return value is 42.
If we call echo with a String instead of Int, what changes in variable_tracker?
AT stays Int, value and result become String
BT changes to String, value and result become String
CT stays placeholder, no change
DFunction will not compile
💡 Hint
Generic type T adapts to input type, see variable_tracker for T and value.
Concept Snapshot
Generic function syntax:
func functionName<T>(param: T) -> T {
    // use T as type
}

- <T> declares a placeholder type.
- T is inferred from arguments when called.
- Allows writing flexible, reusable functions.
Full Transcript
This visual execution traces a generic function declaration in Swift. The function echo<T> is declared with a generic type T as a placeholder. When called with the integer 42, the compiler infers T as Int. Inside the function, the input value is returned as output, preserving the type. Variables value and result track the input and output values. Key moments clarify why <T> is used, how T is inferred, and that input and output types match. The quiz tests understanding of type inference, return step, and behavior with different input types.