Search⌘ K

Infix Functions

Explore how to write and use Kotlin infix functions that place the function name between two arguments to create cleaner, more readable code. Understand the limitations and syntax of infix functions, and practice implementing your own. This lesson sets the foundation for using operator functions next.

Infix functions are a great tool to improve readability of function calls in certain cases.

What are Infix Functions? #

In programming, function calls are typically written in prefix notation, meaning that the function name is in front, followed by its arguments in parentheses:

gcd(54, 24)

With infix functions, however, the function name stands between the arguments. You’ve already seen this with the predefined to function, which creates Pairs (to put into a map):

"Dwayne" to 3.14159

Here, the function name to is surrounded by its two arguments, which brings us to the first limitation of infix functions: an infix function must have exactly two parameters. In the function call, the first argument is ...