0
0
Kotlinprogramming~10 mins

Infix functions for readable calls in Kotlin - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Infix functions for readable calls
Define infix function
Call function with infix syntax
Function executes with passed argument
Return result or perform action
Use result or continue program
An infix function is defined with the 'infix' keyword and called without dots or parentheses for cleaner, readable code.
Execution Sample
Kotlin
infix fun Int.times(str: String) = str.repeat(this)

val result = 3 times "Hi "
println(result)
Defines an infix function 'times' to repeat a string multiple times and calls it in a readable way.
Execution Table
StepActionEvaluationResult
1Define infix function 'times'Int.times(String) = String repeated Int timesFunction ready
2Call '3 times "Hi "'3.times("Hi ")Calls function with this=3, str="Hi "
3Inside function: str.repeat(this)"Hi ".repeat(3)"Hi Hi Hi "
4Assign resultresult = "Hi Hi Hi "Variable 'result' holds "Hi Hi Hi "
5Print resultprintln(result)Output: Hi Hi Hi
6EndProgram endsExecution complete
💡 Function call completes and program ends after printing the repeated string.
Variable Tracker
VariableStartAfter Step 2After Step 4Final
resultundefinedundefined"Hi Hi Hi ""Hi Hi Hi "
Key Moments - 3 Insights
Why can we call the function without dots or parentheses?
Because the function is marked with 'infix', Kotlin allows calling it using infix notation as shown in step 2 of the execution_table.
What does 'this' refer to inside the infix function?
'this' refers to the integer on which the function is called, here it is 3 as shown in step 3 of the execution_table.
Can any function be called with infix notation?
No, only functions with a single parameter and marked with the 'infix' keyword can be called this way, as shown in step 1.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 3, what is the value of 'this' inside the function?
Aresult
B"Hi "
C3
Dundefined
💡 Hint
Check the 'Evaluation' column at step 3 where 'this' is used as the repeat count.
At which step does the variable 'result' get its value assigned?
AStep 4
BStep 3
CStep 2
DStep 5
💡 Hint
Look at the 'Action' column for assignment to 'result' in the execution_table.
If the function was not marked 'infix', how would the call change?
A3 times "Hi "
B3.times("Hi ")
C"Hi ".repeat(3)
Dtimes(3, "Hi ")
💡 Hint
Without 'infix', Kotlin requires dot and parentheses syntax as in step 2.
Concept Snapshot
infix fun <Type>.functionName(param: ParamType) = ...
Call with: value functionName argument
Only single parameter functions can be infix.
Improves readability by removing dots and parentheses.
Useful for DSLs and expressive code.
Full Transcript
This visual execution shows how Kotlin infix functions work. First, an infix function 'times' is defined to repeat a string a number of times. Then it is called using infix syntax without dots or parentheses. Inside the function, 'this' refers to the integer 3. The string "Hi " is repeated 3 times, producing "Hi Hi Hi ". This result is assigned to the variable 'result' and printed. The program ends after printing. Key points are that infix functions must have one parameter and be marked with 'infix' to allow this readable call style.