📖 Scenario: Imagine you are building a simple calculator app that can add numbers in a way that reads like a sentence. You want to make your code easy to read and understand, just like a natural language sentence.
🎯 Goal: You will create an infix function in Kotlin to add two numbers with a readable call style, then use it to add two numbers and print the result.
📋 What You'll Learn
Create a class called Calculator with a property value of type Int
Add an infix function called add inside Calculator that takes another Calculator and returns a new Calculator with the sum of the two values
Create two Calculator objects with values 10 and 5
Use the add function in infix style to add the two objects
Print the value of the result
💡 Why This Matters
🌍 Real World
Infix functions make code easier to read, like writing sentences. This helps when building apps that do calculations or combine data.
💼 Career
Understanding infix functions is useful for Kotlin developers to write clean, readable code in Android apps and backend services.
Progress0 / 4 steps
1
Create the Calculator class with a value property
Create a class called Calculator with a property value of type Int. Initialize it using the primary constructor.
Kotlin
Hint
Use Kotlin's primary constructor syntax to create the class with a property.
2
Add an infix add function to Calculator
Inside the Calculator class, add an infix function called add that takes another Calculator as a parameter named other. It should return a new Calculator with the sum of this.value and other.value.
Kotlin
Hint
Use the keyword 'infix' before the function and return a new Calculator with summed values.
3
Create two Calculator objects and add them using infix
Create two Calculator objects named calc1 with value 10 and calc2 with value 5. Then create a new Calculator called result by adding calc1 and calc2 using the infix add function.
Kotlin
Hint
Create objects with the given values and use the infix function without dot or parentheses.
4
Print the value of the result
Write a println statement to print the value property of the result object.
Kotlin
Hint
Use println(result.value) to show the sum.
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
Step 1: Understand the role of infix functions
Infix functions allow calling a function without using dots and parentheses, making code more readable.
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.
Final Answer:
To call functions without dots and parentheses for more readable code -> Option A
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
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.
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.
Final Answer:
infix fun add(x: Int): Int = this + x -> Option D
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
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).
Step 2: Calculate the result
5 + 10 = 15, so the printed output is 15.
Final Answer:
15 -> Option C
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
Step 1: Check infix function parameter rules
Infix functions must have exactly one parameter. Here, greet has two parameters, which is invalid.
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.
Final Answer:
Infix functions cannot have more than one parameter -> Option B
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
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).