Bird
Raised Fist0
Kotlinprogramming~10 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What is the main purpose of using infix functions in Kotlin?
easy
A. To call functions without dots and parentheses for more readable code
B. To allow functions to have multiple parameters
C. To make functions run faster
D. To restrict functions to only private visibility

Solution

  1. Step 1: Understand the role of infix functions

    Infix functions allow calling a function without using dots and parentheses, making code more readable.
  2. Step 2: Compare options with this purpose

    Only To call functions without dots and parentheses for more readable code correctly describes this purpose; others mention unrelated features.
  3. Final Answer:

    To call functions without dots and parentheses for more readable code -> Option A
  4. Quick Check:

    Infix functions improve readability = D [OK]
Hint: Infix means calling without dots and parentheses [OK]
Common Mistakes:
  • Thinking infix allows multiple parameters
  • Believing infix changes function speed
  • Confusing infix with visibility modifiers
2. Which of the following is the correct syntax to declare an infix function in Kotlin?
easy
A. fun add infix(x: Int): Int = this + x
B. infix fun add(x: Int): Int
C. fun infix add(x: Int, y: Int): Int = x + y
D. infix fun add(x: Int): Int = this + x

Solution

  1. Step 1: Recall infix function syntax

    An infix function must be declared with the keyword infix before fun, have exactly one parameter, and a body.
  2. Step 2: Check each option

    infix fun add(x: Int): Int = this + x correctly uses infix fun add(x: Int): Int = this + x. fun infix add(x: Int, y: Int): Int = x + y has two parameters, invalid. infix fun add(x: Int): Int misses function body. fun add infix(x: Int): Int = this + x has wrong keyword order.
  3. Final Answer:

    infix fun add(x: Int): Int = this + x -> Option D
  4. Quick Check:

    Infix keyword before fun, one parameter only = A [OK]
Hint: Infix keyword comes before fun, one parameter only [OK]
Common Mistakes:
  • Declaring multiple parameters in infix function
  • Placing infix keyword after fun
  • Omitting function body
3. What is the output of the following Kotlin code?
class Calculator {
    infix fun add(x: Int): Int = x + 10
}

fun main() {
    val calc = Calculator()
    println(calc add 5)
}
medium
A. SyntaxError
B. 5
C. 15
D. 10

Solution

  1. Step 1: Understand the infix function call

    The infix function add adds 10 to the parameter x. Calling calc add 5 means calc.add(5).
  2. Step 2: Calculate the result

    5 + 10 = 15, so the printed output is 15.
  3. Final Answer:

    15 -> Option C
  4. Quick Check:

    5 + 10 = 15 [OK]
Hint: Infix calls work like normal calls without dots [OK]
Common Mistakes:
  • Confusing infix call with normal method call syntax
  • Expecting parameter to be ignored
  • Thinking infix changes return value
4. Identify the error in the following Kotlin code using an infix function:
class Person {
    infix fun greet(name: String, age: Int) {
        println("Hello $name, age $age")
    }
}

fun main() {
    val p = Person()
    p greet ("Alice", 30)
}
medium
A. Missing parentheses in function call
B. Infix functions cannot have more than one parameter
C. Infix functions must return a value
D. Class Person cannot have infix functions

Solution

  1. Step 1: Check infix function parameter rules

    Infix functions must have exactly one parameter. Here, greet has two parameters, which is invalid.
  2. Step 2: Verify other options

    Parentheses are optional in infix calls. Infix functions can return Unit (no value). Classes can have infix functions. So only Infix functions cannot have more than one parameter is correct.
  3. Final Answer:

    Infix functions cannot have more than one parameter -> Option B
  4. Quick Check:

    One parameter max for infix = A [OK]
Hint: Infix functions take exactly one parameter [OK]
Common Mistakes:
  • Trying to use multiple parameters in infix function
  • Thinking parentheses are always required
  • Believing infix functions must return a value
5. Given the following Kotlin code, what will be the output?
class Box(val value: Int) {
    infix fun combine(other: Box): Box = Box(this.value + other.value)
}

fun main() {
    val box1 = Box(3)
    val box2 = Box(7)
    val result = box1 combine box2 combine Box(5)
    println(result.value)
}
hard
A. 15
B. 10
C. SyntaxError
D. 35

Solution

  1. Step 1: Understand chaining of infix calls

    The expression box1 combine box2 combine Box(5) is evaluated left to right as (box1 combine box2) combine Box(5).
  2. Step 2: Calculate each combine call

    First combine: 3 + 7 = 10, returns Box(10). Second combine: 10 + 5 = 15, returns Box(15). Printing result.value outputs 15.
  3. Final Answer:

    15 -> Option A
  4. Quick Check:

    3+7=10, 10+5=15 [OK]
Hint: Infix calls chain left to right like normal calls [OK]
Common Mistakes:
  • Assuming combine adds values incorrectly
  • Thinking infix calls don't chain
  • Confusing order of operations
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard